-->

Advanced Concepts in Inheritance and Polymorphism

 

Inheritance and polymorphism are two advanced concepts in C++ that allow for the creation of reusable and flexible code.

Inheritance is a mechanism in C++ that allows a new class to be derived from an existing class. The derived class inherits all the member variables and member functions of the existing class, and it can add new member variables and member functions or modify the existing ones. This allows for the creation of a hierarchy of classes where the base class provides common functionality and the derived classes can specialize or extend it.

Here is an example of inheritance in C++:


#include <iostream> class Shape { public: int width, height; void printArea() { std::cout << "The area of the shape is: " << width * height << std::endl; } }; class Rectangle : public Shape {}; class Triangle : public Shape { public: void printArea() { std::cout << "The area of the triangle is: " << (width * height) / 2 << std::endl; } }; int main() { Rectangle rect; rect.width = 5; rect.height = 10; rect.printArea(); Triangle tri; tri.width = 5; tri.height = 10; tri.printArea(); return 0; }

In this example, the class "Shape" is the base class and it has two member variables "width" and "height", and a member function "printArea()". The classes "Rectangle" and "Triangle" are derived from the class "Shape". The class "Rectangle" doesn't have any additional member variables or functions, so it inherits all the member variables and functions of the base class. The class "Triangle" has a member function "printArea()" that overrides the same function in the base class.

In the main function, two objects are created, one of the class "Rectangle" and one of the class "Triangle". The objects are assigned values for their member variables, and their "printArea()" function is called. When the "printArea()" function of the "Rectangle" object is called, it uses the implementation from the base class, which calculates the area as "width * height". When the "printArea()" function of the "Triangle" object is called, it uses the implementation from the derived class, which calculates the area as "(width * height) / 2".

Polymorphism is a mechanism in C++ that allows objects of different classes to be treated as objects of the same class. This is achieved by using a common interface that is shared by all the classes. The objects can then be referred to using pointers or references of the common interface type. This allows for the creation of generic and flexible code that can work with objects of different types without knowing their exact class.


You may like these posts

Latest Posts

About Sure Mag

Join with us

Advanced Concepts in Inheritance and Polymorphism

 

Inheritance and polymorphism are two advanced concepts in C++ that allow for the creation of reusable and flexible code.

Inheritance is a mechanism in C++ that allows a new class to be derived from an existing class. The derived class inherits all the member variables and member functions of the existing class, and it can add new member variables and member functions or modify the existing ones. This allows for the creation of a hierarchy of classes where the base class provides common functionality and the derived classes can specialize or extend it.

Here is an example of inheritance in C++:


#include <iostream> class Shape { public: int width, height; void printArea() { std::cout << "The area of the shape is: " << width * height << std::endl; } }; class Rectangle : public Shape {}; class Triangle : public Shape { public: void printArea() { std::cout << "The area of the triangle is: " << (width * height) / 2 << std::endl; } }; int main() { Rectangle rect; rect.width = 5; rect.height = 10; rect.printArea(); Triangle tri; tri.width = 5; tri.height = 10; tri.printArea(); return 0; }

In this example, the class "Shape" is the base class and it has two member variables "width" and "height", and a member function "printArea()". The classes "Rectangle" and "Triangle" are derived from the class "Shape". The class "Rectangle" doesn't have any additional member variables or functions, so it inherits all the member variables and functions of the base class. The class "Triangle" has a member function "printArea()" that overrides the same function in the base class.

In the main function, two objects are created, one of the class "Rectangle" and one of the class "Triangle". The objects are assigned values for their member variables, and their "printArea()" function is called. When the "printArea()" function of the "Rectangle" object is called, it uses the implementation from the base class, which calculates the area as "width * height". When the "printArea()" function of the "Triangle" object is called, it uses the implementation from the derived class, which calculates the area as "(width * height) / 2".

Polymorphism is a mechanism in C++ that allows objects of different classes to be treated as objects of the same class. This is achieved by using a common interface that is shared by all the classes. The objects can then be referred to using pointers or references of the common interface type. This allows for the creation of generic and flexible code that can work with objects of different types without knowing their exact class.



SHARE THIS

Author:

Etiam at libero iaculis, mollis justo non, blandit augue. Vestibulum sit amet sodales est, a lacinia ex. Suspendisse vel enim sagittis, volutpat sem eget, condimentum sem.

Most Popular