Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jun 5th, 2006, 12:12 AM   #1
Twilight
Programmer
 
Join Date: Apr 2006
Location: Calgary, Alberta
Posts: 67
Rep Power: 3 Twilight is on a distinguished road
Learning New OOP Concepts

I've done a fair share of programming over the last couple of years, but I was wondering if anyone had any exercises dealing with Inheritance or Polymorphism to help me learn the concepts. Old Uni assignments anyone?

Barring that, just where I can go to learn this kinda stuff would be cool.
Twilight is offline   Reply With Quote
Old Jun 5th, 2006, 2:23 AM   #2
Soulstorm
Hobbyist Programmer
 
Soulstorm's Avatar
 
Join Date: Jan 2006
Location: Menidi, Athens, Greece
Posts: 239
Rep Power: 3 Soulstorm is on a distinguished road
for your second question, I think you should check this out: http://www.cplusplus.com/doc/tutorial/

apart from that, I think you need a book on C++ to help you with the exercises... There is a free electronic book that will teach you the concepts of C++ polymorphism and Inheritance. It's called Thinking in C++ and it's free.
__________________
Project::Soulstorm (personal homepage)
Soulstorm is offline   Reply With Quote
Old Jun 6th, 2006, 10:24 AM   #3
rsnd
Hobbyist Programmer
 
rsnd's Avatar
 
Join Date: Jun 2005
Location: Helltown
Posts: 162
Rep Power: 4 rsnd is on a distinguished road
Even thou things are done a bit differently, Java is really good for learning OOP concepts. The ideas are the same (almost). There should be plenty of c++ classes tutorials around. I really liked the "wrox press c++ something" ebook. I thaught its really good for beginners. Came with my vc++ 6.0 compiler package.

If you are just looking for a challenge or "assignment", make a html parser. Should be good enough exercize for oo stuff.
__________________
Spread your wings and fly! Chicken!

Last edited by rsnd; Jun 6th, 2006 at 10:44 AM.
rsnd is offline   Reply With Quote
Old Jun 6th, 2006, 2:24 PM   #4
Harakim
Hobbyist Programmer
 
Join Date: May 2006
Location: West Jordan, Utah, United States
Posts: 176
Rep Power: 3 Harakim is on a distinguished road
We had to make a farm with different animals, where animals was inherited from the animal class. It was actually quite entertaining.

Basically, you would be able to cycle through the animals. You could choose from three buttons: Speak, Feed, Play Old McDonald.

It was kind of like GigaPet, because you had to feed them or else they would die and then when you played the old McDonald song you wouldn't hear them moo/crow/etc.

Anyway, I thought that project was pretty fun because my group took some liberties with it.
Harakim is offline   Reply With Quote
Old Jun 6th, 2006, 5:09 PM   #5
bl00dninja
Programming Guru
 
bl00dninja's Avatar
 
Join Date: Oct 2004
Location: namespace std
Posts: 1,246
Rep Power: 5 bl00dninja is on a distinguished road
i don't remember exactly what the hell this really does or is, but i think it was a simple test program for mult inheritance and polymorphism.


#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;

class abstractBaseClass
{
public:
	abstractBaseClass()					{cout<<"abstractBaseClass constructor called"<<endl;}
	virtual ~abstractBaseClass()		{cout<<"abstractBaseClass destructor called"<<endl;}

	virtual void printObjectData() = 0;
};
//////////////////////////////////////////////////////////////////////////////////////////
class secondaryParent : virtual public abstractBaseClass
{
protected:
	int P;
public:
	secondaryParent(int pInput = 0)			{P = pInput; cout<<"secondaryParent constructor called"<<endl;}
	virtual ~secondaryParent()				{cout<<"secondaryParent destructor called"<<endl;}
	int getP()					{return P;}
};

class tertiaryParent : virtual public abstractBaseClass
{
protected:
	int Q;
public:
	tertiaryParent(int qInput = 0)			{Q = qInput; cout<<"tertiaryParent constructor called"<<endl;}
	virtual ~tertiaryParent()				{cout<<"tertiaryParent destructor called"<<endl;}
	int getQ()					{return Q;}
};
//////////////////////////////////////////////////////////////////////////////////////////
class child1 : public secondaryParent, public tertiaryParent
{
public:
	child1()							 {cout<<"child1 constructor called"<<endl; P = secondaryParent::P; Q = tertiaryParent::Q;}
	virtual ~child1()					 {cout<<"child1 destructor called"<<endl;}
	virtual void printObjectData()		 {cout<<"child1 "<<getP()<<" "<<getQ()<<endl;}
};

class child2 : public secondaryParent, public tertiaryParent
{
public:
	child2()							 {cout<<"child2 constructor called"<<endl; P = secondaryParent::P; Q = tertiaryParent::Q;}
	virtual ~child2()					 {cout<<"child2 destructor called"<<endl;}
	virtual void printObjectData()		 {cout<<"child2 "<<getP()<<" "<<getQ()<<endl;}
};

class child3 : public secondaryParent, public tertiaryParent
{
public:
	child3()							 {cout<<"child3 constructor called"<<endl; P = secondaryParent::P; Q = tertiaryParent::Q;}
	virtual ~child3()					 {cout<<"child3 destructor called"<<endl;}
	virtual void printObjectData()		 {cout<<"child3 "<<getP()<<" "<<getQ()<<endl;}
};
///////////////////////////////////////////////////////////////////////////////////////
int main()
{
	srand(time(0));//seed rand
	int randChoice, P, Q;
	
	P = ((rand()%100 + 1));
	Q = ((rand()%1000 + 1));

	abstractBaseClass* x;//pointer to base class

	randChoice = rand()%3 + 1;//pick which object to instantiate

	switch (randChoice)
	{
	case 1:  
		x = new child1();
		break;

	case 2:
		x = new child2();
		break;

	case 3:
		x = new child3();
		break;
	}//end switch

	x->printObjectData();

	delete x;//delete base class pointer

	return 0;
}//end main
__________________
i put on my robe and wizard hat...

Have you ever heard of Plato, Aristotle, Socrates?...Morons.
bl00dninja is offline   Reply With Quote
Old Jun 6th, 2006, 9:14 PM   #6
Twilight
Programmer
 
Join Date: Apr 2006
Location: Calgary, Alberta
Posts: 67
Rep Power: 3 Twilight is on a distinguished road
Thanks for the code example, right now I don't really understand whats happening, but I do have "C++ From the Beginning" by Jan Skansholm from my first year programming course, which has 3 chapters on this kinda stuff, so I should be able to pick up the theory there. I just need code problems to practice it on.
Twilight is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

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

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 6:18 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC