Skip to content

Instantly share code, notes, and snippets.

@aditya-vijaykumar
Created December 1, 2020 14:47
Show Gist options
  • Save aditya-vijaykumar/1e168443a0f0cc18f4856f35b932aa43 to your computer and use it in GitHub Desktop.
Save aditya-vijaykumar/1e168443a0f0cc18f4856f35b932aa43 to your computer and use it in GitHub Desktop.
#include <iostream>
using namespace std;
class Shape {
protected: double areaValue;
public:
virtual void area() = 0;
};
class Triangle: public Shape {
protected: int height, base;
public:
Triangle(int h, int b){
height = h;
base = b;
}
void area(){
areaValue = 0.5*base*height;
cout<<"Calculating Triangle area."<<endl<<"Area = "<<areaValue<<" sq. units"<<endl<<endl;
}
};
class Rectangle: public Shape {
protected: int length, breadth;
public:
Rectangle(int l, int b){
length = l;
breadth = b;
}
void area(){
areaValue = length*breadth;
cout<<"Calculating Rectangle area."<<endl<<"Area = "<<areaValue<<" sq. units"<<endl<<endl;
}
};
class Circle: public Shape {
protected: int radius;
public:
Circle(int r){
radius = r;
}
void area(){
areaValue = 3.14*radius*radius;
cout<<"Calculating Circle area."<<endl<<"Area = "<<areaValue<<" sq. units"<<endl<<endl;
}
};
int main() {
Shape *ptr;
Triangle ob1(4,3);
Rectangle ob2(3,6);
Circle ob3(5);
ptr = &ob1;
ptr->area();
ptr = &ob2;
ptr->area();
ptr = &ob3;
ptr->area();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment