0% found this document useful (0 votes)
5 views

Polymorphism_Inheritance_Examples

The document provides code examples illustrating concepts of polymorphism and inheritance in C++. It covers function overloading, function overriding, virtual and abstract classes, and the importance of virtual destructors, along with examples of compile-time and runtime polymorphism. Each section includes code snippets demonstrating the respective concepts.

Uploaded by

khanyyasir40
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)
5 views

Polymorphism_Inheritance_Examples

The document provides code examples illustrating concepts of polymorphism and inheritance in C++. It covers function overloading, function overriding, virtual and abstract classes, and the importance of virtual destructors, along with examples of compile-time and runtime polymorphism. Each section includes code snippets demonstrating the respective concepts.

Uploaded by

khanyyasir40
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/ 10

Polymorphism and Inheritance Code Examples

Function Overloading vs Function Overriding

Function Overloading Example (Compile-Time Polymorphism):

---------------------------------------------------------

#include <iostream>

using namespace std;

class OverloadExample {

public:

void print(int i) {

cout << "Integer: " << i << endl;

void print(double d) {

cout << "Double: " << d << endl;

void print(const string &s) {

cout << "String: " << s << endl;

};

int main() {

OverloadExample obj;

obj.print(42);

obj.print(3.14);
obj.print("Hello, World!");

return 0;

Function Overriding Example (Runtime Polymorphism):

---------------------------------------------------

#include <iostream>

using namespace std;

class Base {

public:

virtual void display() {

cout << "Base class display" << endl;

};

class Derived : public Base {

public:

void display() override {

cout << "Derived class display" << endl;

};

int main() {

Base *basePtr;

Derived derivedObj;

basePtr = &derivedObj;
// Runtime Polymorphism

basePtr->display(); // Calls Derived class display

return 0;

Polymorphism vs Inheritance

Inheritance Example:

--------------------

#include <iostream>

using namespace std;

class Parent {

public:

void parentMethod() {

cout << "Parent method" << endl;

};

class Child : public Parent {

public:

void childMethod() {

cout << "Child method" << endl;

};
int main() {

Child obj;

obj.parentMethod(); // Inherited method

obj.childMethod(); // Child method

return 0;

Polymorphism Example:

---------------------

#include <iostream>

using namespace std;

class Shape {

public:

virtual void draw() {

cout << "Drawing Shape" << endl;

};

class Circle : public Shape {

public:

void draw() override {

cout << "Drawing Circle" << endl;

};
class Rectangle : public Shape {

public:

void draw() override {

cout << "Drawing Rectangle" << endl;

};

int main() {

Shape *shapePtr;

Circle circleObj;

Rectangle rectObj;

shapePtr = &circleObj;

shapePtr->draw(); // Drawing Circle

shapePtr = &rectObj;

shapePtr->draw(); // Drawing Rectangle

return 0;

Virtual vs Abstract Class

Virtual Method Example:

-----------------------

#include <iostream>
using namespace std;

class Base {

public:

virtual void show() {

cout << "Base class method" << endl;

};

class Derived : public Base {

public:

void show() override {

cout << "Derived class method" << endl;

};

int main() {

Base *basePtr;

Derived derivedObj;

basePtr = &derivedObj;

basePtr->show(); // Calls Derived class method

return 0;

Abstract Class Example:

-----------------------
#include <iostream>

using namespace std;

class AbstractBase {

public:

virtual void display() = 0; // Pure virtual function

};

class Concrete : public AbstractBase {

public:

void display() override {

cout << "Concrete class implementation" << endl;

};

int main() {

Concrete obj;

obj.display(); // Concrete class implementation

return 0;

Virtual Destructor

Example:

--------

#include <iostream>
using namespace std;

class Base {

public:

virtual ~Base() {

cout << "Base Destructor" << endl;

};

class Derived : public Base {

public:

~Derived() {

cout << "Derived Destructor" << endl;

};

int main() {

Base *ptr = new Derived();

delete ptr; // Proper cleanup using virtual destructor

return 0;

Compile-Time vs Runtime Polymorphism

Compile-Time Polymorphism Example:

----------------------------------
#include <iostream>

using namespace std;

class Calculator {

public:

int add(int a, int b) {

return a + b;

double add(double a, double b) {

return a + b;

};

int main() {

Calculator calc;

cout << "Integer Addition: " << calc.add(5, 3) << endl;

cout << "Double Addition: " << calc.add(2.5, 1.5) << endl;

return 0;

Runtime Polymorphism Example:

-----------------------------

#include <iostream>

using namespace std;

class Animal {

public:
virtual void sound() {

cout << "Animal Sound" << endl;

};

class Dog : public Animal {

public:

void sound() override {

cout << "Dog Barks" << endl;

};

int main() {

Animal *animalPtr = new Dog();

animalPtr->sound(); // Dog Barks

delete animalPtr;

return 0;

You might also like