i got a problem with inheritance and polymorphism

Apr 22, 2010 at 8:02pm
i am making a program that stores the data for a car truck or van. please help with this problem.
13_14main.cpp
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
#include <cstdlib>
#include <iostream>

#include "Vehicle.h"
#include "Car.h"
#include "Truck.h"
#include "Van.h"

using namespace std;

Vehicle *vehicle=0;

int main(int argc, char *argv[])
{
    int var1=1;//sentry for the program loop
    while(var1==1){
          int var2=0;//sentry for the menu control
          cout <<"What type of vehcile?:"<<endl
               <<"1.Truck"<<endl
               <<"2.Car"<<endl
               <<"3.Van"<<endl
               <<"4.Exit"<<endl;
          cin >>var2;
          switch(var2){
                case 1:vehicle=new Truck;break;
                case 2:vehicle=new Car;break;
                case 3:vehicle=new Van;break;
                case 4:return 0;break;
                }//end menu switch
          string temp;
          //get the make
          cout <<"Enter Make of Vehicle:";
          cin >>temp;
          vehicle->setMake(temp);
          //get the model
          cout <<"Enter Model of Vehicle:";
          cin >>temp;
          vehicle->setModel(temp);
          //get the color
          cout <<"Enter Color(Red, Green, Blue, Yellow, or Black):"<<endl;
          cin >>temp;
          vehicle->setColor(temp);
          //get the type of interior
          cout <<"Enter the type of Interior(Leather, Vinyl, or Cloth):"<<endl;
          cin >>temp;
          vehicle->setInterior(temp);
          double temp2;
          //get the engine size
          while(vehicle->getEngineSize()<4.1){
                cout <<"Enter Engine size:(4.1-8.4)"<<endl;
                cin >>temp2;
                vehicle->setEngineSize(temp2);
                }//end engine loop
          //get the tire size
          while(vehicle->getTireSize()<17){
                cout <<"Enter the Tire size:(17-22)"<<endl;
                cin >>temp2;
                vehicle->setTireSize(temp2);
                }//end tire loop
          system("CLS");
          vehicle->print();
          system("PAUSE");
          system("CLS");
          cout <<"Another Vehicle?"<<endl
               <<"1.Yes"<<endl
               <<"2.No"<<endl;
          cin >>var2;
          delete vehicle;
          }//close program loop
    return EXIT_SUCCESS;
}
.
base-class: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
#include <iostream>
#include <string>

using namespace std;


class Vehicle{
private:
        string vColor;//color of the vehicle
        double engineSize;//size of the engine
        double tireSize;//size of the tires
        string interior;//vehicle interior type
public:
       //initialize variables
       Vehicle(){
          setColor("0");
          setEngineSize(0.0);
          setTireSize(0.0);
          setInterior("0");
          }
       //set the color of the vehicle
       void setColor(string color){
            vColor=color;
            }
       //get the color of the vehicle
       string getColor(){return vColor;}
       //set the size of the engine
       void setEngineSize(double size){
            engineSize=(size>4.0 && size<8.5)? size:0.0;
            }
       //get the size of the engine
       double getEngineSize(){return engineSize;}
       //set the size of the tires
       void setTireSize(double size){
            tireSize=(size>=17 && size<= 22)? size:0.0;
            }
       //get the size of the tires
       double getTireSize(){return tireSize;}
       //set the matieral of the interior
       void setInterior(string mat){
            interior=mat;
            }
       //get the matieral of the interior
       string getInterior(){return interior;}
       //rewritable print function
       virtual void print()=0;
};//end class 

derived-class:Car.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
#include <iostream>
#include <string>

using namespace std;


class Vehicle{
private:
        string vColor;//color of the vehicle
        double engineSize;//size of the engine
        double tireSize;//size of the tires
        string interior;//vehicle interior type
public:
       //initialize variables
       Vehicle(){
          setColor("0");
          setEngineSize(0.0);
          setTireSize(0.0);
          setInterior("0");
          }
       //set the color of the vehicle
       void setColor(string color){
            vColor=color;
            }
       //get the color of the vehicle
       string getColor(){return vColor;}
       //set the size of the engine
       void setEngineSize(double size){
            engineSize=(size>4.0 && size<8.5)? size:0.0;
            }
       //get the size of the engine
       double getEngineSize(){return engineSize;}
       //set the size of the tires
       void setTireSize(double size){
            tireSize=(size>=17 && size<= 22)? size:0.0;
            }
       //get the size of the tires
       double getTireSize(){return tireSize;}
       //set the matieral of the interior
       void setInterior(string mat){
            interior=mat;
            }
       //get the matieral of the interior
       string getInterior(){return interior;}
       //rewritable print function
       virtual void print()=0;
};//end class 

derived-class: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
#include <iostream>
#include <string>
#include <iomanip>

#include "Vehicle.h"

using namespace std;


class Truck:public Vehicle{
private:
        string make;//make of the truck
        string model;//model of the truck
public:
       //initialize variables
       Truck():Vehicle(){
          setMake("0");
          setModel("0");
          }
       //set the make of the truck
       void setMake(string input){
            make=input;
            }
       //get the make of the truck
       string getMake(){return make;}
       //set the model of the truck
       void setModel(string input){
            model=input;
            }
       //get the model of the truck
       string getModel(){return model;}
       //print out the truck object
       void print(){
            cout <<fixed<<setprecision(2);
            cout <<"Truck:"<<endl
                 <<"Make:     "<<getMake()<<endl
                 <<"Model:    "<<getModel()<<endl
                 <<"Engine:   "<<getEngineSize()<<"L"<<endl
                 <<"Tires:    "<<getTireSize()<<endl
                 <<"Interior: "<<getInterior()<<endl
                 <<"Color:    "<<getColor()<<endl;
            }
};//end truck class 

derived-class:Van.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
#include <iostream>
#include <string>
#include <iomanip>

#include "Vehicle.h"

using namespace std;


class Van:public Vehicle{
private:
        string make;//make of the van
        string model;//model of the van
public:
       //initialize variables
       Van():Vehicle(){
          setMake("0");
          setModel("0");
          }
       //set the make of the van
       void setMake(string input){
            make=input;
            }
       //get the make of the van
       string getMake(){return make;}
       //set the model of the van
       void setModel(string input){
            model=input;
            }
       //get the model of the van
       string getModel(){return model;}
       //print out the van object
       void print(){
            cout <<fixed<<setprecision(2);
            cout <<"Van:"<<endl
                 <<"Make:     "<<getMake()<<endl
                 <<"Model:    "<<getModel()<<endl
                 <<"Engine:   "<<getEngineSize()<<"L"<<endl
                 <<"Tires:    "<<getTireSize()<<endl
                 <<"Interior: "<<getInterior()<<endl
                 <<"Color:    "<<getColor()<<endl;
            }
};//end van class 
Apr 22, 2010 at 8:03pm
the errors i am getting are:
line-error message
5 from 13_14main.cpp In file included from Car.h:5, from 13_14main.cpp
7 redefinition of `class Vehicle'
7 previous definition of `class Vehicle'
6 from 13_14main.cpp In file included from Truck.h:5, from 13_14main.cpp
7 redefinition of `class Vehicle'
7 previous definition of `class Vehicle'
7 from 13_14main.cpp In file included from Van.h:5, from 13_14main.cpp
7 redefinition of `class Vehicle'
7 previous definition of `class Vehicle'
In function `int main(int, char**)':
34 'class Vehicle' has no member named 'setMake'
38 'class Vehicle' has no member named 'setModel'
[Build Error] [13_14main.o] Error 1
Apr 22, 2010 at 9:01pm
To start with - car.h and vehicle.h are the same - look at them.
Apr 23, 2010 at 5:21pm
sorry Car.h so supposed to be pretty much the same as Truck.h and Van.h
Apr 23, 2010 at 5:45pm
That is not the point. The point is that car.h and vehicle.h are the exact same thing, only with different names regarding their header files. Sorry, but compilers hate that. Can't they fix it on their own? :)

You also forgot to declare and define setMake() and setModel() in one of the header files that defines the class Vehicle.

-Albatross
Apr 23, 2010 at 5:52pm
You need to put in include guards.

See this: http://www.cplusplus.com/forum/articles/10627/ Specifically section 3
Apr 23, 2010 at 6:03pm
Can't they fix it on their own? :)
LOL :p

i also wonder about that from time to time. other languages seems to be able to fix simple things like this.
Apr 26, 2010 at 5:26pm
ok look i messed up with the copying and pasting on the car.h but i fixed the program on my own so it's all solved.
Topic archived. No new replies allowed.