|
![]() |
Overclock.net - Overclocking.net > Software, Programming and Coding > Coding and Programming > Application Programming | |
C++ - Polymorphic method calls
|
||
![]() |
|
|
LinkBack | Thread Tools |
|
|
#1 (permalink) | ||||||||||||
|
PC Gamer
|
Hi there!
__________________![]() In my CS class, we've been getting taught how to use polymorphism. What im trying to do is get a different output based on the childclass. I have: 1 parent class: Car 3 child classes: Luxury, Sports, Offroad My regular function calls, where i call functions from the parent class are fine and work dandy. But, when it gets to getDescription, i really want it to override the parent class's function...and instead use the child class's function depending on what car type the object is. At the moment. It always outputs the parent class's version of getDescription() Car.h Code:
virtual string getDescription(); Code:
string Car::getDescription()
{
string output;
output.append("Car Type not found");
return output;
}
Code:
int getBHP(); string getDescription(); Code:
int Sports::getBHP()
{
return BrakeHorsePower;
}
string Sports::getDescription()
{
string output = "sports car class call";
//output.append("BHP: ");
//output.append(itos(getBHP())); <---Some help
here would be nice too:P Im trying to convert the output of
getBHP (which is int) to a string so it can be appended to
my string variable. Ive included <sstream> as needed. I think
im just using it wrong)
return output;
}
Code:
void Controller::listCars() //Function to loop through all objects in vector and print their values
{
system("cls");
vector <Car*>::iterator iter = theCars.begin();
for(iter = theCars.begin();iter != theCars.end();++iter)
{
cout << "==== Car " << (*iter)->getCarID() << " ====" << endl;
cout << "CarType: " << (*iter)->getCarType() << endl;
cout << "Manufacturer: " << (*iter)->getManufacturer() << endl;
cout << "Model: " << (*iter)->getModel() << endl;
cout << "EngineSize: " << (*iter)->getEngineSize() << endl;
cout << "CostPerDay: " << (*iter)->getCostPerDay() << endl;
cout << (*iter)->getDescription() << endl; <----I want this function to use the childclass variant depending on the CarType. Always comes out as the parent class's version though. (Which is just output "CarType not found")
cout << "===============" << endl << endl;
}
system("pause");
showMainMenu();
}
Last edited by Desmolas : 04-24-09 at 09:33 AM |
||||||||||||
|
|
|
|
|
#2 (permalink) | |||||||||||||
|
Price/Performance Fanboy
![]() |
*WOOOOOOOOOOSSSHHHHHHHHH*
That, was the sound of the entire post going over my head. Bump for ya to get some decent input tho.
__________________
|
|||||||||||||
|
|
|
|
#3 (permalink) | ||||||||||||
|
PC Gamer
|
Sorry
__________________ I wish i could explain what im thinking and what my problem is a bit better.
|
||||||||||||
|
|
|
|
|
#4 (permalink) | ||||||||||||||
|
Price/Performance Fanboy
![]() |
Quote:
![]() I'm hardware dude, not software, except for your basic troubleshooting and tweaking.
__________________
|
||||||||||||||
|
|
|
|
#5 (permalink) |
|
Case Modder
![]() |
Sports inherits from Car and the method signatures for getDescription() are the same, yes? And your creating various derived flavors of car and saving a pointer to the base class, yes? This worked for me:
Code:
class Car
{
public:
virtual string getDescription() { return "car"; }
};
class Sports : public Car
{
public:
string getDescription() { return "sports car"; }
};
int main(int argc, char **argv)
{
Car *car = new Car();
string desc = car->getDescription();
car = new Sports();
desc = car->getDescription();
}
__________________
Rich Custom Wooden Case Builder
Last edited by Spotswood : 04-25-09 at 09:43 AM |
|
|
|
|
|
#6 (permalink) | ||||||||||||
|
PC Gamer
|
Yes Spotswood thats exactly what i was trying to do. Thanks for the help again:P
__________________+rep and personal props from me.
Last edited by Desmolas : 04-28-09 at 05:57 AM |
||||||||||||
|
|
|
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
|
|