Professional Documents
Culture Documents
Polymorphism: Agnel Institute of Technology & Design
Polymorphism: 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);
}
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
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
{
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
int main()
{
B b;
A a;
A *aptr=&a;
aptr->display();
aptr=&b;
aptr->display();
}
OUTPUT:
Base
Derived