Professional Documents
Culture Documents
CSE Assignment Virtual Function
CSE Assignment Virtual Function
CSE Assignment Virtual Function
29 Aug
2016
Q7. Write a C++ program to implement Polymorphism using the
concept of Virtual functions
#include<iostream>
#include<stdlib.h>
using namespace std;
class BaseClass{
public:
virtual void Display(){
cout<<"You are accessing Display Function of Superclass"<<endl;
}
};
int main(){
BaseClass Base,*BasePointer;
DerivedClass Derived, *DerivedPointer;
DerivedClass2 Derived2;
BasePointer=&Base;
BasePointer->Display();
BasePointer=&Derived;
BasePointer->Display();
BasePointer=&Derived2;
BasePointer->Display();
DerivedPointer=&Derived;
DerivedPointer->Display();
DerivedPointer=&Derived2;
DerivedPointer->Display();
return 0;
}
Output:
Date:
2016
29 Aug
switch(num){
case 1: area Rectangle;
int length,breadth;
cout<<"Enter Length and Breadth of Rectangle"<<endl;
cin>>length>>breadth;
Rectangle.objarea(length,breadth);
break;
case 2: area Square;
int sqlength;
cout<<"Enter Length of Square"<<endl;
cin>>sqlength;
Square.objarea(sqlength);
break;
case 3: area Circle;
float radius;
cout<<"Enter Radius of Circle followed by decimals"<<endl;
cin>>radius;
Circle.objarea(radius);
break;
case 4: exit(1);
break;
default: cout<<"Enter Valid Input!"<<endl;
}
}while(num<5);
return 0;
}
Output:
Enter the Choice
1. Area of Rectangle
2. Area of Square
3. Area of Circle
4. exit
1
Enter Length and Breadth of Rectangle
10 20
Area is : 200
Enter the Choice
1. Area of Rectangle
2. Area of Square
3. Area of Circle
4. exit
2
Enter Length of Square
10
Area is : 100
Enter the Choice
1. Area of Rectangle
2. Area of Square
3. Area of Circle
4. exit
4