Write a program to compute the area of circle and triangle by using the pure virtual function.
#include <iostream>
using namespace std;
class Shape{
protected:
public:
Shape(){}
virtual void area(){}
};
class Triangle: public Shape{
float base, height;
public:
Triangle(float b, float h):base(b), height(h), Shape(){}
void area(){
cout<<0.5 * base * height;
}
};
class Circle: public Shape{
float radius;
public:
Circle(float r):radius(r), Shape(){}
void area(){
cout<<3.14159 * radius * radius;
}
};
int main(){
Circle circle(7);
Triangle triangle(5, 8);
cout<<"Circle: ";circle.area();cout<<endl;
cout<<"Triangle: ";triangle.area();cout<<endl;
return 0;
}
Comments
Leave a comment