The Code given below can be used in TURBO C++ Compilers: -
#include <iostream.h>
#include <conio.h>
class Shape
{
public:
virtual int getArea() = 0;
void setWidth(int w)
{
width = w;
}
void setHeight(int h)
{
height = h;
}
protected:
int width;
int height;
};
class Rectangle : public Shape
{
public:
int getArea()
{
return (width * height);
}
};
class Triangle : public Shape
{
public:
int getArea()
{
return (width * height) / 2;
}
};
void main()
{
clrscr();
Rectangle Rect;
Triangle Tri;
Rect.setWidth(5);
Rect.setHeight(7);
cout << "Total Rectangle area: " << Rect.getArea() << endl;
Tri.setWidth(5);
Tri.setHeight(7);
cout << "Total Triangle area: " << Tri.getArea() << endl;
getch();
}
//.......Coded by RISHAB NAIR
The Code given below can be used in gcc/g++ Compilers: -
#include <iostream>
using namespace std;
class Shape
{
public:
virtual int getArea() = 0;
void setWidth(int w)
{
width = w;
}
void setHeight(int h)
{
height = h;
}
protected:
int width;
int height;
};
class Rectangle : public Shape
{
public:
int getArea()
{
return (width * height);
}
};
class Triangle : public Shape
{
public:
int getArea()
{
return (width * height) / 2;
}
};
int main(void)
{
Rectangle Rect;
Triangle Tri;
Rect.setWidth(5);
Rect.setHeight(7);
cout << "Total Rectangle area: " << Rect.getArea() << endl;
Tri.setWidth(5);
Tri.setHeight(7);
cout << "Total Triangle area: " << Tri.getArea() << endl;
return 0;
}
//.......Coded by RISHAB NAIR
#ENJOY CODING
Post a Comment
FOR ANY DOUBTS AND ERRORS FEEL FREE TO ASK. YOUR DOUBTS WILL BE ADDRESSED ASAP