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).
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.