Constructor and parameters pls help

Pages: 12
You can delete line 13. Line 13 and line 4 are apparently redundant, or otherwise it's not clear what you wanted there.

You have declared a default constructor on line 15. A default constructor is a constructor which doesn't accept any arguments.

You should define that constructor by either:
1. adding a default-specifier
2. adding a member-initializer-list (optionally) and a body.

You can delete line 14. You don't need to define a destructor; the default one is fine in this case. If you prefer or are required, you can add a default-specifier.

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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# include <iostream>

// using namespace std;
// `using namespace` is strongly discouraged, especially for beginners.  
// Don't use it.  Call things by their full name.  

class Vehicle {
public:
    // default constructor
    Vehicle() {} // alternatively Vehicle() = default;

    // parametrized constructor
    Vehicle(std::string vehicleType, int numberOfDoors, int maxSpeed)
        : type  {vehicleType}
        , nDoors{numberOfDoors}
        , speed {maxSpeed} {
    }
         
    // accessors
    std::string getType()   const { return type;   }
    int         getNDoors() const { return nDoors; }
    int         getSpeed()  const { return speed;  }
    // mutators
    void setType  (std::string vehicleType) { type   = vehicleType;   }
    // use a name more descriptive than "setNumber" 
    void setNDoors(int numberOfDoors)       { nDoors = numberOfDoors; }
    void setSpeed (int maxSpeed)            { speed  = maxSpeed;      }
    
private:
    // reduce code duplication and verbosity in constructor body(ies) by
    // using in-class initialzation where applicable:
    // https://goo.gl/wLyadq
    // notice that doing this allows the default constructor to be empty
    std::string type { "SUV" };
    int nDoors       { 10 }; 
    int speed        { 3 };
};

int main() { 
    // prefer constexpr over const
    constexpr int numVehicles = 2;
    
    // don't use garbage words in names "myXXX", "theYYY" "aZZZ", etc.,
    Vehicle vehicles[numVehicles];

    for (int i = 0; i < numVehicles; ++i){
        // restrict the scope of names as much as possible
        std::string type;
        int nDoors;
        int maxSpeed;
        
        std::cout << "\nPlease enter the vehicle type " << i+1 << ": ";
        std::getline(std::cin, type);
        vehicles[i].setType(type);
        
        std::cout << "Enter the amount of doors the vehicle have " << i+1 << ": ";
        std::cin >> nDoors;
        vehicles[i].setNDoors(nDoors);
        
        std::cout << "Enter the maximum speed for the vehicle " << i+1 << ": ";
        std::cin >> maxSpeed;
        vehicles[i].setSpeed(maxSpeed);
    }

   for(int i = 0; i < numVehicles; ++i) {
        std::cout << "Vehicle " << i+1 << " = " << vehicles[i].getType() << ", "
                  << vehicles[i].getNDoors() << " doors, and "
                  << vehicles[i].getSpeed() << " miles per hour.\n";
    }
}


http://coliru.stacked-crooked.com/a/d019b0668ce7d698
Last edited on
@mbozzi
Thank you for yor advice. Your code is really effective and easy to read.
Could your rows 34-36 be also written
1
2
3
    std::string type {"SUV"};
    int nDoors       {10}; 
    int speed        {3};

?
Could your rows 34-36 be also written
1
2
3
    std::string type {"SUV"};
    int nDoors       {10}; 
    int speed        {3};


Yes, and they should be. Thanks.
Last edited on
Also note that you setter and multi-argument constructor.

Should probably be passing the string as a const reference, to avoid the unnecessary copy.

void setType (const std::string& vehicleType) { type = vehicleType; }

Remember passing a non-trivial type like a std::string by value requires a copy that can be expensive.


Last edited on
Topic archived. No new replies allowed.
Pages: 12