[C++] Polymorphism – Overriding

Polymorphism is one of big 3 pillars of OOP (Encapsulation, Inheritance, and Polymorphism). Overriding works in a inheritance chain. Let’s find out how C++ supports overriding.

 

1. Pointers to Derived Types

Base class pointers can point to any object derived from that base class. But the opposite is not true. (A lion is an animal, but an animal is necessarily a lion) It is really important feature of OOP.

You can also use the “dynamic_cast<>()” operator to cast a pointer back and forth in the hierarchical tree. The restriction of the “dynamic_cast” is that the base class should have at least one virtual function.

class Animal
{
public:
  string name;
};

class Lion : public Animal
{ };

int _tmain(int argc, _TCHAR* argv[])
{
  Animal *a;
  Lion *l;

  Lion lk;
  lk.name = "Lion King";

  a = &lk; //possible
  // l = a; // error
  l = (Lion *)(a); // back to a lion

  return (EXIT_SUCCESS);
}

 

2. Virtual Functions and Overriding

A virtual function is a function that is declared with a “virtual” keyword in a base class and redefined in a derived class.

An interesting point of a virtual function is that when it is invoked by a bass class pointer that points to the derived object, the derived version of a function will be called. The determination of which version of a function (base or derived) is called is made at runtime, not at compile time.

class Animal
{
public:
  virtual void Cry();
  void ShowMe(); // not virtual
};

class Lion : public Animal
{
public:
  void Cry(); // virtual keyword is not required
  void ShowMe(); // shadowing(hiding) the base function
};

void Animal::Cry()
{
  cout << "Can't cry!" << endl;
}
void Lion::Cry()
{
  cout << "Roaring..." << endl;
}

void Animal::ShowMe()
{
  cout << "I am an animal!" << endl;
}
void Lion::ShowMe()
{
  cout << "I am a Lion!" << endl;
}

int _tmain(int argc, _TCHAR* argv[])
{
  Lion *l = new Lion();
  Animal *a = l;
  a->Cry(); // "Roaring..."
  a->ShowMe(); // "animal"
  l->ShowMe(); // "lion"
  l->Animal::ShowMe(); // "animal"
  return (EXIT_SUCCESS);
}

 

3. Pure Virtual Functions and Abstract Classes

Some times you cannot provide any meaningful implementation to a virtual function in a base class. In this case, you just want to create an empty function in a base class and force the derived classes to implement the function.

The “pure virtual function” does not have a function body. To declare a function as a pure virtual, assign 0 to the function. A class that contains at least one virtual function is called an “abstract class“.

class Animal
{
public:
  virtual void Cry() = 0;
};

class Lion : public Animal
{
public:
  void Cry();
};

void Lion::Cry()
{
  cout << "Roaring..." << endl;
}

int _tmain(int argc, _TCHAR* argv[])
{
  Lion *l = new Lion();
  Animal *a = l;

  a->Cry(); // "Roaring..."
  // l->Animal::Cry(); // error

  return (EXIT_SUCCESS);
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s