0% found this document useful (0 votes)
117 views1 page

Polymorphism in C++

Polymorphism in C++ allows a call to a member function to cause different code to execute depending on the type of object invoking the function. The example shows a base Shape class that is derived by Rectangle and Triangle classes, with each overriding the area() function differently. This demonstrates polymorphism, as calling area() on a Shape object will execute different code depending on if the object is a Rectangle or Triangle.

Uploaded by

daisyduck2013
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
117 views1 page

Polymorphism in C++

Polymorphism in C++ allows a call to a member function to cause different code to execute depending on the type of object invoking the function. The example shows a base Shape class that is derived by Rectangle and Triangle classes, with each overriding the area() function differently. This demonstrates polymorphism, as calling area() on a Shape object will execute different code depending on if the object is a Rectangle or Triangle.

Uploaded by

daisyduck2013
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

6/21/2021 Polymorphism in C++ - Tutorialspoint

Polymorphism in C++

The word polymorphism means having many forms. Typically, polymorphism occurs when there is
a hierarchy of classes and they are related by inheritance.
C++ polymorphism means that a call to a member function will cause a different function to be
executed depending on the type of object that invokes the function.
Consider the following example where a base class has been derived by other two classes −

Live Demo
#include <iostream>

using namespace std;

class Shape {

protected:

int width, height;

public:

Shape( int a = 0, int b = 0){

width = a;

height = b;

int area() {

cout << "Parent class area :" <<endl;

return 0;

};

class Rectangle: public Shape {

public:

Rectangle( int a = 0, int b = 0):Shape(a, b) { }

int area () {

cout << "Rectangle class area :" <<endl;

return (width * height);

};

class Triangle: public Shape {

public:

Triangle( int a = 0, int b = 0):Shape(a, b) { }

int area () {

https://www.tutorialspoint.com/cplusplus/cpp_polymorphism.htm 1/4

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy