Data Structures using C PlusPlus: Chapter 2: Inheritance and Polymorphism

Polymorphism

Data Structures using C++ Program

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

Polymorphism

• Definition : Polymorphism in C++ means one name with many forms. It allows the same function name or operator to behave differently depending on the context or the object that invokes it.

• 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.


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 virtual function.

 

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.

 

Data Structures using C PlusPlus: Chapter 2: Inheritance and Polymorphism : Tag: Data Structure, C++, C Programing : Data Structures using C++ Program - Polymorphism


Data Structures using C PlusPlus: Chapter 2: Inheritance and Polymorphism



Under Subject


Data Structures using CPlusPlus

CS25C05 2nd Semester ECE Dept | 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


Electron Devices

EC25C01 2nd Semester ECE Dept | 2025 Regulation | 2nd Semester 2025 Regulation


Data Structures using CPlusPlus

CS25C05 2nd Semester ECE Dept | 2025 Regulation | 2nd Semester 2025 Regulation


Circuits and Network Analysis

EC25C02 2nd Semester ECE Dept | 2025 Regulation | 2nd Semester 2025 Regulation


Re-Engineering for Innovation

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


Engineering Drawing - Laboratory

ME25C01 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation


Data Structures using CPlusPlus - Laboratory

CS25C05 2nd Semester ECE Dept | 2025 Regulation | 2nd Semester 2025 Regulation


Devices and Circuits Laboratory

EC25C03 2nd Semester ECE Dept | 2025 Regulation | 2nd Semester 2025 Regulation