Table of Contents
hide
Example : A C++ Polymorphism Examples in C++ to show simple Compile Time/Early Binding/Static Polymorphism.
Click this link to access the example of C++ Function Overloading.
Example : A C++ program to show simple Dynamic/Run Time/Late Binding Polymorphism.
#include <iostream>
using namespace std;
class A
{
public:
void display1()
{
cout<<"Base Class Called"<<endl;
}
};
class B: public A
{
public:
void display2()
{
cout<<"Child Class Called";
}
};
int main()
{
B y;
y.display1();
y.display2();
return 0;
}
Output :
Base Class Called
Child Class Called
// ------- OR -------
#include <iostream>
using namespace std;
class A
{
public:
void display()
{
cout<<"Base Class Called"<<endl;
}
};
class B: public A
{
public:
void display()
{
cout<<"Child Class Called";
}
};
int main()
{
A x;
x.display();
B y;
y.display();
return 0;
}
Output :
Base Class Called
Child Class Called
// ----- OR ---------
#include <iostream>
using namespace std;
class A
{
public:
void display()
{
cout<<"Base Class Called"<<endl<<endl;
}
};
class B: public A
{
public:
void display()
{
cout<<"Child Class Called"<<endl;
}
};
int main()
{
B y;
y.display();
y.display();
return 0;
}
Output :
Child Class Called
Child Class Called
// ----- OR ---------
#include <iostream>
using namespace std;
class A
{
public:
void display()
{
cout<<"Base Class Called"<<endl<<endl;
}
};
class B: public A
{
public:
void display()
{
cout<<"Child Class Called"<<endl;
}
};
int main()
{
B y;
y.A::display();
y.display();
return 0;
}
Output :
Base Class Called
Child Class Called
// ----- OR ---------
#include <iostream>
using namespace std;
class A
{
public:
virtual void display()
{
cout<<"Base Class Called"<<endl<<endl;
}
};
class B: public A
{
public:
void display()
{
cout<<"Child Class Called"<<endl;
}
};
int main()
{
B y;
y.display();
y.display();
return 0;
}
Output :
Child Class Called
Child Class Called
// ----- OR --------
#include <iostream>
using namespace std;
class A
{
public:
virtual void display()
{
cout<<"Base Class Called"<<endl<<endl;
}
};
class B: public A
{
public:
virtual void display()
{
cout<<"Child Class Called"<<endl;
}
};
int main()
{
B y;
y.display();
y.display();
return 0;
}
Output :
Child Class Called
Child Class Called
Example : A C++ program to show simple Dynamic/Run Time/Late Binding Polymorphism using virtual function.
#include <iostream>
using namespace std;
class A
{
int x;
public:
A()
{
x = 100;
}
void display()
{
cout <<x<<endl;
}
};
class B: public A
{
int y;
public:
B()
{
y = 200;
}
void display()
{
cout <<y;
}
};
int main()
{
A f,*a;
B b;
a = &f;
a->display();
a=&b;
a->display();
return 0;
}
Output :
100
100
NB : This is because second call of 'a' (after the getting the address of 'b')the display() function of base class replaces replaces the existence of display() function of derived class.
// ----- OR -----
#include <iostream>
using namespace std;
class A
{
int x;
public:
A()
{
x = 100;
}
virtual void display()
{
cout <<x<<endl;
}
};
class B: public A
{
int y;
public:
B()
{
y = 200;
}
void display()
{
cout <<y;
}
};
int main()
{
A f,*a;
B b;
a = &f;
a->display();
a=&b;
a->display();
return 0;
}
Output :
100
200
NB : Here run-time binding is necessary to support polymorphism using the virtual keyword.
// --------- OR ---------
#include <iostream>
using namespace std;
class A
{
int x;
public:
A()
{
x = 100;
}
virtual void display()
{
cout <<x<<endl;
}
};
class B: public A
{
int y;
public:
B()
{
y = 200;
}
virtual void display()
{
cout <<y;
}
};
int main()
{
A f,*a;
B b;
a = &f;
a->display();
a=&b;
a->display();
return 0;
}
Output :
100
200
// --------- OR ---------
#include <iostream>
using namespace std;
class A
{
int x;
public:
A()
{
x = 100;
}
virtual void display()
{
cout <<x<<endl;
}
};
class B: public A
{
int y;
public:
B()
{
y = 200;
}
void display()
{
cout <<y;
}
};
int main()
{
A a;
B b, *ptr;
ptr = &b;
ptr->display();
ptr=&a;
ptr->display();
return 0;
}
Output : [Error] invalid conversion from 'A*' to 'B*' [-fpermissive]
NB: Here, ptr is declared as a pointer to B. But &a is of type A*, and we cannot assign a base class object’s address to a derived class pointer directly — that's why we get the error.
Example : A C++ program to show Pure Virtual Function to show Dynamic Polymorphism.
#include <iostream>
class Animal
{
public:
virtual void makeSound() = 0; // pure virtual function
};
class Dog : public Animal
{
public:
void makeSound()
{
std::cout << "Dogs sound Woof!" << std::endl;
}
};
class Cat : public Animal
{
public:
void makeSound()
{
std::cout << "Cats sound Meow!" << std::endl;
}
};
int main() {
Animal* animalPtr;
Dog dog;
Cat cat;
animalPtr = &dog;
animalPtr->makeSound();
animalPtr = &cat;
animalPtr->makeSound();
return 0;
}
Output:
Dogs sound Woof!
Cats sound Meow!
Example : A C++ program to show Virtual and Pure Virtual Functions to show Dynamic Polymorphism.
#include <iostream>
using namespace std;
// Base class Shape with one virtual function and one pure virtual function
class Shape
{
public:
// Virtual function - can be overridden by derived classes
virtual void area()
{
cout << "Calculating area in base Shape class" << endl;
}
// Pure virtual function - must be overridden by all non-abstract derived classes
virtual void draw() = 0;
};
// Derived class Circle from Shape
class Circle : public Shape
{
public:
// Override area() function
void area()
{
cout << "The formula of Area of Circle is = 3.14*r*r" << endl;
}
// Implement the pure virtual function draw()
void draw()
{
cout << "Drawing Circle" << endl;
}
};
// Derived class Rectangle from Shape
class Rectangle : public Shape
{
public:
// Override area() function
void area()
{
cout << "The formula of Area of Rectangle is = l * b" << endl;
}
// Implement the pure virtual function draw()
void draw()
{
cout << "Drawing Rectangle" << endl;
}
};
int main()
{
// Pointer to base class Shape
Shape *ptr;
// Create objects of derived classes
Circle c;
Rectangle r;
// Point to Circle object
ptr = &c;
ptr->area(); // Calls Circle's area()
ptr->draw(); // Calls Circle's draw()
// Point to Rectangle object
ptr = &r;
ptr->area(); // Calls Rectangle's area()
ptr->draw(); // Calls Rectangle's draw()
return 0;
}
Output:
The formula of Area of Circle is = 3.14*r*r
Drawing Circle
The formula of Area of Rectangle is = l * b
Drawing Rectangle
NB:
(i) Here,Circle and Rectangle are abstract classes because they inherit from Shape which has a pure virtual function draw() and hence is abstract class and also
we must override draw() in these derive class.
(ii) Here, Virtual functions give us optional overriding and Pure virtual functions enforce overriding (makes the base class abstract).This approach is ideal for building flexible polymorphic systems.
(iii) Here, we can't create objects of abstract class Shape (such as Shape s, *ptr) rather we only declare pointer for the abstract class(such as Shape *ptr) .
0 Comments