Whats up guys ,
I have to design a a motorbike class today using inheritance from a vehicle class that i had previously designed. My goals are
1) to design a vehicle class with speed and turn rate - DONE
2) From this basic class design a motorbike with Speed (Inherited from motor class) and Type of bike (Char type)
I keep getting one error in my .cpp file in line 18 saying "Motorbike was not declared in this scope"
You have a motorbike class defined in motorbike.h but there's no motorbike object created in your main.cpp. Line 15 is trying to create a vehicle object, not a motorbike object. Motorbikes may be vehicles and inherit vehicle properties, but vehicles are not necessarily motorbikes.
You don't need to #include "vehicle.h" in your vehicle.h file, just the #ifndef, #define and #endif. I don't see that you need to include iostream anywhere besides the main.cpp where you using the output functions.
Im currently having trouble with my character array and i thought i will learn that before going on to the tougher stuff. i keep getting a "Invalid array assignment" error in my motorbike.h
As for assigning an array. BikeType is currently a C style string, so you would need to use a C library call to copy a value to it.
strcpy (BikeType, "Suzuki");
It's best not to mix C and C++ styles.
The better way to do this is:
1 2 3 4 5 6 7 8 9 10 11 12
#include <string> // Use the C++ string library
...
// Replace line 23 in header with:
string BikeType;
...
// Replace line 30 with
BikeType = "Suzuki";
...
// And your getter needs to change
string motorbike::getBikeType()
{ return BikeType;
}
Thank you AbstractionAnon, after a bit of reading up i now understand the string way to display characters.
In line 9-10 i instantiated vehicle but im still getting errors. There is something very wrong here could someone show me how to fix this.
Please excuse me i am new to C++
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#include "vehiclee.h" //include the declaration of the class
#include "Motorbike.h"
#include <iostream>
usingnamespace std;
int main() {
vehicle vehicle ; //object of the class vehicle
cout << "\nThe speed is: " << vehicle.getSpeed(); //shows speed on screen
cout << "\nThe turn time is: " << vehicle.getTurnTime();
motorbike motorbike;
cout << "\n The Number Of Wheels is: " << motorbike.getWheels;
cout << "\n The Bike Type is: " << motorbike.getBikeType();
return 0;
}
Hey Guys ! appreciate you`s helping me in my journey of learning c++ programming.
One more question however . Lets say i wanted the user to be able to enter there own number for the motorbike attributes. How would i go about doing this ? I haven't tried it yet myself im just trying to get knowledge beforehand .
Would it be as simple as changing the get functions into ask
1 2 3 4 5 6 7 8
class motorbike : public vehicle {
public: //declare functions
int askWheels();
char askBikeType(); //function
motorbike (); //contructor
my question is were would i put my
1 2
cout << "\nEnter The Number Of Wheels is: " <<endl;
cin << motorbike.Wheels << endl;
Any suggestions on how i should go about modifying this code ?
You created an askWheels function prototype. This code goes in your implementation of that function.
A few of problems with line 2 in your second snippet.
1) The cin operator is the wrong direction. Input operations use >>.
2) You don't use endl with input operations.
3) Since this code goes in a member function, you don't qualify access to the member variable.
1 2 3 4 5
int motorbike::askWheels()
{ cout << "\nEnter The Number Of Wheels: " <<endl;
cin >> Wheels;
return Wheels;
}