A) Write a program to illustrate run time polymorphism.
B) Write a program illustrates pure virtual function and calculate the area of different shapes by using abstract class.
A) AIM: Write a program to illustrate run-time polymorphism.
SOURCE CODE:
#include<iostream>
using namespace std;
class BaseClass
{
public:
virtual void print()
{
cout<<"Base class print() method.\n";
}
};
class DerivedClass: public BaseClass
{
public:
void print()
{
cout<<"Derived class print() method.\n";
}
};
int main(void)
{
BaseClass *bc = new DerivedClass;
bc->print(); // RUN-TIME POLYMORPHISM
return 0;
}
OUTPUT:
Derived class print() method.
B) AIM: Write a program illustrates pure virtual function and calculate the area of different shapes by using abstract class.
SOURCE CODE:
#include<iostream>
using namespace std;
class shape
{
public:
double x,y;
virtual void getdata()=0;
virtual void area()=0;
};
class rectangle:public shape
{
public:
double a;
int length,width;
void getdata()
{
cout<<"Enter the length and width of rectangle:"<<endl;
cin>>length>>width;
}
void area()
{
a=length*width;
}
void display()
{
cout<<"Area of rectangle is:"<<a<<endl;
}
};
class triangle:public shape
{
public:
double a;
int height,base;
void getdata()
{
cout<<"Enter the base and height of triangle:"<<endl;
cin>>height>>base;
}
void area()
{
a=0.5*base*height;
}
void display()
{
cout<<"Area of triangle is:"<<a<<endl;
}
};
int main()
{
shape *s;
rectangle r;
triangle t;
cout<<"RECTANGLE"<<endl;
s=&r;
s->getdata();
s->area();
r.display();
cout<<"TRIANGLE"<<endl;
s=&t;
s->getdata();
s->area();
t.display();
return 0;
}
OUTPUT:
RECTANGLE
Enter the length and width of rectangle:
4
2
Area of rectangle is:8
TRIANGLE
Enter the base and height of triangle:
5
9
Area of triangle is:22.5
B) Write a program illustrates pure virtual function and calculate the area of different shapes by using abstract class.
A) AIM: Write a program to illustrate run-time polymorphism.
SOURCE CODE:
#include<iostream>
using namespace std;
class BaseClass
{
public:
virtual void print()
{
cout<<"Base class print() method.\n";
}
};
class DerivedClass: public BaseClass
{
public:
void print()
{
cout<<"Derived class print() method.\n";
}
};
int main(void)
{
BaseClass *bc = new DerivedClass;
bc->print(); // RUN-TIME POLYMORPHISM
return 0;
}
OUTPUT:
Derived class print() method.
B) AIM: Write a program illustrates pure virtual function and calculate the area of different shapes by using abstract class.
SOURCE CODE:
#include<iostream>
using namespace std;
class shape
{
public:
double x,y;
virtual void getdata()=0;
virtual void area()=0;
};
class rectangle:public shape
{
public:
double a;
int length,width;
void getdata()
{
cout<<"Enter the length and width of rectangle:"<<endl;
cin>>length>>width;
}
void area()
{
a=length*width;
}
void display()
{
cout<<"Area of rectangle is:"<<a<<endl;
}
};
class triangle:public shape
{
public:
double a;
int height,base;
void getdata()
{
cout<<"Enter the base and height of triangle:"<<endl;
cin>>height>>base;
}
void area()
{
a=0.5*base*height;
}
void display()
{
cout<<"Area of triangle is:"<<a<<endl;
}
};
int main()
{
shape *s;
rectangle r;
triangle t;
cout<<"RECTANGLE"<<endl;
s=&r;
s->getdata();
s->area();
r.display();
cout<<"TRIANGLE"<<endl;
s=&t;
s->getdata();
s->area();
t.display();
return 0;
}
OUTPUT:
RECTANGLE
Enter the length and width of rectangle:
4
2
Area of rectangle is:8
TRIANGLE
Enter the base and height of triangle:
5
9
Area of triangle is:22.5