Answer to Question #195285 in C++ for Sword_Dancer

Question #195285

Create a class named Shape with a function that prints "This is a shape". Create another class named Polygon inheriting the Shape class with the same function that prints "Polygon is a shape". Create two other classes named Rectangle and Triangle having the same function which prints "Rectangle is a polygon" and "Triangle is a polygon" respectively. Again, make another class named Square having the same function which prints "Square is a rectangle".

Now, try calling the function by the object of each of these classes.


1
Expert's answer
2021-05-20T04:52:45-0400
#include <iostream>
using namespace std;
class Shape{
    public:
    Shape(){}
    virtual void print(){
        cout<<"\nThis is a shape.";
    }
};
class Polygon: public Shape{
    public:
    Polygon(){}
    void print(){
        cout<<"\nPolygon is a shape.";
    }
};
class Rectangle: public Polygon{
    public:
    Rectangle(){}
    void print(){
        cout<<"\nRectangle is a Polygon.";
    }
};
class Triangle: public Polygon{
    public:
    Triangle(){}
    void print(){
        cout<<"\nTriangle is a Polygon.";
    }
};
class Square: public Rectangle{
    public:
    Square(){}
    void print(){
        cout<<"\nSquare is a Rectangle.";
    }
};
int main(){
    Shape S;
    Polygon P;
    Rectangle R;
    Triangle T;
    Square Sq;
    S.print();
    P.print();
    R.print();
    T.print();
    Sq.print();
    return 0;
} 

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