//base class Polygon with two dimensions
//Derived classes Rectangle and Triangle to calculate Area()
#include <iostream>
using namespace std;
class Polygon
{
protected:
int dim1,dim2;
public:
void readData()
{
cout<<"Enter dimensions:";
cin>>dim1>>dim2;
}
};
class Rectangle:public Polygon
{
public:
int Area()
{
return dim1*dim2;
}
};
class Triangle:public Polygon
{
public:
int Area()
{
return (dim1*dim2/2);
}
};
int main()
{
Polygon *p;
Rectangle r;
cout<<endl<<"For rectangle:"<<endl;
r.readData();
p=&r;
cout<<"Area of rectangle is "<<((Rectangle*)p)->Area();
//Derived classes Rectangle and Triangle to calculate Area()
#include <iostream>
using namespace std;
class Polygon
{
protected:
int dim1,dim2;
public:
void readData()
{
cout<<"Enter dimensions:";
cin>>dim1>>dim2;
}
};
class Rectangle:public Polygon
{
public:
int Area()
{
return dim1*dim2;
}
};
class Triangle:public Polygon
{
public:
int Area()
{
return (dim1*dim2/2);
}
};
int main()
{
Polygon *p;
Rectangle r;
cout<<endl<<"For rectangle:"<<endl;
r.readData();
p=&r;
cout<<"Area of rectangle is "<<((Rectangle*)p)->Area();
No comments:
Post a Comment