Object Oriented Programming: Chapter 3: Inheritance and Compile Time Polymorphism

Polymorphism

C++ Programming | Object Oriented Programming

What is meant by polymorphism? Explain the various types of polymorphism in C++ with suitable examples.

Polymorphism

• Polymorphism means many forms. It is one of the important features of OOP.

• Polymorphism is basically an ability to create a variable, a function or an object that has more than one form.

• The primary goal of polymorphism is an ability of the object of different types to respond to methods and data values by using the same name. The programmer does not have to know the exact type of the object in advance hence exact behavior of the object is determined at the run time.

• Polymorphism is concerned with the application of specific implementation or use of abstract base class.


1. Difference between Inheritance and Polymorphism


Inheritance

1. Inheritance is a property in which some of the properties and methods of base class can be derived by the derived class.

2. Various types of inheritance can be single inheritance, multiple inheritance, multilevel inheritance and hybrid inheritance.

Polymorphism

1. Polymorphism is ability for an object to used different forms. The name of the function remains the same but it can perform different tasks.

2. Various types of polymorphism are compile time polymorphism and run time polymorphism. In compile time polymorphism there are two types of overloading possible. ‒‒‒‒Functional overloading and operator overloading. In run time polymorphism there is a use of A virtual function.

 

2. Implementing Polymorphism

The polymorphism can be implemented by two types ‒ Compile time and run time.


• Following is a simple C++ program that illustrate the concept of polymorphism.

#include <iostream>

using namespace std;

class test {

private:

    int a,b,c,d;

public:

    int sum(int,int);  //Two forms of the function named sum

    int sum(int,int,int);

};

int test::sum(int a,int b)

{

    return (a+b);

}

int test::sum(int a,int b,int c)

{

    return (a+b+c);

}

 int main()

{

test obj;

int a, b, c,choice;

int result=0;

cout<<"\n\t\t Main Menu";

cout<<"\n\t 1.Addition of two numbers";

cout<<"\n\t 2.Addition of three numbers";

cout<<"\n Enter Your choice: ";

cin >> choice;

switch(choice)

{

case 1: cout<<"\nEnter 2 numbers: ";

 cin >> a >> b;

result = obj.sum(a,b);

break;

case 2: cout<<"\nEnter 3 numbers: ";

cin >> a >> b >> c;

result = obj.sum(a,b,c);

break;

default: cout<<"\nWrong Choice";

break;

}

cout<<"\n\nresult: " << result << endl;

return 0;

}

Output

Main Menu

1.Addition of two numbers

2. Addition of three numbers

Enter Your choice: 1

Enter 2 numbers: 10 20

result: 30

 

Review Question

1.What is meant by polymorphism? Explain the various types of polymorphism in C++ with suitable examples.

 

Object Oriented Programming: Chapter 3: Inheritance and Compile Time Polymorphism : Tag: Oops, Computer Programming : C++ Programming | Object Oriented Programming - Polymorphism


Object Oriented Programming: Chapter 3: Inheritance and Compile Time Polymorphism



Under Subject


Object Oriented Programming (OOPs)

CS25C07 2nd Semester CSE, CSE(CY) Depts | 2025 Regulation | 2nd Semester 2025 Regulation



Related Subjects


English Essentials II

EN25C02 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation



Linear Algebra

MA25C02 2nd Semester | 2025 Regulation


Applied Physics (CSIE) II

PH25C03 2nd Semester AIDS, CSE, IT, CSE(CY) Dept | 2025 Regulation | 2nd Semester 2025 Regulation


Digital Principles and Computer Organization

CS25C06 2nd Semester AIDS, CSE, IT, CSE(CY) Dept | 2025 Regulation | 2nd Semester 2025 Regulation


Basic Electrical and Electronics Engineering

EE25C01 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation


Object Oriented Programming (OOPs)

CS25C07 2nd Semester CSE, CSE(CY) Depts | 2025 Regulation | 2nd Semester 2025 Regulation


Re-Engineering for Innovation

ME25C05 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation


Object Oriented Programming (OOPs) - Laboratory

CS25C07 2nd Semester CSE, CSE(CY) Depts | 2025 Regulation | 2nd Semester 2025 Regulation