0% found this document useful (0 votes)
57 views5 pages

Polymorphism: Agnel Institute of Technology & Design

The document discusses various examples of polymorphism in C++. It includes examples of function overloading to compute powers and volumes of different shapes. It also provides examples of function overriding and pure virtual functions. The last example demonstrates runtime polymorphism by calling a virtual function through a base class pointer, which displays the derived class' implementation.

Uploaded by

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

Polymorphism: Agnel Institute of Technology & Design

The document discusses various examples of polymorphism in C++. It includes examples of function overloading to compute powers and volumes of different shapes. It also provides examples of function overriding and pure virtual functions. The last example demonstrates runtime polymorphism by calling a virtual function through a base class pointer, which displays the derived class' implementation.

Uploaded by

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

AGNEL INSTITUTE OF TECHNOLOGY & DESIGN

POLYMORPHISM
1. Write a C++ program to implement function overloading in order to compute power
(m,n) where, (i) m is double, n is int, (ii) m and n are int.
PROGRAM:
#include<iostream>
#include<cmath>
using namespace std;
void power(int,int );
void power(double,int);
int main()
{
int m1,n1,n2;
double m2;
cout<<"Enter the value of m and n (where m is double, n is int):";
cin>>m1>>n1;
cout<<"Enter the value of m and n (where m and n are int):";
cin>>m2>>n2;
power(m1,n1);
power(m2,n2);
}

void power(int m1,int n1)


{
int c=pow(m1,n1);
cout<<"\nThe value is:"<<c;
}
void power(double m2,int n2)
{
int d=pow(m2,n2);
cout<<"\nThe value is:"<<d;
}
OUTPUT:
Enter the value of m and n (where m is double, n is int):25 5
Enter the value of m and n (where m and n are int):10 9

The value is:9765625


The value is:1000000000

2. Write a C++ program to implement function overloading in order to compute volume


of cube, cylinder and sphere.
PROGRAM:
#include<iostream>
using namespace std;
void volume(int);
void volume(float,int,int);
void volume(float,int);
int main()
{

OOPC++ Roll No: 18CO51


AGNEL INSTITUTE OF TECHNOLOGY & DESIGN

int s, r1, r2, h;


float pi;
cout<<"Enter the value of Pi: ";
cin>>pi;
cout<<"Enter the value of side of the cube: ";
cin>>s;
cout<<"Enter the values of radius & height of the cylinder: ";
cin>>r1>>h;
cout<<"Enter the value of radius of the sphere: ";
cin>>r2;
volume(s);
volume(pi,r1,h);
volume(pi,r2);
return 0;
}
void volume(int s)
{
cout<<"\nVolume of cube is:"<<s*s*s;
}
void volume(float pi,int r1,int h)
{
cout<<"\nVolume of cylinder is:"<<pi*r1*r1*h;
}
void volume(float pi,int r2)
{
cout<<"\nVolume of sphere is:"<<(4*pi*r2*r2*r2)/3;
}
OUTPUT:
Enter the value of Pi: 3.1412
Enter the value of side of the cube: 10
Enter the values of radius & height of the cylinder: 7 10
Enter the value of radius of the sphere: 7

Volume of cube is:1000


Volume of cylinder is:1539.19
Volume of sphere is:1436.58

3. Write a C++ program to implement function overriding in order to calculate volume


of cube.
PROGRAM:
#include<iostream>
using namespace std;
class base
{
public:
float s;
void getdata()
{
cout<<"Enter side of the cube: ";

OOPC++ Roll No: 18CO51


AGNEL INSTITUTE OF TECHNOLOGY & DESIGN

cin>>s;
}
void display()
{
cout<<"Volume of cube is: "<<s*s*s;
}
};
class derived: public base
{
public:
float s;
void getdata()
{
cout<<"\n\nEnter side of the cube: ";
cin>>s;
}
void display()
{
cout<<"Volume of cube is: "<<s*s*s;
}
};
int main()
{
base b;
derived d;
b.getdata();
b.display();
d.getdata();
d.display();
return 0;
}
OUTPUT:
Enter side of the cube: 7
Volume of cube is: 343

Enter side of the cube: 10


Volume of cube is: 1000

4. Write a C++ program to illustrate use of pure virtual function in order to compute
volume of cube.
PROGRAM:
#include<iostream>
using namespace std;
class A
{
public:
virtual void disp()=0;
};
class B:public A

OOPC++ Roll No: 18CO51


AGNEL INSTITUTE OF TECHNOLOGY & DESIGN

{
public:
float s;

void getdata()
{
cout<<"Enter side: ";
cin>>s;
}
void disp()
{
cout<<"Volume of cube is: "<<s*s*s;
}
};
int main()
{
B b;
b.getdata();
b.disp();
}
OUTPUT:
Enter side: 10
Volume of cube is: 1000

5. Implement run time polymorphism with help of an example.


PROGRAM:
#include<iostream>
using namespace std;
class A
{
public:
virtual void display()
{
cout<<"\nBase";
}
};
class B:public A
{
public:
void display()
{
cout<<"\nDerived";
}
};

int main()
{
B b;

OOPC++ Roll No: 18CO51


AGNEL INSTITUTE OF TECHNOLOGY & DESIGN

A a;
A *aptr=&a;
aptr->display();
aptr=&b;
aptr->display();
}
OUTPUT:
Base
Derived

OOPC++ Roll No: 18CO51

You might also like