In: Computer Science
Polymorphism and Inheritance
Shape.h
class Shape {
string color;
bool filled;
public:
Shape();
Shape(string, bool);
string getColor();
bool isFilled();
void setColor(string);
void setFilled(bool);
string toString();
//virtual string toString();
};
Rectangle.h
class Rectangle : public Shape {
double length;
double width;
public:
Rectangle();
Rectangle(double, double);
Rectangle(double, double, string, bool);
double getLength();
double getWidth();
void setLength(double);
void setWidth(double);
double getArea();
double getPerimeter();
printShape() function prototypes:
void printShape(Shape); or
void printShape(Shape &);
^^test which works correctly
Make a Rectangle class and a Triangle class that extend Shape class
^^not sure how to do this
the main that has to be used
int main() {
Shape s;
Rectangle r(5,6, "red", true);
Triangle t(6, 10, "blue", true);
printShape(s);
Shape s1("blue", false);
printShape(s1);
//add virtual and pass the object by reference
printShape(r);
printShape(t);
return 0;
}
the instances need to be created and printShape() method needs to be called
triangle formula used is =1/2base*height
The C++ code for the above problem statement is as follows:-
#include <iostream>
using namespace std;
class shape
{
public:
string color;
bool filled;
shape()
{
color="";
filled=false;
}
shape(string c,bool f)
{
color=c;
filled=f;
}
string getColor()
{
return color;
}
bool isFilled()
{
return filled;
}
void setColor(string c)
{
color=c;
}
void setFilled(bool f)
{
filled=f;
}
void toString() //virtual method of toString() as mentioned in the
question.
{
string f,s;
if(filled==true)
f="filled";
else
f="not filled";
cout<<"\n Color of shape is: "<<color<<" and the
shape is: "<<f;
}
virtual void printShape(shape S) //virtual method and pass object
by reference
{
string f,s;
if(S.filled==true)
f="filled";
else
f="not filled";
cout<<"\n Color of shape is: "<<S.color<<" and
the shape is: "<<f;
}
};
class rectangle : public shape //class rectangle inherit class
shape
{
double length;
double width;
public:
rectangle()
{
length=0;
width=0;
filled=false;
color="";
}
rectangle(double l,double w)
{
length=l;
width=w;
}
rectangle(double l,double w,string s,bool f)
{
length=l;
width=w;
filled=f;
color=s;
}
double getLength()
{
return length;
}
double getWidth()
{
return width;
}
void setLength(double l)
{
length=l;
}
void setWidth(double w)
{
width=w;
}
double getArea()
{
return length*width;
}
double getPArimeter()
{
return 2*(length+width);
}
string toString() //calling the parent class method
{
shape::toString();
}
void printShape(rectangle R) //virtual method and pass object by
reference
{
string f;
if(R.filled==true)
f="filled";
else
f="not filled";
cout<<"\n Color of rectangle is: "<<R.color<<"
and the rectangle is: "<<f;
}
};
class triangle : public shape //class rectangle inherit class
shape
{
double base;
double height;
public:
triangle()
{
height=0;
base=0;
filled=false;
color="";
}
triangle(double h,double b)
{
height=h;
base=b;
}
triangle(double h,double b,string s,bool f)
{
height=h;
base=b;
filled=f;
color=s;
}
double getHeight()
{
return height;
}
double getBase()
{
return base;
}
void setHeight(double h)
{
height=h;
}
void setBase(double b)
{
base=b;
}
double getArea()
{
return 0.5*height*base;
}
string toString() //calling the parent class method
{
shape::toString();
}
void printShape(triangle T) //virtual method and pass object by
reference
{
string f;
if(T.filled==true)
f="filled";
else
f="not filled";
cout<<"\n Color of triangle is: "<<T.color<<" and
the triangle is: "<<f;
}
};
int main()
{
shape s;
shape s1("blue",false);
shape *sh; //declaring pointer of shape class
sh=&s; // storing the address of the shape s object
sh->printShape(s); //for calling method printShape() for shape
s
sh=&s1;
sh->printShape(s1); //for calling method printShape() for shape
s1
rectangle r(5,6,"Red",true);
rectangle *rec;
rec=&r; //for calling method printShape() for rectangle r
rec->printShape(r);
triangle t(6,10,"blue",true);
triangle *tri;
tri=&t; //for calling method printShape() for triangle t
tri->printShape(t);
return 0;
}
The documentation is done by the single line comment where ever necessary, the output of the above code is as follows:-
Color of shape is: and the shape is: not filled
Color of shape is: blue and the shape is: not filled
Color of rectangle is: Red and the rectangle is: filled
Color of triangle is: blue and the triangle is: filled