The name of the class having pure virtual functions is called abstract class. Abstract class is important in polymorphism. Polymorphism in object oriented programming happens when there's inheritance. Polymorphism can be seen as a function in base class declared as virtual is being implemented differently in derived classes (children classes).
For instance,
class Shape {
public:
virtual void draw() = 0;
virtual void calculateArea() = 0;
};
class Circle: public Shape{
private:
int radius;
public:
void setRadius(int r){
radius = r;
}
void calculateArea (){
cout<<"The area of the circle \t";
cout<<3.142* radius * radius;
}
};
class Rectangle: public Shape {
private:
int length, width;
public:
void calculateArea (){
cout<<"The area of the rectangle \t";
cout<<width * length;
}
};
class Triangle: public Shape {
private:
int base, height;
public:
void calculateArea (){
cout<<"The area of the Triangle \t";
cout<<0.5* base * height;
}
};
Comments
Leave a comment