Inheritance and Polymorphism
COS2614 - Programming: Contemporary Concepts · Introduction to C++ and Qt 4
Inheritance and Polymorphism
Inheritance and polymorphism are two fundamental concepts in object-oriented programming (OOP) that allow you to create more flexible and reusable code. Inheritance enables a new class (the derived class) to inherit properties and methods from an existing class (the base class). Polymorphism allows methods to do different things based on the object that it is acting upon. Understanding these concepts is essential for writing effective C++ programs.
Key idea: After studying this topic, you should be able to:
- Define inheritance and polymorphism in C++.
- Implement inheritance in C++ using base and derived classes.
- Use polymorphism through virtual functions and method overriding.
- Understand the importance of constructors and destructors in inheritance.
What is Inheritance?
Inheritance is a mechanism in C++ that allows a class to inherit properties and methods from another class. The class that is inherited from is called the base class, while the class that inherits is called the derived class. This relationship allows for code reuse and can simplify the code structure.
Basic Inheritance Example
Consider a base class called Animal and a derived class called Dog.
class Animal { public: void speak() { cout << "Animal speaks" << endl; } }; class Dog : public Animal { public: void speak() { cout << "Dog barks" << endl; } }; In this example, Dog inherits from Animal. The Dog class can override the speak method to provide its own implementation.
Using Inheritance
You can create objects of the derived class and call their methods:
int main() { Dog dog; dog.speak(); // Outputs: Dog barks Animal* animal = &dog; animal->speak(); // Outputs: Dog barks because of polymorphism return 0; }What is Polymorphism?
Polymorphism allows methods to be used interchangeably with different types of objects. In C++, polymorphism is typically achieved through the use of virtual functions.
Virtual Functions
A virtual function is a member function in the base class that you expect to override in derived classes. To declare a virtual function, you use the virtual keyword in the base class. This allows the program to determine which function to call at runtime based on the type of the object being referenced.
Example of Polymorphism
Continuing with our Animal example, we can make the speak method virtual:
class Animal { public: virtual void speak() { cout << "Animal speaks" << endl; } }; class Dog : public Animal { public: void speak() override { cout << "Dog barks" << endl; } }; class Cat : public Animal { public: void speak() override { cout << "Cat meows" << endl; } }; Now, if we create an array of Animal pointers, we can store different derived class objects and call the speak method:
int main() { Animal* animals[2]; animals[0] = new Dog(); animals[1] = new Cat(); for (int i = 0; i < 2; i++) { animals[i]->speak(); } return 0; }This will output:
Dog barks
Cat meows
Constructors and Destructors in Inheritance
When using inheritance, constructors and destructors play a critical role. The constructor of the base class is called first, followed by the constructor of the derived class. Conversely, the destructor of the derived class is called first, followed by the destructor of the base class.
Example of Constructors and Destructors
class Animal { public: Animal() { cout << "Animal created" << endl; } virtual ~Animal() { cout << "Animal destroyed" << endl; } }; class Dog : public Animal { public: Dog() { cout << "Dog created" << endl; } ~Dog() { cout << "Dog destroyed" << endl; } }; In this example, when a Dog object is created, the output will be:
Animal created
Dog created
When the object is destroyed, the output will be:
Dog destroyed
Animal destroyed
Common Mistakes
Watch out: A common mistake is to forget to declare a base class method as virtual. This will lead to the base class method being called instead of the derived class method, resulting in unexpected behavior.
Summary
- Inheritance allows a derived class to inherit properties and methods from a base class.
- Polymorphism allows methods to be called on objects of different types through a common interface.
- Virtual functions are essential for achieving polymorphism in C++.
- Constructors and destructors are called in a specific order when dealing with inheritance.
Check your understanding
- What is the purpose of inheritance in C++?
- How does polymorphism work in C++?
- What is the role of the virtual keyword?
- Explain the order in which constructors and destructors are called in inheritance.