InHeritance Program

Hey guys, I really appreciate everything that you do for me and it really helps me a lot...I have learned so much. But I have a new problem, for my assignment this week it deals with InHeritance and Pointers and I have 90% of my program set up but I need help with the last 10% of it.

Here is my assignment and I have bold the section that I need help with!

Create a Vehicle class to store information

Attributes:

make (you can also think of this as the Manufacturer)

model

color

year

mileage

(Note: All attributes should not be public.)

Behaviors:

1. Create mutator (Set) and accessor (Get) functions for all attributes of this class.

2. Create a default constructor that initializes all of the attributes to default values (blanks in the case of strings or 0 in the case of numbers.)

3. Make sure to have validation to ensure that the mileage is never set to a number less than 0.

4. Create a constructor that takes the make, model, year,color, and mileage values and sets them for a newly created vehicle.

5. Create a virtual function called details that does not do anything. (This will force the cars, trucks, and motorcycles that inherit from this class to overload this function.)




Create a Car class that inherits from the vehicle class

Attributes:

There are no additional attributes

Behaviors:

1. Override the details function to print the details for this car.

2. (Example on an output line of a car’s details:

“The current car is a 2008 Red Ford Mustang with 5000 miles.”)



Create a Truck class that inherits from the vehicle class

Attributes:

bedsize

Behaviors:

1. Create mutator (Set) and accessor (Get) functions for all attributes of this class.

2. Override the details function to print the details for this truck.

(Example on an output line of a truck’s details:

“The current truck is a 2006 Black Ford F150 with 10000 miles and a 10-foot bedsize.”)



Create a Motorcycle class that inherits from the vehicle class

Attributes:

Biketype

Note: (Some types of bikes would be chopper, cruiser, dirt bike, touring, and custom. )

Behaviors:

1. Create mutator (Set) and accessor (Get) functions for all attributes of this class.

2. Override the details function to print the details for this motorcycle.

(Example on an output line of a motorcycle’s details:

“The current motorcycle is a 2007 Silver Harley Cherokee chopper with 8000 miles”)







Create a program with a main() to be used by the inventory manager to keep track of all of the vehicles on the lot.

1) Create the following vehicles of the correct type

Make Model Color Year Type Bed size Bike type

1. Porsche 911 Silver 2005 Car

2. Ford Mustang Red 2007 Car

3. Kawasaki Ninja Black 2004 Motorcycle custom

4. Ford F150 White 2007 Truck 10-foot

5. Voltzwagon Jetta Black 2006 Car

6. Harley Cherokee Silver 2000 Motorcycle chopper

7. Toyota Tacoma Blue 2002 Truck 12-foot

8. Honda CBR1500CC Red 2008 Motorcycle cruiser

9. <<Your dream vehicle here>>

2) Prompt the user to change:

a. The model of one car.

b. The bed size of one truck.

c. The bike type for one motorcycle.

d. The year of any one vehicle (you choose the type).

3) Create and array of pointers to these objects and use a loop to call the details() function for each one in turn and print out the details for all vehicles on the lot.



And here is my current code!

Vehicle.h
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#ifndef VEHICLE_H
#define VEHICLE_H

#include <iostream>
#include <fstream>
#include <iomanip>
#include <functional>
#include <algorithm>
#include <string>
#include <cstdlib>
#include <sstream>
using namespace std;

//Vehicle Class
class Vehicle {
protected:
	Vehicle myVehicle[9];
	string make;  //make
    string model; // model
    string color;  // color
    int	year;  // year
    int mileage;  // miles on car
	string type;  //Type of vehicle

public:
	//Constructor that will set information for a new car
	void New_vehicle (string a, string b, string c, int d, int e) 
	{make = a; model = b; color = c; year = d; mileage = e;}
	
	Vehicle(); //Default constructor
	Vehicle(string, string, string, int, int, string);
	//mutator and accessor functions
	void setMake(string);
    void setModel(string);
    void setColor(string);
    void setYear(int);
    void setMileage(int);
	void setType(string);

    string getMake();
    string getModel();
    string getColor();
    int getYear();
    int getMileage();
	string getType();

	//Check mileage to see if valid
	void valid_mileage(int);

	//virtual function
	virtual void details() {
	}

};
//Sets to default values
Vehicle::Vehicle() {
	make = " ";
    model = " ";
    color = " ";
    year = 0;
    mileage = 0;
	type = " ";
}

Vehicle::Vehicle(string make, string model, string color, int year, int mileage, string type) {
    Vehicle::make =  make;
    Vehicle::model = model;
    Vehicle::color = color;
    Vehicle::year = year;
    valid_mileage(mileage);
	Vehicle::type = type;
}

void Vehicle::setMake(string make) {
    Vehicle::make = make;
}

void Vehicle::setModel(string model) {
    Vehicle::model = model;
}

void Vehicle::setColor(string color) {
    Vehicle::color = color;
}

void Vehicle::setYear(int year) {
    Vehicle::year = year;
}

void Vehicle::setMileage(int mileage) {
    valid_mileage(mileage);
}

void Vehicle::setType(string type) {
	Vehicle::type = type;
}


string Vehicle::getMake() {
    return make;
}
string Vehicle::getModel() {
    return model;
}
string Vehicle::getColor() {
    return color;
}
int Vehicle::getYear() {
    return year;
}
int Vehicle::getMileage() {
    return mileage;
}

string Vehicle::getType() {
	return type;
}


void Vehicle::valid_mileage(int mileage) {
    if (mileage>=0)
        Vehicle::mileage=mileage;
    else {
        Vehicle::mileage=0;
        cout << "WARNING! You have entered invalid mileage!\n";
    }

	    Vehicle& getVehicle(int n) {
        return myVehicle[n];
    }
};

#endif 


Car.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#ifndef CAR_H
#define CAR_H

#include "Vehicle.h"

//Car class that inherits from the vehicle class
class Car:public Vehicle
	{
	private:
	//There are no new attributes
	public:
	void details() {
	cout << "The current car is a " << year << ' ' << color << ' '
    << make << ' ' << model << " with " << mileage << " miles.\n";
	}
};

#endif 


Truck.h
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
#ifndef TRUCK_H
#define TRUCK_H

#include "Vehicle.h"

//Truck class that inherits from the vehicle class
class Truck:public Vehicle 
	{
	private:
		int bedsize; //BedSize

	public:
		
		Truck(); //constructor
		Truck(int);

		//mutator and accessor functions
		void setBedsize(int);
		int getBedsize();

	};

	Truck::Truck() {
		bedsize = 0;
	}

	Truck::Truck(int bedsize) {
		Truck::bedsize = bedsize;
	}

	void Truck::setBedsize(int bedsize) {
		Truck::bedsize = bedsize;
	}

	int Truck::getBedsize() {
		return bedsize;
	}

	void details() {
	cout << "The current truck is a " << year << ' ' << color << ' '
    << make << ' ' << model << " with " << mileage << " miles and a " << bedsize << "-foot bedsize.\n";
	}
};
#endif


Motorcycle.h
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
#ifndef MOTORCYCLE_H
#define MOTORCYCLE_H

#include "Vehicle.h"

//Truck class that inherits from the vehicle class
class Motorcycle:public Vehicle
	{
	private:
		string bikeType; //BedSize

	public:
		Motorcycle(); //constructor
		Motorcycle(string);

		//mutator and accessor functions
		void setBikeType(string);
		string getBikeType();

	};

	Motorcycle::Motorcycle() {
		bikeType = " ";
	}

	Motorcycle::Motorcycle(string bikeType) {
		Motorcycle::bikeType = bikeType;
	}

	void Motorcycle::setBikeType(string bikeType) {
		Motorcycle::bikeType = bikeType;
	}

	int Motorcycle::getBikeType() {
		return bikeType;
	}

	void details() {
	cout << "The current motorcycle is a " << year << ' ' << color << ' '
    << bikeType << ' ' << model << " with " << mileage << " miles.\n";
	}
};
#endif 


Vehicle.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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include "Car.h"
#include "Truck.h"
#include "Motorcycle.h"
using namespace std;

