Answer to Question #216937 in C++ for fiend

Question #216937
What is name of class which has only pure virtual functions and how it can be used to implement important concept of object oriented programming called polymorphism. Elaborate with example of shape class having functions draw() and calculateArea().Inherit the Circle, Rectangle and Triangle classes from shape class.
Assume there is an array having 20 different shapes objects. How to draw all of the in single loop
1
Expert's answer
2021-07-14T00:49:28-0400

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;
}
};

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS