okay so my professor wanted me to include a constructor in my dealership class.. However, I am not clear on this parameter. I have my header file and cpp file working fine. But however when I try to add this constructor with 3 parameters, my whole program fails to work. here is the program to my header.
I have no idea if this is the right way to include it.
First the initialization list should initialize all the class member variables not just type. Second the constructor is a function so it needs a function body.
Also you may need to also define the no argument constructor as well since once you define another constructor the compiler will no longer generate this "default" constructor.
#include <string>
class Vehicle
{
public:
Vehicle(std::string vehicleType, int numberOfDoors, int maxSpeed)
: type{vehicleType}
:number{numberOfDoors}
:speed{maxSpeed}
void setType(std::string vehicleType) {
type = vehicleType;}
void setNumber(int numberOfDoors){
number = numberOfDoors;}
void setSpeed(int maxSpeed) {
speed = maxSpeed;}
std::string getType() const {return type;}
int getNumber() const {return number;}
int getSpeed() const {return speed;}
private:
std::string type;
int number;
int speed;
};
I don't understand the last part. Also, I have no idea what to include in the function body since the assignment was to instantiate vehicle to suv, truck and sedan. and to use a constructor with default parameter[SUV]
Also, pass std::string by reference, and make the parameters const in the implementation, but not in the declaration.
The following is probably a bit trivial, but you should have the function definitions in the cpp file, and just have the class declaration in the header file. The client shouldn't see the implementation. The member initialisation list is fine if it is only in the cpp file.
Your default constructor doesn't initialize number or speed.
In this case I think the easiest thing is to provide default values for all of the parameters in the 1st constructor. Then you won't need a default constructor:
1 2 3 4 5 6
Vehicle (int numberOfDoors=4, int maxSpeed=90, const std::string& vehicleType = "SUV") :
number(numberOfDoors),
speed(maxSpeed),
type(vehicleType),
classexist(13)
{} // constructor needs a body, even if it's empty.
Thanks guys. But the thing is my professor wants the user to input the values and then create a loop for the user to do it 8 times. Then create another loop to keep track. This has to be done with 3 sets, and 3 gets. So there can be multiple types of vehicles. I had a running program before inputting the constructor.
constructors are functions so you don't need to put a semicolon at the end of the definition body
a class is a new variable type, and it needs constructors so that the program knows what to do when you make a new variable of that type.
The default constructor is the constructor that takes no parameters. Like when you say 'int x' and don't assign it a value, it is calling a default constructor (at least for this example). The body of this constructor should set the member values to whatever you say their default values should be, but is not required and you could leave default constructor empty, though not recommended.
c++ supplies you with this basic default constructor so long as no type of constructor is defined. But when you made your constructor with the 3 parameters, you no longer had a default constructor.
On line 8 you get an error because you made an array of vehicles of size eight, essentially creating 8 'vehicles' using the default constructor, which doesn't exist.
well yea, what's the point of making another constructor with the exact same parameters?
You have the exact same constructor at the beginning of your class.
Are you trying to do something with that declaration on line 13? cuz otherwise you're not using it
I have no idea. That was just my assignment to create 3 sets and 3 gets as well as a constructor and based on the powerpoint my professor created, it was something different. it was related to that.