int main() {
	
	int tempInt;
	string tempString;
	Vehicle myVehicle;

	for (int i = 0; i<9; i++) {
        cout << "Enter the Make of the Vehicle: ";
        cin >> tempString;
        myVehicle.getVehicle(i).setMake(tempString);

        cout << "Enter the Model of the Vehicle: ";
        cin >> tempString;
        myVehicle.getVehicle(i).setModel(tempString);

        cout << "Enter the Color of the Vehicle: ";
        cin >> tempString;
        myVehicle.getVehicle(i).setColor(tempString);

        cout << "Enter the Year of the Vehicle: ";
        cin >> tempInt;
        myVehicle.getVehicle(i).setYear(tempInt);

		cout << "Enter the Mileage of Vehicle: ";
		cin >> tempInt;
		myVehicle.getVehicle(i).setMileage(tempInt);

		cout << "Enter the Type of Vehicle: ";
		cin >> tempString;
		myVehicle.getVehicle(i).setType(tempString);

		cout << "Enter the Bed Size of Truck(if entered as truck): ";
		cin >> tempInt;
		myVehicle.getVehicle(i).setBedSize(tempInt);

		cout << "Enter the Bike Type(if entered as bike): ";
		cin >> tempString;
		myVehicle.getVehicle(i).setBikeType(tempString);


        cout << "__________________________________________________________\n"<< endl;
    }


Please assist me guys and if you see where I have done anything wrong then please help me out!
You can't do this because it requires an infinite amount of memory:

1
2
3
class Vehicle {
protected:
	Vehicle myVehicle[9];


You defined a type attribute but you don't use it.

The non-default constructor for Truck and Motorcycle should allow you to set the
common characteristics as well as the specialized ones.

Instead of valid_mileage, why not use an unsigned data type? Then the check isn't
necessary.

Here is my new cpp file please assist with the above problem using 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
#include "Car.h"
#include "Truck.h"
#include "Motorcycle.h"
using namespace std;

int main() {
	
	int tempInt;
	string tempString;
	int menu=0;

	// My Vehicle set up(Make, model, color, year, type,bedsize, bike, mileage)
	Vehicle *myVehicles[9];
	myVehicles[0] = new Vehicle("Porsche","911","Silver",2005,"Car", 0, " ", 12000);
	myVehicles[1] = new Vehicle("Ford","Mustang","Red",2007,"Car", 0, " ", 14213);
	myVehicles[2] = new Vehicle("Kawasaki","Ninja","Black",2004,"Motorcycle", 0, " Custom ", 11989);
	myVehicles[3] = new Vehicle("Ford","F150","White",2007,"Truck", 10, " ", 23458);
	myVehicles[4] = new Vehicle("Voltzwagon","Jetta","Black",2006,"Car", 0, " ", 45000);
	myVehicles[5] = new Vehicle("Harley","Cherokee","Silver",2000,"Motorcycle", 0, "Chopper ", 53246);
	myVehicles[6] = new Vehicle("Toyota","Tacoma","Blue",2002,"Truck", 12, " ", 73000);
	myVehicles[7] = new Vehicle("Honda","CBR1500CC","Red",2008,"Motorcycle", 0, "Cruiser", 7050);
	myVehicles[8] = new Vehicle("Toyota","Tacoma","White",2009,"Truck", 6, " ", 10);

	 while (menu!=6)//keep going untill 6 is selected
	{
        cout << "Vehicle Menu\n\n";			//Menu
		cout << "1. Change the Model of one Tar:\n";
		cout << "2. Change the Bed Size of one Truck:\n";
		cout << "3. Change the Bike Type for one Motorcycle:\n";
		cout << "4. Change the Year of a Truck:\n";
		cout << "5. Display the Vehicle Inventory List:\n";
		cout << "6. Exit the Program:\n\n\n";

        cout << "Please choose one of the above options: ";
        cin >> menu;
        cin.ignore();
        cout << "_______________________________________________________\n";

	 }
Last edited on
Oh and I changed my Vehicle.h 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
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#ifndef VEHICLE_H
#define VEHICLE_H

#include <iostream>
#include <fstream>
#include <iomanip>
#include <functional>
#include <algorithm>
#include <string>
#include <cstdlib>
#include <sstream>
using namespace std;

//Vehicle Class
class Vehicle {
protected:
	string make;  //make
    string model; // model
    string color;  // color
    int	year;  // year
    int mileage;  // miles on car
	string type;  //Type of vehicle

public:
	//Constructor that will set information for a new car
	void New_vehicle (string a, string b, string c, int d, int e) 
	{make = a; model = b; color = c; year = d; mileage = e;}
	
	Vehicle(); //Default constructor
	Vehicle(string, string, string, int, string, int, string, int);
	//mutator and accessor functions
	void setMake(string);
    void setModel(string);
    void setColor(string);
    void setYear(int);
    void setMileage(int);
	void setType(string);

    string getMake();
    string getModel();
    string getColor();
    int getYear();
    int getMileage();
	string getType();

	//Check mileage to see if valid
	void valid_mileage(int);

	//virtual function
	virtual void details() {
	}

};
//Sets to default values
Vehicle::Vehicle() {
	make = " ";
    model = " ";
    color = " ";
    year = 0;
    mileage = 0;
	type = " ";
}
	// My Vehicle set up(Make, model, color, year, type,bedsize, bike, mileage)
Vehicle::Vehicle(string make, string model, string color, int year, string type,int bedSize, string bikeType, int mileage) {
    Vehicle::make =  make;
    Vehicle::model = model;
    Vehicle::color = color;
    Vehicle::year = year;
    valid_mileage(mileage);
	Vehicle::type = type;
}

void Vehicle::setMake(string make) {
    Vehicle::make = make;
}

void Vehicle::setModel(string model) {
    Vehicle::model = model;
}

void Vehicle::setColor(string color) {
    Vehicle::color = color;
}

void Vehicle::setYear(int year) {
    Vehicle::year = year;
}

void Vehicle::setMileage(int mileage) {
    valid_mileage(mileage);
}

void Vehicle::setType(string type) {
	Vehicle::type = type;
}


string Vehicle::getMake() {
    return make;
}
string Vehicle::getModel() {
    return model;
}
string Vehicle::getColor() {
    return color;
}
int Vehicle::getYear() {
    return year;
}
int Vehicle::getMileage() {
    return mileage;
}

string Vehicle::getType() {
	return type;
}


void Vehicle::valid_mileage(int mileage) {
    if (mileage>=0)
        Vehicle::mileage=mileage;
    else {
        Vehicle::mileage=0;
        cout << "WARNING! You have entered invalid mileage!\n";
    }

	    Vehicle& getVehicle(int n) {
        return myVehicle[n];
    }
};

#endif
Last edited on
I modified my code guys, but just for testing purposed I took out all of the stuff for the menu(since that's where the remaining errors are) and when I did, my program worked but it did not assign any of the vehicles to the correct class. So if someone can help me get my errors fixed(or explain how I need to fix them) and please also tell me how to get all of the motorcycles in the array to be associated with the motorcycle class and how to get the cars associated with the car class, and how to get the trucks associated with the truck class...so that the details will print the proper information.

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
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#include "Car.h"
#include "Truck.h"
#include "Motorcycle.h"
using namespace std;

int main() {
	
	int tempInt;
	string temp;
	int menu=0;
	int current_vehicle=0; 
	int	tmpInt=0;

	// My Vehicle set up(Make, model, color, year, type,bedsize, bike, mileage)
	Vehicle *myVehicles[9];
	myVehicles[0] = new Car("Porsche","911","Silver",2005, 12000);
	myVehicles[1] = new Car("Ford","Mustang","Red",2007, 14213);
	myVehicles[2] = new Motorcycle("Kawasaki","Ninja","Black",2004, " Custom ", 11989);
	myVehicles[3] = new Truck("Ford","F150","White",2007, 10, 23458);
	myVehicles[4] = new Car("Voltzwagon","Jetta","Black",2006, 45000);
	myVehicles[5] = new Motorcycle("Harley","Cherokee","Silver",2000, "Chopper ", 53246);
	myVehicles[6] = new Truck("Toyota","Tacoma","Blue",2002, 12, 73000);
	myVehicles[7] = new Motorcycle("Honda","CBR1500CC","Red",2008, "Cruiser", 7050);
	myVehicles[8] = new Truck("Toyota","Tacoma","White",2009, 6, 10);

	 while (menu!=7)//keep going until 7 is selected
	{
        cout << "Vehicle Menu\n\n";			//Menu
		cout << "1. Select the Vehicle to edit:\n";
		cout << "2. Change the Model of one Car:\n";
		cout << "3. Change the Bed Size of one Truck:\n";
		cout << "4. Change the Bike Type for one Motorcycle:\n";
		cout << "5. Change the Year of a Truck:\n";
		cout << "6. Display the Vehicle Inventory List:\n";
		cout << "7. Exit the Program:\n\n\n";

        cout << "Please choose one of the above options: ";
        cin >> menu;
        cin.ignore();
        cout << "_______________________________________________________\n";

	 

	 	//switch begin
        switch ( menu )
		 {
        case 1:
			//Here the user will select which vehicle on the lot that they want to edit!
			cout << " Pick a vehicle to edit\n";
			cin >> current_vehicle;
            --current_vehicle;
            cin.ignore();
            cout << "_______________________________________________________\n";
            break;

        case 2:
			//User will be able to change the model of the car
            cout << " The Model of car is: "
            << *myVehicles[current_vehicle].getModel() << endl;
            cout << " Please enter the change to the model of the car: ";
            getline(cin, temp);
            myVehicles[current_vehicle].setModel(temp);
            cout << "_______________________________________________________\n";
            break;

        case 3:
			//The user can change the bed size of the current vehicle selected
            cout << " The Bed Size of the current truck is: "
            << myVehicles[current_vehicle].getBedSize() << endl;
            cout << " Please enter the change the change to the Bed Size: ";
            getline(cin, temp);
            myVehicles[current_vehicle].setBedSize(temp);
            cout << "_______________________________________________________\n";
            break;

        case 4:
			//User can change the bike type of the current vehicle selected
            cout << " The Bike Type of the current motorcycle is: "
            << myVehicles[current_vehicle].getBikeType() << endl;
            cout << " Please enter the change to the Bike Type: ";
            getline(cin, temp);
            myVehicles[current_vehicle].setBikeType(temp);
            cout << "_______________________________________________________\n";
            break;

        case 5:
			//User can change the year of the current vehicle selected
            cout << " The current year of the truck is: "
            << myVehicles[current_vehicle].getYear() << endl;
            cout << " Please enter the change to the year of the truck: ";
            getline(cin, temp);
            myVehicles[current_vehicle].setYear(temp);
            cout << "_______________________________________________________\n";
            break;

        case 6:
			//Pointers to print details for all cars
           
			cout << " The current Cars on the lot are: ";
			&Car::details;
			//Pointer to print details for all trucks
			
			cout << " The current Trucks on the lot are: ";
			&Truck::details;
          
			//Pointers to print details for all motorcycles
			
			cout << " The current Motorcycles on the lot are: ";
			&Motorcycle::details;
			cout << "_______________________________________________________\n";
            break;

		    default:
            cout << " Please restart the program, you have entered invalid information!\n\n\n";
            break;
        }		//switch end
    }		//while
    return 0;
}


ERRORS:

1>------ Build started: Project: Week 6Assignment, Configuration: Debug Win32 ------
1> VehicleMain.cpp
1>\vehiclemain.cpp(59): error C2228: left of '.getModel' must have class/struct/union
1> type is 'Vehicle *'
1> did you intend to use '->' instead?
1>\vehiclemain.cpp(62): error C2228: left of '.setModel' must have class/struct/union
1> type is 'Vehicle *'
1> did you intend to use '->' instead?
1>\vehiclemain.cpp(69): error C2228: left of '.getBedSize' must have class/struct/union
1> type is 'Vehicle *'
1> did you intend to use '->' instead?
1>\vehiclemain.cpp(72): error C2228: left of '.setBedSize' must have class/struct/union
1> type is 'Vehicle *'
1> did you intend to use '->' instead?
1>\vehiclemain.cpp(79): error C2228: left of '.getBikeType' must have class/struct/union
1> type is 'Vehicle *'
1> did you intend to use '->' instead?
1>\vehiclemain.cpp(82): error C2228: left of '.setBikeType' must have class/struct/union
1> type is 'Vehicle *'
1> did you intend to use '->' instead?
1>\vehiclemain.cpp(89): error C2228: left of '.getYear' must have class/struct/union
1> type is 'Vehicle *'
1> did you intend to use '->' instead?
1>\vehiclemain.cpp(92): error C2228: left of '.setYear' must have class/struct/union
1> type is 'Vehicle *'
1> did you intend to use '->' instead?
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
it actually tells you what to use,
1> did you intend to use '->' instead?

you need to use the arrow operator to refer to the current objects methods.

http://cplusplus.com/doc/tutorial/classes/
Pointers to Classes, about 3/4 down the page.

Also line 15: Vehicle *myVehicles[9];
You are creating a table, dual array. you may as well have said:
Vehicle myVehicles[][9];
Last edited on
Ok I changed my code to this...and here are my errors!

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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#include "Car.h"
#include "Truck.h"
#include "Motorcycle.h"
using namespace std;

int main() {
	
	int tempInt;
	string temp;
	int menu=0;
	int current_vehicle=0; 
	int	tmpInt=0;

	// My Vehicle set up(Make, model, color, year, type,bedsize, bike, mileage)
	Vehicle *myVehicles[9];
	myVehicles[0] = new Car("Porsche","911","Silver",2005, 12000);
	myVehicles[1] = new Car("Ford","Mustang","Red",2007, 14213);
	myVehicles[2] = new Motorcycle("Kawasaki","Ninja","Black",2004, " Custom ", 11989);
	myVehicles[3] = new Truck("Ford","F150","White",2007, 10, 23458);
	myVehicles[4] = new Car("Voltzwagon","Jetta","Black",2006, 45000);
	myVehicles[5] = new Motorcycle("Harley","Cherokee","Silver",2000, "Chopper ", 53246);
	myVehicles[6] = new Truck("Toyota","Tacoma","Blue",2002, 12, 73000);
	myVehicles[7] = new Motorcycle("Honda","CBR1500CC","Red",2008, "Cruiser", 7050);
	myVehicles[8] = new Truck("Toyota","Tacoma","White",2009, 6, 10);

	 while (menu!=7)//keep going until 7 is selected
	{
        cout << "Vehicle Menu\n\n";			//Menu
		cout << "1. Select the Vehicle to edit:\n";
		cout << "2. Change the Model of one Car:\n";
		cout << "3. Change the Bed Size of one Truck:\n";
		cout << "4. Change the Bike Type for one Motorcycle:\n";
		cout << "5. Change the Year of a Truck:\n";
		cout << "6. Display the Vehicle Inventory List:\n";
		cout << "7. Exit the Program:\n\n\n";

        cout << "Please choose one of the above options: ";
        cin >> menu;
        cin.ignore();
        cout << "_______________________________________________________\n";

	 

	 	//switch begin
        switch ( menu )
		 {
        case 1:
			//Here the user will select which vehicle on the lot that they want to edit!
			cout << " Pick a vehicle to edit\n";
			cin >> current_vehicle;
            --current_vehicle;
            cin.ignore();
            cout << "_______________________________________________________\n";
            break;

        case 2:
			//User will be able to change the model of the car
			cout << " The Model of car is: " << myVehicles->getModel() << endl;
            cout << " Please enter the change to the model of the car: ";
            getline(cin, temp);
            myVehicles->setModel(temp);
            cout << "_______________________________________________________\n";
            break;

        case 3:
			//The user can change the bed size of the current vehicle selected
            cout << " The Bed Size of the current truck is: "
            << myVehicles->getBedSize() << endl;
            cout << " Please enter the change the change to the Bed Size: ";
            getline(cin, temp);
            myVehicles->setBedSize(temp);
            cout << "_______________________________________________________\n";
            break;

        case 4:
			//User can change the bike type of the current vehicle selected
            cout << " The Bike Type of the current motorcycle is: "
            << myVehicles->getBikeType() << endl;
            cout << " Please enter the change to the Bike Type: ";
            getline(cin, temp);
            myVehicles->setBikeType(temp);
            cout << "_______________________________________________________\n";
            break;

        case 5:
			//User can change the year of the current vehicle selected
            cout << " The current year of the truck is: "
            << myVehicles->getYear() << endl;
            cout << " Please enter the change to the year of the truck: ";
            getline(cin, temp);
            myVehicles->setYear(temp);
            cout << "_______________________________________________________\n";
            break;

        case 6:
			//Pointers to print details for all cars
           
			cout << " The current Cars on the lot are: ";
			&Car::details;
			//Pointer to print details for all trucks
			
			cout << " The current Trucks on the lot are: ";
			&Truck::details;
          
			//Pointers to print details for all motorcycles
			
			cout << " The current Motorcycles on the lot are: ";
			&Motorcycle::details;
			cout << "_______________________________________________________\n";
            break;

		    default:
            cout << " Please restart the program, you have entered invalid information!\n\n\n";
            break;
        }		//switch end
    }		//while
    return 0;
}


ERRORS:

1>------ Build started: Project: Week 6Assignment, Configuration: Debug Win32 ------
1> VehicleMain.cpp
1>\vehiclemain.cpp(58): error C2227: left of '->getModel' must point to class/struct/union/generic type
1> type is 'Vehicle *[9]'
1>\vehiclemain.cpp(61): error C2227: left of '->setModel' must point to class/struct/union/generic type
1> type is 'Vehicle *[9]'
1>\vehiclemain.cpp(68): error C2227: left of '->getBedSize' must point to class/struct/union/generic type
1> type is 'Vehicle *[9]'
1>\vehiclemain.cpp(71): error C2227: left of '->setBedSize' must point to class/struct/union/generic type
1> type is 'Vehicle *[9]'
1>\vehiclemain.cpp(78): error C2227: left of '->getBikeType' must point to class/struct/union/generic type
1> type is 'Vehicle *[9]'
1>\vehiclemain.cpp(81): error C2227: left of '->setBikeType' must point to class/struct/union/generic type
1> type is 'Vehicle *[9]'
1>\vehiclemain.cpp(88): error C2227: left of '->getYear' must point to class/struct/union/generic type
1> type is 'Vehicle *[9]'
1>\vehiclemain.cpp(91): error C2227: left of '->setYear' must point to class/struct/union/generic type
1> type is 'Vehicle *[9]'
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========


And I am unsure what they mean!
Well I got a feeling you need to reread the link (it uses pointers to a class), and your still using a dual array which is not needed because you only have 9 objects. Perhaps if you want 9*n objects then you should keep it as is but otherwise is pointless and may have an impact on your code.

perhaps instead of using the -> operator you just need to declare the element properly. change back to the way you had it, and change the array to be single dimension and access it like this:

myVehicles[current_vehicle].getModel(); // <----- that should work.

In the example see lines 18 and 22:
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
// pointer to classes example
#include <iostream>
using namespace std;

class CRectangle {
    int width, height;
  public:
    void set_values (int, int);
    int area (void) {return (width * height);}
};

void CRectangle::set_values (int a, int b) {
  width = a;
  height = b;
}

int main () {
  CRectangle a, *b, *c;
  CRectangle * d = new CRectangle[2];
  b= new CRectangle;
  c= &a;
  a.set_values (1,2);
  b->set_values (3,4);
  d->set_values (5,6);
  d[1].set_values (7,8);
  cout << "a area: " << a.area() << endl;
  cout << "*b area: " << b->area() << endl;
  cout << "*c area: " << c->area() << endl;
  cout << "d[0] area: " << d[0].area() << endl;
  cout << "d[1] area: " << d[1].area() << endl;
  delete[] d;
  delete b;
  return 0;
}


edit: see using the arrow operator is fine, and that part of the code is correct : ->setYear(temp);
But left of the operator you are using: 1> type is 'Vehicle *[9]' (could also be written as 'Vehicle [0][9]')
instead of a pointer.
I'm sorry I did not read your code fully earlier I simply replied to what the compiler was complaining about. In the case of pointer and arrow operator to declare the pointer you could have made a declaration like: Vehicle * ptr = &myVehicles[current_vehicle];
then simply: ptr->set_values(); // would work as intended.
Last edited on
Wow thanks dude that took away a lot of the errors...now my next question is how will I get the ones from the truck class and the motorcycle class to work??

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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#include "Car.h"
#include "Truck.h"
#include "Motorcycle.h"
using namespace std;

int main() {
	
	int tempInt;
	string temp;
	int menu=0;
	int current_vehicle=0; 
	int	tmpInt=0;

	// My Vehicle set up(Make, model, color, year, type,bedsize, bike, mileage)
	Vehicle *myVehicles[9];
	myVehicles[0] = new Car("Porsche","911","Silver",2005, 12000);
	myVehicles[1] = new Car("Ford","Mustang","Red",2007, 14213);
	myVehicles[2] = new Motorcycle("Kawasaki","Ninja","Black",2004, " Custom ", 11989);
	myVehicles[3] = new Truck("Ford","F150","White",2007, 10, 23458);
	myVehicles[4] = new Car("Voltzwagon","Jetta","Black",2006, 45000);
	myVehicles[5] = new Motorcycle("Harley","Cherokee","Silver",2000, "Chopper ", 53246);
	myVehicles[6] = new Truck("Toyota","Tacoma","Blue",2002, 12, 73000);
	myVehicles[7] = new Motorcycle("Honda","CBR1500CC","Red",2008, "Cruiser", 7050);
	myVehicles[8] = new Truck("Toyota","Tacoma","White",2009, 6, 10);

	 while (menu!=7)//keep going until 7 is selected
	{
        cout << "Vehicle Menu\n\n";			//Menu
		cout << "1. Select the Vehicle to edit:\n";
		cout << "2. Change the Model of one Car:\n";
		cout << "3. Change the Bed Size of one Truck:\n";
		cout << "4. Change the Bike Type for one Motorcycle:\n";
		cout << "5. Change the Year of a Truck:\n";
		cout << "6. Display the Vehicle Inventory List:\n";
		cout << "7. Exit the Program:\n\n\n";

        cout << "Please choose one of the above options: ";
        cin >> menu;
        cin.ignore();
        cout << "_______________________________________________________\n";

	 

	 	//switch begin
        switch ( menu )
		 {
        case 1:
			//Here the user will select which vehicle on the lot that they want to edit!
			cout << " Pick a vehicle to edit\n";
			cin >> current_vehicle;
            --current_vehicle;
            cin.ignore();
            cout << "_______________________________________________________\n";
            break;

        case 2:
			//User will be able to change the model of the car
			cout << " The Model of car is: " << myVehicles[current_vehicle]->getModel() << endl;
            cout << " Please enter the change to the model of the car: ";
            getline(cin, temp);
            myVehicles[current_vehicle]->setModel(temp);
            cout << "_______________________________________________________\n";
            break;

        case 3:
			//The user can change the bed size of the current vehicle selected
            cout << " The Bed Size of the current truck is: "
            << myVehicles[current_vehicle]->getBedSize() << endl;
            cout << " Please enter the change the change to the Bed Size: ";
            getline(cin, temp);
            myVehicles[current_vehicle]->setBedSize(temp);
            cout << "_______________________________________________________\n";
            break;

        case 4:
			//User can change the bike type of the current vehicle selected
            cout << " The Bike Type of the current motorcycle is: "
            << myVehicles[current_vehicle]->getBikeType() << endl;
            cout << " Please enter the change to the Bike Type: ";
            getline(cin, temp);
            myVehicles[current_vehicle]->setBikeType(temp);
            cout << "_______________________________________________________\n";
            break;

        case 5:
			//User can change the year of the current vehicle selected
            cout << " The current year of the truck is: "
            << myVehicles[current_vehicle]->getYear() << endl;
            cout << " Please enter the change to the year of the truck: ";
            getline(cin, temp);
            myVehicles[current_vehicle]->setYear(tempInt);
            cout << "_______________________________________________________\n";
            break;

        case 6:
			//Pointers to print details for all cars
           
			cout << " The current Cars on the lot are: ";
			&Car::details;
			//Pointer to print details for all trucks
			
			cout << " The current Trucks on the lot are: ";
			&Truck::details;
          
			//Pointers to print details for all motorcycles
			
			cout << " The current Motorcycles on the lot are: ";
			&Motorcycle::details;
			cout << "_______________________________________________________\n";
            break;

		    default:
            cout << " Please restart the program, you have entered invalid information!\n\n\n";
            break;
        }		//switch end
    }		//while
    return 0;
}




Errors:

1>------ Build started: Project: Week 6Assignment, Configuration: Debug Win32 ------
1> VehicleMain.cpp
1>\vehiclemain.cpp(68): error C2039: 'getBedSize' : is not a member of 'Vehicle'
1> \vehicle.h(15) : see declaration of 'Vehicle'
1>\vehiclemain.cpp(71): error C2039: 'setBedSize' : is not a member of 'Vehicle'
1> \vehicle.h(15) : see declaration of 'Vehicle'
1>\vehiclemain.cpp(78): error C2039: 'getBikeType' : is not a member of 'Vehicle'
1> \vehicle.h(15) : see declaration of 'Vehicle'
1>\vehiclemain.cpp(81): error C2039: 'setBikeType' : is not a member of 'Vehicle'
1> \vehicle.h(15) : see declaration of 'Vehicle'
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
line 15 your still using a dual array, so I don't see how it removed the errors.

also the code is still the same as the last lot you posted ?
change line 15 from Vehicle *myVehicles[9]; to: Vehicle myVehicles[9];

secondly change all the declaration using the arrow operator to the dot operator:
this: myVehicles[current_vehicle]->setYear(tempInt); to this: myVehicles[current_vehicle].setYear(tempInt);

Then after that we can deal with the above errors you just posted, which is to do with inheritence.
More than likely will have to cast to the correct type <Truck> not Vehicle, etc.
Last edited on
Topic archived. No new replies allowed.