problem with inheritance

Feb 27, 2011 at 3:25pm
Hi!

I have a base class called vehicle and two sub classes Car and Bike.
I have a handler file that has a couple of functions like addVehicle,showAll, showCars and showBikes.

Now to my problem, when the bikes are presented it looks like this.
Model: BMW Year: 2005 Cost: 2000
Model: Kawasaki Year: 2030 Cost: 40000
and so on....

At the end I want all costs to be added together and presented with a total cost.
So I've written something like this

1
2
3
4
5
6
7
for(int i=0;i<this->nrOfPresents;i++)
		{
			if(typeid(*this->prsnt[i])==typeid(Company))
			{
				this->prsnt[i]->present();
			}
		}


but when I try to access my function that returns the price for each bike it cant see it it only shows me the functions in the base class and not any function from the sub classes.
If I didn't use inheritance I would have done something like this

1
2
3
4
for(int i=0;i<this->nrOfPresents;i++)
   {    
        int totalCost+=this->prsnt[i]->getCost();
   }


How do I make this work, something tells me it should be easy?

Best Regards Ogward
Feb 27, 2011 at 3:31pm
Since all vehicles have a cost, getCost should be part of your parent class Vehicle, and not part of your child classes. Then this problem disappears.

Only things specific to cars and bikes should be in Car and Bike. Things that apply to all vehicles should be in Vehicle.
Feb 27, 2011 at 4:27pm
I know that, but only the bikes should have the cost.
if it was the way you said then it wouldnt be a problem...
the parent class vehicle has model and year but the bike class has cost as well and car has doors.

Feb 27, 2011 at 6:12pm
I know that, but only the bikes should have the cost.


So cars don't cost anything? Are you sure that's right?
Feb 27, 2011 at 6:20pm
I just made this up, the program is not about vehicles, its about something else.
But it should work as I stated above.

I'm positive it should be like that.
So how do I access the getCost() function that is in the child class?
Feb 27, 2011 at 6:31pm
In that event you have to downcast. But I'm almost certain that's not what you're supposed to do. Downcasting is actually quite rare and is a sign of a design flaw.

Perhaps if you post the actual classes and not these vehicle/car/etc classes I could be of more help.


If you want to go with the downcasting approach, you can do it like this (but again... this is generally a BAD IDEA and should be avoided):

1
2
3
4
5
6
7
8
for(int i=0; i < nrOfPresents; ++i)
{
    Bike* p = dynamic_cast<Bike*>(prsnt[i]);
    if(p)
        totalcost += p->getCost();
    else
        {}  // p will be null if prsnt[i] was not a Bike
}
Feb 27, 2011 at 6:55pm
ok, the actual program is like this.

parent class Present
child class Private
child class Company

So there are presents for the ppl in a company and for ppl that are not in the company, and they want to know the cost for the company gifts but not for the private gifts.

the thing you explained, does it have something to do with clone() or typeid?
Feb 28, 2011 at 5:57am
Well if it were me I wouldn't make a class hierarchy out of this -- but whatever.

Anyway yeah my solution above will work. I suppose it isn't so horrible after all.

the thing you explained, does it have something to do with clone() or typeid?


No. It's downcasting. I'm not using typeid or clone anywhere in there.
Feb 28, 2011 at 6:24am
Thanks a lot for the help!
Topic archived. No new replies allowed.