I have 2 classes. The base class has a virtual display function and member variable shipname and date. The display() function in the base class prints the ship name. The derived class has an override of display() which also prints the ship name and a member variable y.
void Ship::display(){
cout << shipName << endl << manufactureDate < <endl;
}
void CruiseShip::display(){
cout << shipName << maxNoOfPassengers << endl;
}
I have a loop that calls an object from base class and object from derived class but it outputs only one ship name instead of two
When I call display from derived class I the ship name is empty, but when I call it from base object it returns its value.
void main(){
Ship sh;
CruiseShip cruShip;
CargoShip cargShip;
sh.setName("Monster");
sh.setDate("11/11/2011");
cruShip.setNoOfPassengers(10);
cargShip.setCapacity(1000);
/*Ship *x[3] = {&sh, &cruShip, &cargShip};
for(int i=0;i<3;++i){
x[i]->display();
}*/
cout<<sh.getName()<<endl;
cout<<cruShip.getName()<<endl;
cout<<cargShip.getName();
system("pause");
}
Monster shows only from object sh
sh
. What names do you expect the others to have, and why? – Mike Seymour