Welcome back! We're continuing our journey into object-oriented programming (OOP) with a new and exciting topic: Polymorphism. You've already learned about classes, objects, and inheritance, which are essential building blocks of OOP. Now, it's time to explore how polymorphism can make your code more flexible and reusable.
Polymorphism allows objects to be treated as instances of their parent class rather than their actual class. This provides a way to use a single interface to represent different types of objects. Here’s what we’ll cover in this lesson:
C++
programming.C++
: You'll learn how to use virtual functions and method overriding to achieve polymorphism.Polymorphism in C++
allows you to call derived class methods through a base class pointer or reference. This can make your code more dynamic and general. For example, consider the following code snippet:
C++1#include <iostream> 2#include <string> 3 4// Define a base class Person with name and age attributes 5class Person { 6public: 7 Person(const std::string& name, int age) : name(name), age(age) {} 8 9 // Define a virtual display method to signify, that it can be overridden by derived classes 10 virtual void display() { 11 std::cout << "Name: " << name << ", Age: " << age << std::endl; 12 } 13 14private: 15 std::string name; 16 int age; 17}; 18 19// Define a derived class Student with a major attribute 20class Student : public Person { 21public: 22 Student(const std::string& name, int age, const std::string& major) 23 : Person(name, age), major(major) {} 24 25 // Override the display method to include the major attribute. Note that the 'override' keyword is optional but recommended for clarity. 26 void display() override { 27 Person::display(); 28 std::cout << "Major: " << major << std::endl; 29 } 30 31private: 32 std::string major; 33}; 34 35// Define a derived class Teacher with a subject attribute 36class Teacher : public Person { 37public: 38 Teacher(const std::string& name, int age, const std::string& subject) 39 : Person(name, age), subject(subject) {} 40 41 // Override the display method to include the subject attribute 42 void display() override { 43 Person::display(); 44 std::cout << "Subject: " << subject << std::endl; 45 } 46 47private: 48 std::string subject; 49}; 50 51int main() { 52 // Create instances of Student and Teacher using base class pointers 53 Person* person1 = new Student("Alice", 30, "Computer Science"); 54 Person* person2 = new Teacher("Bob", 25, "Mathematics"); 55 56 // Call the display method on the base class pointers. The appropriate derived class method will be invoked. 57 person1->display(); 58 person2->display(); 59 60 // Free the allocated memory to avoid memory leaks 61 delete person1; 62 delete person2; 63 return 0; 64}
In this example, the display
method of the derived classes (Student
and Teacher
) overrides the base class method in Person
. When we call display
on a base class pointer (Person*
), the appropriate derived class method is invoked.
Note that we need to delete the allocated memory for the base class pointers to avoid memory leaks. This is a good practice when working with pointers in C++
.
Polymorphism is crucial because it introduces flexibility and scalability to your code:
Excited to dive into the practice section and apply polymorphism to your programs? Let's get started and see the power of this concept in action!