Overclock.net - Overclocking.net
     
 
Home Gallery Reviews Blogs Register Today's Posts Mark Forums Read Members List


Go Back   Overclock.net - Overclocking.net > Software, Programming and Coding > Coding and Programming > Application Programming

Reply
 
LinkBack Thread Tools
Old 04-24-09   #1 (permalink)
PC Gamer
 
Desmolas's Avatar
 
intel ati

Join Date: Nov 2008
Posts: 11

Rep: 0 Desmolas Unknown
Unique Rep: 0
Trader Rating: 0
Default C++ - Polymorphic method calls

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();
Car.cpp
Code:
string Car::getDescription()
{
	string output;
	output.append("Car Type not found");
	return output;
}
Sports.h
Code:
	int getBHP();
	string getDescription();
Sports.cpp
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;
}
Controller.cpp
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();

}
__________________
System: Neil Freakin' Peart
CPU
i7 920 2.66Ghz
Motherboard
Asus P6T Intel X56
Memory
6GB OCZ DDR3 1600MHz
Graphics Card
HD 4870 1GB
Hard Drive
1TB Hitachi Deskstar
Power Supply
Corsair TX 850w
Case
Coolermaster RC-1000
CPU cooling
Coolermaster Hyper Z600
GPU cooling
<stock>
OS
Vista 64bit
Monitor
Samsung 215tw 21"

Last edited by Desmolas : 04-24-09 at 09:33 AM
Desmolas is offline   Reply With Quote
Old 04-24-09   #2 (permalink)
Price/Performance Fanboy
 
DesertRat's Avatar
 
amd ati

Join Date: Mar 2006
Location: Southern Kommiefornia
Posts: 6,077

Rep: 401 DesertRat is a proven memberDesertRat is a proven memberDesertRat is a proven memberDesertRat is a proven memberDesertRat is a proven member
Unique Rep: 287
Hardware Reviews: 3
Trader Rating: 28
Default

*WOOOOOOOOOOSSSHHHHHHHHH*

That, was the sound of the entire post going over my head.

Bump for ya to get some decent input tho.
__________________
Quote:
Originally Posted by Knitelife View Post
Best way to explain my thinking:
With enough money, anyone can win a race.
I'm the guy that wants to prove you can win the Daytona 500 with a jet engine shoehorned into a Ford Pinto driven by a chimpanzee.

System: Puff the Magic Dragon(Spooky)
CPU
AMD X4 20(unlocked 720)@3.62GHz(250x14.5@1.5v)
Motherboard
Biostar TA790GX3 A2+
Memory
2x2GB G.Skill DDR2 800 cl4
Graphics Card
XFX 4890XT
Hard Drive
WD 250GB SATA
Sound Card
Creative X-Fi XtremeMusic
Power Supply
Tt Toughpower 750W modular
Case
Gigabyte 3D Aurora
CPU cooling
Scythe Mugen2 + Ultra Kaze 3000RPM
GPU cooling
Scythe Musashi + Mod. RHS90 + 2x 12cm Grn LED fans
OS
Windows Vista X64 Home Premium
Monitor
Sceptre 42" 1080p
DesertRat is online now Overclocked Account   Reply With Quote
Old 04-24-09   #3 (permalink)
PC Gamer
 
Desmolas's Avatar
 
intel ati

Join Date: Nov 2008
Posts: 11

Rep: 0 Desmolas Unknown
Unique Rep: 0
Trader Rating: 0
Default

Sorry I wish i could explain what im thinking and what my problem is a bit better.
__________________
System: Neil Freakin' Peart
CPU
i7 920 2.66Ghz
Motherboard
Asus P6T Intel X56
Memory
6GB OCZ DDR3 1600MHz
Graphics Card
HD 4870 1GB
Hard Drive
1TB Hitachi Deskstar
Power Supply
Corsair TX 850w
Case
Coolermaster RC-1000
CPU cooling
Coolermaster Hyper Z600
GPU cooling
<stock>
OS
Vista 64bit
Monitor
Samsung 215tw 21"
Desmolas is offline   Reply With Quote
Old 04-24-09   #4 (permalink)
Price/Performance Fanboy
 
DesertRat's Avatar
 
amd ati

Join Date: Mar 2006
Location: Southern Kommiefornia
Posts: 6,077

Rep: 401 DesertRat is a proven memberDesertRat is a proven memberDesertRat is a proven memberDesertRat is a proven memberDesertRat is a proven member
Unique Rep: 287
Hardware Reviews: 3
Trader Rating: 28
Default

Quote:
Originally Posted by Desmolas View Post
Sorry I wish i could explain what im thinking and what my problem is a bit better.
Don't feel at all bad. I'm seriously software retarded when it comes to coding etc.

I'm hardware dude, not software, except for your basic troubleshooting and tweaking.
__________________
Quote:
Originally Posted by Knitelife View Post
Best way to explain my thinking:
With enough money, anyone can win a race.
I'm the guy that wants to prove you can win the Daytona 500 with a jet engine shoehorned into a Ford Pinto driven by a chimpanzee.

System: Puff the Magic Dragon(Spooky)
CPU
AMD X4 20(unlocked 720)@3.62GHz(250x14.5@1.5v)
Motherboard
Biostar TA790GX3 A2+
Memory
2x2GB G.Skill DDR2 800 cl4
Graphics Card
XFX 4890XT
Hard Drive
WD 250GB SATA
Sound Card
Creative X-Fi XtremeMusic
Power Supply
Tt Toughpower 750W modular
Case
Gigabyte 3D Aurora
CPU cooling
Scythe Mugen2 + Ultra Kaze 3000RPM
GPU cooling
Scythe Musashi + Mod. RHS90 + 2x 12cm Grn LED fans
OS
Windows Vista X64 Home Premium
Monitor
Sceptre 42" 1080p
DesertRat is online now Overclocked Account   Reply With Quote
Old 04-25-09   #5 (permalink)
Case Modder
 
Spotswood's Avatar
 
Join Date: Jul 2008
Location: New Hampshire, USA
Posts: 236

Rep: 46 Spotswood is acknowledged by some
Unique Rep: 39
Trader Rating: 0
Default

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
Overclock.net Mod of the Month

Last edited by Spotswood : 04-25-09 at 09:43 AM
Spotswood is offline   Reply With Quote
Old 04-28-09   #6 (permalink)
PC Gamer
 
Desmolas's Avatar
 
intel ati

Join Date: Nov 2008
Posts: 11

Rep: 0 Desmolas Unknown
Unique Rep: 0
Trader Rating: 0
Default

Yes Spotswood thats exactly what i was trying to do. Thanks for the help again:P

+rep and personal props from me.
__________________
System: Neil Freakin' Peart
CPU
i7 920 2.66Ghz
Motherboard
Asus P6T Intel X56
Memory
6GB OCZ DDR3 1600MHz
Graphics Card
HD 4870 1GB
Hard Drive
1TB Hitachi Deskstar
Power Supply
Corsair TX 850w
Case
Coolermaster RC-1000
CPU cooling
Coolermaster Hyper Z600
GPU cooling
<stock>
OS
Vista 64bit
Monitor
Samsung 215tw 21"

Last edited by Desmolas : 04-28-09 at 05:57 AM
Desmolas is offline   Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools



All times are GMT -5. The time now is 01:04 PM.


Overclock.net is a Carbon Neutral Site Creative Commons License

Terms of Service / Forum Rules | Privacy Policy | DMCA Info | Advertising | Become an Official Vendor
Copyright © 2009 Shogun Interactive Development. Most rights reserved.
Page generated in 0.12824 seconds with 8 queries