![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Programmer
Join Date: Apr 2006
Location: Calgary, Alberta
Posts: 67
Rep Power: 3
![]() |
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. |
|
|
|
|
|
#2 |
|
Hobbyist Programmer
Join Date: Jan 2006
Location: Menidi, Athens, Greece
Posts: 243
Rep Power: 3
![]() |
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) |
|
|
|
|
|
#3 |
|
Hobbyist Programmer
Join Date: Jun 2005
Location: Helltown
Posts: 162
Rep Power: 4
![]() |
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 11:44 AM. |
|
|
|
|
|
#4 |
|
Hobbyist Programmer
Join Date: May 2006
Location: West Jordan, Utah, United States
Posts: 176
Rep Power: 3
![]() |
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. |
|
|
|
|
|
#5 |
|
Programming Guru
![]() Join Date: Oct 2004
Location: namespace std
Posts: 1,246
Rep Power: 6
![]() |
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. |
|
|
|
|
|
#6 |
|
Programmer
Join Date: Apr 2006
Location: Calgary, Alberta
Posts: 67
Rep Power: 3
![]() |
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.
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|