-->

Advanced Topics in Inheritance and Polymorphism

 Inheritance is a powerful feature in C++ that allows you to create a new class based on an existing class. The new class inherits all the member variables and member functions of the existing class, and can also add new member variables and member functions or override existing ones. This makes it easy to reuse code and build complex relationships between classes.

For example, consider the following code:


#include <iostream> class Shape { public: int x, y; void move(int dx, int dy) { x += dx; y += dy; } }; class Circle : public Shape { public: int radius; double area() { return 3.14 * radius * radius; } }; int main() { Circle c; c.x = 10; c.y = 20; c.radius = 5; std::cout << "Circle area: " << c.area() << std::endl; c.move(1, 2); std::cout << "Circle position: 
(" << c.x << ", " << c.y << ")" << std::endl; return 0; }

This program defines a base class "Shape" with two member variables "x" and "y" and a member function "move()". The next line defines a derived class "Circle" that inherits from the base class "Shape" using the keyword "public". The derived class adds a new member variable "radius" and a member function "area()".

In the main function, an object of the class "Circle" is created with the line "Circle c;". The object "c" has access to all the member variables and member functions of both the base class and the derived class.

The next few lines assign values to the member variables of the object "c", and the line "std::cout << "Circle area: " << c.area() << std::endl;" calls the member function "area()" of the object "c". This causes the text "Circle area: 78.5" to be printed to the console, followed by a new line.

The line "c.move(1, 2);" calls the member function "move()" of the object "c", which updates the values of the member variables "x" and "y". The next line "std::cout << "Circle position: (" << c.x << ", " << c.y << ")" << std::endl;" prints the new position of the circle to the console.

Finally, the line "return 0;" is used to exit the main function and the program, indicating that it has completed successfully.

Polymorphism is another powerful feature in C++ that allows you to use objects of different classes interchangeably as long as they share a common interface. This can be achieved using virtual functions and pointers to objects of the base class.

For example, consider the following code:

#include <iostream> class Shape { public: virtual double area() = 0; }; class Circle : public Shape { public: int radius; double area() { return 3.14 * radius * radius; } }; class Rectangle : public Shape { public: int width, height; double

 

area() { return width * height; } };

int main()

{ Shape *shapes[2]; 

shapes[0] = new Circle;

 shapes[1] = new Rectangle;

 shapes[0]->radius = 5; shapes[1]->width = 3; 

shapes[1]->height = 4;

 for (int i = 0; i < 2; i++) { 

std::cout << "Shape " << i << " area: " << shapes[i]->area() 

<< std::endl; } return 0; }


This program defines a base class "Shape" with a pure virtual function "area()"
meaning that any class that inherits from it must implement its own version of the "area()" 
function. The next two lines define two derived classes "Circle" and "Rectangle" that both 
inherit from the base class "Shape" and implement their own versions of the "area()" function. In the main function, an array of pointers to objects of the base class "Shape" is declared
 with the line "Shape *shapes[2];". The next two lines initialize two elements of the array  
with pointers to objects of the derived classes "Circle" and "Rectangle", respectively. The next two lines assign values to the member variables of the objects pointed to by the 
elements of the array, and the following for loop iterates over the elements of the array 
and calls the "area()" function of each object, which returns the correct area of the object 
regardless of its actual class. The program then exits with the line "return 0;", indicating that it has completed successfully. This is just a brief overview of inheritance and polymorphism in C++. There are many more
 advanced concepts to learn, such as virtual inheritance, abstract classes, and templates, 
but understanding the basics of inheritance and polymorphism is essential for any C++ programmer.

 

You may like these posts

Latest Posts

About Sure Mag

Join with us

Advanced Topics in Inheritance and Polymorphism

 Inheritance is a powerful feature in C++ that allows you to create a new class based on an existing class. The new class inherits all the member variables and member functions of the existing class, and can also add new member variables and member functions or override existing ones. This makes it easy to reuse code and build complex relationships between classes.

For example, consider the following code:


#include <iostream> class Shape { public: int x, y; void move(int dx, int dy) { x += dx; y += dy; } }; class Circle : public Shape { public: int radius; double area() { return 3.14 * radius * radius; } }; int main() { Circle c; c.x = 10; c.y = 20; c.radius = 5; std::cout << "Circle area: " << c.area() << std::endl; c.move(1, 2); std::cout << "Circle position: 
(" << c.x << ", " << c.y << ")" << std::endl; return 0; }

This program defines a base class "Shape" with two member variables "x" and "y" and a member function "move()". The next line defines a derived class "Circle" that inherits from the base class "Shape" using the keyword "public". The derived class adds a new member variable "radius" and a member function "area()".

In the main function, an object of the class "Circle" is created with the line "Circle c;". The object "c" has access to all the member variables and member functions of both the base class and the derived class.

The next few lines assign values to the member variables of the object "c", and the line "std::cout << "Circle area: " << c.area() << std::endl;" calls the member function "area()" of the object "c". This causes the text "Circle area: 78.5" to be printed to the console, followed by a new line.

The line "c.move(1, 2);" calls the member function "move()" of the object "c", which updates the values of the member variables "x" and "y". The next line "std::cout << "Circle position: (" << c.x << ", " << c.y << ")" << std::endl;" prints the new position of the circle to the console.

Finally, the line "return 0;" is used to exit the main function and the program, indicating that it has completed successfully.

Polymorphism is another powerful feature in C++ that allows you to use objects of different classes interchangeably as long as they share a common interface. This can be achieved using virtual functions and pointers to objects of the base class.

For example, consider the following code:

#include <iostream> class Shape { public: virtual double area() = 0; }; class Circle : public Shape { public: int radius; double area() { return 3.14 * radius * radius; } }; class Rectangle : public Shape { public: int width, height; double

 

area() { return width * height; } };

int main()

{ Shape *shapes[2]; 

shapes[0] = new Circle;

 shapes[1] = new Rectangle;

 shapes[0]->radius = 5; shapes[1]->width = 3; 

shapes[1]->height = 4;

 for (int i = 0; i < 2; i++) { 

std::cout << "Shape " << i << " area: " << shapes[i]->area() 

<< std::endl; } return 0; }


This program defines a base class "Shape" with a pure virtual function "area()"
meaning that any class that inherits from it must implement its own version of the "area()" 
function. The next two lines define two derived classes "Circle" and "Rectangle" that both 
inherit from the base class "Shape" and implement their own versions of the "area()" function. In the main function, an array of pointers to objects of the base class "Shape" is declared
 with the line "Shape *shapes[2];". The next two lines initialize two elements of the array  
with pointers to objects of the derived classes "Circle" and "Rectangle", respectively. The next two lines assign values to the member variables of the objects pointed to by the 
elements of the array, and the following for loop iterates over the elements of the array 
and calls the "area()" function of each object, which returns the correct area of the object 
regardless of its actual class. The program then exits with the line "return 0;", indicating that it has completed successfully. This is just a brief overview of inheritance and polymorphism in C++. There are many more
 advanced concepts to learn, such as virtual inheritance, abstract classes, and templates, 
but understanding the basics of inheritance and polymorphism is essential for any C++ programmer.

 


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