Constructor and parameters pls help

Pages: 12
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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <string>

class Vehicle
{
public:
    Vehicle(std::string vehicleType, int numberOfDoors, int maxSpeed)
         : type{vehicleType}
    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;
};
There are several issues with your constructor.

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.


I tried adding all 3 class member variables on it. But it still gives an error saying there needs to be an open { after : type{vehicleType}

So you're saying
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#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]

Any ideas?
1
2
3
4
5
6
7
 Vehicle(std::string vehicleType, int numberOfDoors, int maxSpeed)
         : type(vehicleType), number(numberOfDoors), speed(maxSpeed) {}

//OR

 Vehicle(std::string vehicleType, int numberOfDoors, int maxSpeed)
         : type{vehicleType}, number{numberOfDoors}, speed{maxSpeed} {}


http://www.learncpp.com/cpp-tutorial/8-5a-constructor-member-initializer-lists/
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.
What about something like this?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <iostream>
#include <string>

class Vehicle
{
public:
    Vehicle (int numberOfDoors, int maxSpeed, const std::string& vehicleType = "SUV")
            : number(numberOfDoors), speed(maxSpeed), type(vehicleType)
        {classexist = 13;}
    Vehicle () {classexist = 13;}

    void setType(const 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;}
    int getClassExist() const {return classexist;}

private:
    int number;
    int speed;
    std::string type;
    int classexist;
};

void claimExistance(const Vehicle& car);

int main() {
    Vehicle truck(2, 40, "truck");
    Vehicle sedan(3, 65, "sedan");
    Vehicle suv(2, 83);

    claimExistance(truck);
    claimExistance(sedan);
    claimExistance(suv);

    return 0;
}

void claimExistance(const Vehicle& car) {
    std::cout << "Does truck exist? --> ";
    if(13 == car.getClassExist()) {
        std::cout << "Yes, " << car.getType() << " has " 
                    << car.getNumber() << " doors and can run at " 
                    << car.getSpeed() << " something per hour." 
                    << std::endl;
    } else {
        std::cout << "No, it does not exists." << std::endl;
    }
}

It has to be splitted into several files, of course.
@Enoziat
Line 9, 10:
Prefer initialization to assignment in constructors:
https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#c49-prefer-initialization-to-assignment-in-constructors

Or, another more applicable suggestion:
Prefer in-class initializers to member-initializers in constructors for constant initializers.
https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#c48-prefer-in-class-initializers-to-member-initializers-in-constructors-for-constant-initializers

Lines 12-21:
Avoid trivial getters and setters:
https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#Rh-get

For consistency, I would suggest using the uniform initialization syntax (braces, not parens) in member-initializer-lists and almost everywhere else.
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.

this is my header now..
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#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;


and this is my cpp file

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>
#include <string>
#include "Vehicle.h"

using namespace std;
int main()
{   const int numVehicles = 8;
    Vehicle myVehicle[numVehicles];
    string theVehicle;
    int numDoors;
    int maxSpeed;
    for (int i = 0; i < numVehicles; i++){
    cout << "\nPlease enter the vehicle type " << i+1 << ": ";
    getline(cin, theVehicle);
    myVehicle[i].setType(theVehicle);
    cout << "Enter the amount of doors the vehicle have " << i+1 << ": ";
    cin >> numDoors;
    cin.ignore();
    myVehicle[i].setNumber(numDoors);
    cout << "Enter the maximum speed for the vehicle " << i+1 << ": ";
    cin >> maxSpeed;
    cin.ignore();
    myVehicle[i].setSpeed(maxSpeed);
}

   for(int i = 0; i < numVehicles; i++)
    {
        cout << "Vehicle " << i+1 << " = " << myVehicle[i].getType() << ","
        << myVehicle[i].getNumber()<< " doors, and "
        << myVehicle[i].getSpeed() << " miles per hour." << endl;
    }
}


However, when I run my cpp file, it gives me errors.
Last edited on
Can you be more specific? What errors? Please post the exact text of the errors.
line 8: no matching function for call to 'Vehicle:: Vehicle();
because you don't have a default constructor
So how do I add a default constructor? I'm confused
So how do I add a default constructor? I'm confused

Same as how you did your other constructor, but with no parameters and initialising member variables to their default values.
1
2
3
4
Vehicle() : type{}, number{}, speed{}
{
    
}

You also don't need a semi-colon after the ending braces of your constructor.
So I added a default constructor
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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;}

     Vehicle(int, int, string);
    ~Vehicle();
     Vehicle();
    std::string getType() const {return type;}
    int getNumber() const {return number;}
    int getSpeed() const {return speed;}

private:
    std::string type;
    int number;
    int speed;
};


the error message now comes up at line 13..
states "expect ')' before ',' token.
Last edited on
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.
you forgot the std:: on line 13 but idk if thats the error
When I put std::string it said Vehicle::Vehicle(std::string, int, int) can not be overloaded
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.
Pages: 12