C++ Polymorphism Introduction šŸŽÆ

beginner
25 min

C++ Polymorphism Introduction šŸŽÆ

Welcome to the fascinating world of C++ Polymorphism! This lesson is designed to help you understand one of the most powerful features of C++ that allows objects of different classes to be treated as objects of a common superclass. Let's dive in!

What is Polymorphism? šŸ“

Polymorphism is a principle that allows one interface to be used for objects of different types. It comes from the Greek words 'poly' (many) and 'morph' (forms). In C++, we have two types of polymorphism:

  1. Compile-time Polymorphism (Static Polymorphism): This is achieved using functions overloading and operators overloading.

  2. Runtime Polymorphism (Dynamic Polymorphism): This is achieved using base class pointers and references to derived class objects.

Understanding Compile-time Polymorphism šŸ’”

Let's start with a simple example of compile-time polymorphism.

cpp
#include<iostream> using namespace std; void print(int num) { cout << "Number: " << num << endl; } void print(char ch) { cout << "Character: " << ch << endl; } int main() { int number = 10; char character = 'A'; print(number); // Compile-time polymorphism in action! print(character); return 0; }

In the above code, we have two functions with the same name print but different parameters. During compile-time, the compiler decides which function to call based on the arguments passed.

Moving on to Runtime Polymorphism šŸ’”

Runtime polymorphism is a bit more complex but extremely powerful. It allows us to perform operations on objects of a superclass, even if they are actually instances of a subclass.

cpp
#include<iostream> using namespace std; class Animal { public: virtual void sound() { cout << "Animal sound" << endl; } }; class Dog : public Animal { public: void sound() { cout << "Bark!" << endl; } }; class Cat : public Animal { public: void sound() { cout << "Meow!" << endl; } }; int main() { Dog myDog; Cat myCat; Animal *myPet = &myDog; // Here, myPet is a base class pointer pointing to a derived class object. myPet->sound(); // This will call the sound method of the derived class Dog. myPet = &myCat; // Now, myPet is pointing to Cat object. myPet->sound(); // This will call the sound method of the derived class Cat. return 0; }

In the above code, we have a base class Animal and two derived classes Dog and Cat. The sound method is declared in the base class as a virtual function. This allows the derived classes to override the method with their specific implementations.

In the main function, we have a base class pointer myPet that can point to either a Dog or Cat object. When we call the sound method on myPet, it calls the appropriate method based on the object it is currently pointing to.

Quiz Time šŸŽÆ

Quick Quiz
Question 1 of 1

What is Polymorphism in C++?

Quick Quiz
Question 1 of 1

Which of the following is an example of Compile-time Polymorphism?

Quick Quiz
Question 1 of 1

What does the keyword 'virtual' do in C++?

Stay tuned for more on C++ Polymorphism! In the next lesson, we'll dive deeper into runtime polymorphism and explore more practical examples. Happy coding! šŸ’”