Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 [tutorial] Inheritance & Polymorphism
Index -> Programming, C++ -> C++ Tutorials
View previous topic Printable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Catalyst




PostPosted: Sun Jul 13, 2003 1:42 am   Post subject: [tutorial] Inheritance & Polymorphism

In inheritance and polymorphism are two major components of OOP

Ill start by covering inheritance (briefly)

Inheritance can be characterized by "is a". If classB inherits from classA, classB is a classA. This means that all of the functions and variables in classA are also part of classB. The new class adds its own unique functions and data while retaining the old info from classA. This avoids repition of code

example:
code:

class Animal
{
  public:
  int idnum;
  void Procreate(){}
};

class Dog:public Animal
{
   public:
   int dogtag;
   void Run(){}
};
class Bird:public Animal
{
   public:
   void Fly(){}
};


Polymorphism is closely related to inheritance. There different types of polymorphism. They include operator overloading, and function overloading. The type that deals with inheritance is very useful.

Polymorphism is when a function in a derived class overides a function in the base class. To do this the function should have the word virtual in front of it (it can be done without it but it is less proper). The virtual function from the base class is redefined without the virtual keyword in the derived classes.

for example
code:

class Animal
{
  public:
  int idnum;
  virtual void Move()
 { //generic move
 }
};

class Dog:public Animal
{
   public:
   int dogtag;
   void Move()
   {//the dog runs

    }
};
class Bird:public Animal
{
   public:
   void Move()
   {//the bird flys

    }
};


This may not seem very useful at the moment. But the virtual functions of the derived objects can be accesed using a pointer to the base class (only the common functions and data can be accessed)

to illustrate
code:

Animal *fido=new Dog;
Animal *tweet=new Bird;

fido->Move();
tweet->Move();


in some cases the virtual function need not be defined in the base class. In this case the class can be declared as abstract. This occurs when it has one or more abstract functions (alt: pure virtual functions)
an abstract function is declared as so

code:

virtual void mycode()=0;


This has been an intro to polymorphism and inheritance. If u gave anything to add just post.
Sponsor
Sponsor
Sponsor
sponsor
SilverSprite




PostPosted: Sun Jul 13, 2003 3:21 am   Post subject: (No subject)

how about subsubclasses? possible?
Tony




PostPosted: Sun Jul 13, 2003 3:29 am   Post subject: (No subject)

I'd assume so... Just inherit a class that inherits another class already Laughing

code:

class Animal
{
  public:
  int idnum;
  void Procreate(){}
};

class Dog:public Animal
{
   public:
   int dogtag;
   void Run(){}
};

class Poodle:public Dog
{
public:
char[30] name;
};


Poodle is a subclass of a Dog and still inherits properties of Animal (since dog is an animal)
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
Homer_simpson




PostPosted: Sun Jul 13, 2003 10:12 pm   Post subject: (No subject)

that's some sweet ass tutorials... Now i'm all clear with class and subclasses ... T Y
SilverSprite




PostPosted: Mon Jul 14, 2003 12:10 am   Post subject: (No subject)

yeah thx tony thats what i figured.. but you never know..
Display posts from previous:   
   Index -> Programming, C++ -> C++ Tutorials
View previous topic Tell A FriendPrintable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 5 Posts ]
Jump to:   


Style:  
Search: