Constructor + member function

I am trying to write a program with 5 data members constructor initalize the first 3 data members. A displayCarDetails member function should display all the data members and its values.
The problem is that it does not display it.

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
71
72
73
74
#include <iostream>
#include <string>

class motorVehicle{
public:
MotorVehicle(std::string make,
             std::string fuelType,
             int yearofManufacture,
             std::string color,
             int engineCapacity)
: name { make },
 fuelt { fuelType },
 year { yearofManufacture }{

}
void setName(std::string make){
    name = make;
}

std::string getName() const{
    return name;
}

void setFuelt(std::string fuelType){
    fuelt = fuelType;
}

std::string getFuelt() const{
    return fuelt;
}

void setYear(int yearofManufacture){
    year = yearofManufacture;
}

int getYear() const{
    return year;
}   
void setVcolor(std::string color){
    vcolor = color;
}

std::string getVcolor() const{
    return color;
}

void setEngine(int engineCapacity){
    engine = engineCapacity;
}

int getEngine() const{
    return engine;
}

void displayCarDetails(){
    std::cout<<"Make: "<<make
    <<"\nFuel type: "<< fuelType
    <<"\nYear of Manufacture: "<< yearofManufacture
    <<"\nColor: "<< color
    <<"\nEngine capacity: "<< engineCapacity;
}
private:
std::string name;
std::string fuelt;
int year;
std::string vcolor;
int engine;
};

int main(){
MotorVehicle vehicle1{"Skoda Octavia", "diesel", 2003};
vehicle1.displayCarDetails();
}
Last edited on
The problem is that this code does not compile, for many many reasons. For example, on line 71 you try to create an object of type MotorVehicle, and no such type exists. C++ is case sensitive.

Your compiler will have told you about the errors. You should read what the compiler says to you. You cannot run programs that don't even compile.

Other problems:

Constructor uses M instead of m.
You try to use the member variable make. Which does not exist.
You try to use the member variable fuelType. Which does not exist.
You try to use the member variable yearofManufacture. Which does not exist.
You try to use the member variable color. Which does not exist.
You try to use the member variable engineCapacity. Which does not exist.
You try to create an object using a constructor which accepts THREE parameters, {"Skoda Octavia", "diesel", 2003} but the constructor expects FIVE parameters (std::string make, std::string fuelType, int yearofManufacture, std::string color, int engineCapacity)
Last edited on
Topic archived. No new replies allowed.