Question

In: Computer Science

Polymorphism and Inheritance Shape.h class Shape {             string color;             bool filled; public:      &n

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

Solutions

Expert Solution

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


Related Solutions

This question demonstrates the use of inheritance and polymorphism. Design a Ship class that has the...
This question demonstrates the use of inheritance and polymorphism. Design a Ship class that has the following members: • A field for the name of the ship (a string). • A field for the year that the ship was built (a string). • A constructor and appropriate accessors and mutators. • A toString method that overrides the toString method in the Object class. The Ship class toString method should display the ship’s name and the year it was built. Design...
public class Graph { private ST<String, SET<String>> st; public Graph() { st = new ST<String, SET<String>>();...
public class Graph { private ST<String, SET<String>> st; public Graph() { st = new ST<String, SET<String>>(); } public void addEdge(String v, String w) { // Put v in w's SET and w in v's SET. if (!st.contains(v)) st.put(v, new SET<String>()); if (!st.contains(w)) st.put(w, new SET<String>()); st.get(v).add(w); st.get(w).add(v); } public Iterable<String> adjacentTo(String v) { return st.get(v); } public Iterable<String> vertices() { return st.keys(); } // See Exercises 4.5.1-4 for V(), E(), degree(), // hasVertex(), and hasEdge(). public static void main(String[] args)...
In C++ Demonstrate inheritance. Create an Airplane class with the following attributes: • manufacturer : string...
In C++ Demonstrate inheritance. Create an Airplane class with the following attributes: • manufacturer : string • speed : float Create a FigherPlane class that inherits from the Airplane class and adds the following attributes: • numberOfMissiles : short
Java public class UpperCaseString { //1      protected String content; // 2      public UpperCaseString(String content)...
Java public class UpperCaseString { //1      protected String content; // 2      public UpperCaseString(String content) { // 3           this.content = content.toUpperCase(); // 4      } // 5      public String toString() { // 6           return content.toUpperCase(); // 7      } // 8      public static void main(String[] args) { // 9           UpperCaseString upperString =              new UpperCaseString("Hello, Cleo!"); // 10           System.out.println(upperString); // 11      } // 12 } // 13 THE FOLLOWING 3 QUESTIONS REFER...
Consider this program: public class Main { public static void main(String[] args) { String s1 =...
Consider this program: public class Main { public static void main(String[] args) { String s1 = "hello"; String s2 = "hello"; String s3 = new String("hello"); System.out.println(s1 == s2); System.out.println(s2 == s3); System.out.println(s2.equals(s3)); } } When we run the program, the output is: true false true Explain why this is the output, using words and/or pictures.
Class Employee (All IN JAVA) public class Employee {public String strName, strSalary; public Employee(){strName = "...
Class Employee (All IN JAVA) public class Employee {public String strName, strSalary; public Employee(){strName = " ";strSalary = "$0";} public Employee(String Name, String Salary){strName = Name;strSalary = Salary;} public void setName(String Name){strName = Name;} public void setSalary(String Salary){strSalary = Salary;}public String getName(){return strName;} public String getSalary(){return strSalary;} public String toString(){return(strName + " has a salary of " + strSalary); Create another method to return the name and salary nicely formatted as a string (hint – research the toString method). You...
public class StringNode { private String item; private StringNode next; } public class StringLL { private...
public class StringNode { private String item; private StringNode next; } public class StringLL { private StringNode head; private int size; public StringLL(){ head = null; size = 0; } public void add(String s){ add(size,s); } public boolean add(int index, String s){ ... } public String remove(int index){ ... } } In the above code add(int index, String s) creates a StringNode and adds it to the linked list at position index, and remove(int index) removes the StringNode at position...
java programing Q: Given the following class: public class Student { private String firstName; private String...
java programing Q: Given the following class: public class Student { private String firstName; private String lastName; private int age; private University university; public Student(String firstName, String lastName, int age, University university) { this.firstName = fisrtName; this.lastName = lastName; this.age = age; this.university = university; } public String getFirstName(){ return firstName; } public String getLastName(){ return lastName; } public int getAge(){ return age; } public University getUniversity(){ return university; } public String toString() { return "\nFirst name:" + firstName +...
public class StringTools {    public static int count(String a, char c) {          ...
public class StringTools {    public static int count(String a, char c) {           }
Create a class called Dishwash with a double called CubicFeet, a string called color, and a...
Create a class called Dishwash with a double called CubicFeet, a string called color, and a method called Washing. The Washing method should return a string to the main class with the text "Now washing dishes!" In the main class create an array of DishWasher size 3. Give each DishWasher a different color and CubicFeet. Use a foreach loop to display that info, call the Washing method, and display the text returned from the Washing method call. c#
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT