Trying to create a copy constructor

I need help with a copy constructor. I'm not really understanding them. I have a base class called Vehicle and a derived class called Car.
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
class Vehicle
{
	private:
		int age;

	protected:
		float price;

	public:
		Vehicle(){age=0;price=0.0;}
		~Vehicle(){age=0;price=0.0;}
		void setAge( int x) {age=x;}
		void setPrice(float x){price=x;}
		int getAge(){return age;}
		float getPrice(){return price;}

};

class Car: public Vehicle
{
	public:
		bool RaceCarStatus;
		void setRaceCarStatus(bool y){RaceCarStatus = y;}
		bool  getRaceCarStatus(){return RaceCarStatus;}

		Car(){RaceCarStatus=false;}
		~Car(){RaceCarStatus=false;}
	/*Car(const Car & y){Vehicle = y.getRaceCarStatus;}*/

};

The part that is commented out is the copy constructor that I tried to create.

Here is the main function that test to make sure every attribute is returning the right parameters:
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
void main()
{
	Vehicle x;
	cout <<"Initial value for x: " << endl;
	cout <<"Age = " << x.getAge()<<"  "<< "Price = " << x.getPrice()<<endl;

	x.setAge(40);
	x.setPrice(20000);
	cout << "Modified value for x: " << endl;
	cout << "Age = "   << x.getAge()<<"  "<< "Price = " << x.getPrice()<< endl;

	

	Car y;
	cout <<"Initial value for y:"<<endl;
	cout <<"Age = " << y.getAge()<<"  "<<"Price = "<<y.getPrice()<<endl;
	y.setAge(50);
	y.setPrice(15000);
	y.setRaceCarStatus(true);
	cout << "Modified value for y:"<< endl;
	cout << "Age = " << y.getAge()<<"  "<< "Price = "<<y.getPrice()<<endl;
	cout << "Race car status?"<<y.getRaceCarStatus<<endl;

system ("pause");

}


I'm trying to test the new attributes and behavior of car but I think its not working because of the copy constructor. Can someone explain what is wrong here and also explain a copy constructor. Its just not clicking. Oh I also forgot that race car status is supposed to return yes or no, am I defining that right?
You're not understanding copy constructors for a good reason: with inheritance, they don't make sense. If you need to use inheritance and polymorphism, use the clone pattern instead.
Yeah I would look into that but this is the way my professor wants it done. So yeah I need someone that understand it with inheritance. It may be a crappy way but as of right now that's the way he wants it done.
The copy constructor is just a normal constructor that just so happens to take as its only parameter a reference to another object of the same type, preferably const. Which part specifically is getting you caught up? I will try to explain it more.

By the way, either that professor is trying to show that copy ctors and polymorphism don't mix, or he actually thinks they should go together (in which case he shouldn't be allowed anywhere near any code ever).
Last edited on
I guess the part I am getting caught up on is what exactly am I trying to copy. In the class Car? I only added one attribute which was Race car status. Am I passing a reference from the x to y? As you can see I am really confused.
The copy constructor is called when your object is being copied. Or rather, when an instance of your class is being created as a copy of an existing instance.

Think of it this way: You have a snow man with a bow tie and a fez, and you're starting with a fresh snow man. You need to copy the bow tie and the fez onto the fresh snowman so that both snow men are copies of each other - just remember you can't touch the original snow man.
Last edited on
I understand now! Thanks a lot.
Topic archived. No new replies allowed.