Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Assignment 11 Using C++ Problem In this assignment, you are going to practice in

ID: 3688623 • Letter: A

Question

Assignment 11

Using C++

Problem

In this assignment, you are going to practice inheritance and operator overloading.

The requirements are as follows:

1. Write a program that can calculate the area for Rectangle, Circle and Triangle; and calculate the volume for Cubic, Sphere and Cone. The Cubic is a derived class from Rectangle, the Cone and Sphere should be derived class from Circle. Rectangle, Circle and Triangle are derived class from base class Shape.

2. The object should be constructed after user’s input and store the constructed objects in a linkedlist for later use. Print out the constructed object after each construction. (Hint: The objects should be stored in the Node, and you may use base class to represent all derived class for declaring variables in the class Node)

3. Overload the operator + for all derived classes so that when adding two objects of the same class, the operator returns a new object that has the summed area and volume (2D shapes have area only) of the two added objects. The parameters for the new object (e.g. base, height, radius etc.) can be determined arbitrarily based on the area and volume.

4. Overload the operator << to output all parameters for a shape, including area and volume.

5. User have commands: construct, add, print and quit: Construct: Let user to input the choice of shape and corresponding parameters.

Construct the object and store it in the linkedlist.

Add: Print out all objects in the vector to let user choose which two to be summed up. (Remember to check index range) Add the two chosen objects and print out the result. If two objects are of different class pop up the message. You DO NOT have to store the result into the linkedlist.

Print: Print out all objects in the vector.

Quit: Destruct everything and quit the program.

/////////////////////////////////////////////////////////////////////

Submit .cpp and .h files directly, and DO NOT submit the whole solution (not everything in one .cpp file).

Sample I/O

Commands are:

construct, add, print and quit

I: construct Circle 5

O: Circle radius: 5 area: 78.5

I: construct Circle 2

O: Circle radius: 2 area: 12.56

I: print

O: Circle radius: 5 area: 78.5

Circle radius: 2 area: 12.56

I: add 0 2

O: Index out of range!

I: add 0 1

O: Circle radius: 5.39 area: 91.06

I: construct Sphere 3

O: Sphere radius: 5 surface area: 113.04 volume: 113.04

I: construct Rectangle 3 5

O: Rectangle length: 5 width: 3 area: 15

I: print

O: Circle radius: 5 area: 78.5

Circle radius: 2 area: 12.56

Sphere radius: 5 surface area: 113.04 volume: 113.04

Rectangle length: 5 width: 3 area: 15

I: quit

O: End!

Explanation / Answer

Here is the code for the question mentioned. Sample output is also attached/ Please do rate the answer if it helped. Thank you very much.

shapes.cpp

#include <iostream>
#include <typeinfo>
#include <cmath>
using namespace std;
#define PI 3.14

class Shape
{
public :
virtual double area() const =0 ;

};

class Rectangle : public Shape
{
private:
double length, breadth;
public:
Rectangle()
{
length=0;
breadth=0;
}
Rectangle(double len,double brd)
{
length = len;
breadth = brd;
}
double getLength() const
{
return length;
}
double getBreadth() const
{
return breadth;
}
double area() const
{
return length * breadth;
}
friend ostream& operator << (ostream &output, const Rectangle &rect)
{
output<<"Rectangle length: "<<rect.length<<" breadth: "<<rect.breadth;
output<<" area: "<<rect.area()<<endl;
return output;
}

Rectangle operator + (const Rectangle &other)
{

double newArea = area() + other.area();
double newlength=length<other.length?length : other.length;
double newbreadth = newArea/newlength;
return Rectangle(newlength,newbreadth);
}
};


class Cubic :public Rectangle
{
private:

public:
Cubic(double len):Rectangle(len,len)
{

}
double area() const
{
return getLength() * getLength() * 6;
}
double volume() const
{
return getLength() * getLength()*getLength();
}
friend ostream& operator << (ostream &output, const Cubic &c)
{
output<<"Cubic length: "<<c.getLength();
output<<" area: "<<c.area()<<" volume: "<<c.volume()<<endl;
return output;
}

Cubic operator + (const Cubic &other)
{
double newArea = area() + other.area();
double newlength= pow(newArea,1.0/3);

return Cubic(newlength);
}

};

class Circle :public Shape
{
private:
double radius;
public:
Circle(double rad)
{
radius = rad;
}
double area() const
{
return PI * radius * radius;
}
double getRadius() const
{
return radius;
}
friend ostream& operator << (ostream &output, const Circle &c)
{
output<<"Circle radius: "<<c.radius;
output<<" area: "<<c.area()<<endl;
return output;
}

Circle operator + (const Circle &other)
{
double newArea = area() + other.area();
double newRadius= sqrt(newArea/PI);

return Circle(newRadius);
}
};

class Triangle : public Shape
{
private:
double base, height;
public:
Triangle(double b,double h)
{
base=b;
height=h;
}
double area() const
{
return 1/2.0 * base * height;
}
friend ostream& operator << (ostream &output, const Triangle &tri)
{
output<<"Triangle base: "<<tri.base<<" height: "<<tri.height;
output<<" area: "<<tri.area()<<endl;
return output;
}

Triangle operator + (const Triangle &other)
{
double newArea = area() + other.area();
double newBase = base>other.base? base:other.base;
double newHeight = 2 * newArea / newBase;


return Triangle(newBase,newHeight);
}
};

class Cone: public Circle
{
private:
double height,side;
public:
Cone(double r, double h, double l):Circle(r)
{

height = h;
side = l;
}
double volume() const
{
return PI * getRadius() * getRadius() * height / 3.0;
}
double area() const
{
return PI * getRadius() * side;
}
double getHeight() const
{
return height;
}

double getSide() const
{
return side;
}

friend ostream& operator << (ostream &output, const Cone &c)
{
output<<"Cone radius: "<<c.getRadius()<<" height: "<<c.getHeight();
output<<" side: "<<c.getSide()<<" area: "<<c.area()<<" volume: "<<c.volume()<<endl;
return output;
}

Cone operator + (const Cone &other)
{
double newArea = area() + other.area();
double newRadius = getRadius()>other.getRadius()? getRadius(): other.getRadius();
double newSide = newArea / (PI * newRadius);
double newVolume= volume() + other.volume();
double newHeight = newVolume * 3 / (PI * newRadius * newRadius);

return Cone(newRadius, newHeight, newSide);
}
};

class Sphere : public Circle
{
public:
Sphere(double rad):Circle(rad)
{
}
double volume() const
{
return 4/3.0 * PI * pow(getRadius(),3);
}

double area() const
{
return 4 * PI * pow(getRadius(),2);
}

friend ostream& operator << (ostream &output, const Sphere &s)
{
output<<"Sphere radius: "<<s.getRadius();
output<<" area: "<<s.area()<<" volume: "<<s.volume()<<endl;
return output;
}

Sphere operator + (const Sphere &other)
{
double newArea = area() + other.area();
double newRadius = sqrt(newArea/(4*PI));

return Sphere(newRadius);
}
};

class Node
{
private:
Shape *shape;
Node *next;
public:
Node()
{
shape = NULL;
next = NULL;
}
Node(Shape *s)
{
shape = s;
next = NULL;
}

Shape * getShape()
{
return shape;
}
void setShape (Shape *s)
{
shape = s;
}

void setNext(Node* n)
{

next = n;
}
Node* getNext()
{
return next;
}
};

class List
{
private:
Node* head;
public:
List()
{
head=NULL;
}
void add(Shape * s)
{
if(head==NULL)
{
head = new Node(s);
}
else
{
Node* p=head;
while(p->getNext()!=NULL)
{
p=p->getNext();
}

p->setNext(new Node(s));
}
}

void print()
{
Node *p=head;
Shape *s;
while(p!=NULL)
{
s=p->getShape();

if(typeid(*s)==typeid(Rectangle))
cout<<*dynamic_cast<Rectangle*>(s)<<endl;
else if(typeid(*s)==typeid(Cubic))
cout<<*dynamic_cast<Cubic*>(s)<<endl;
else if(typeid(*s)==typeid(Circle))
cout<<*dynamic_cast<Circle*>(s)<<endl;
else if(typeid(*s)==typeid(Sphere))
cout<<*dynamic_cast<Sphere*>(s)<<endl;
else if(typeid(*s)==typeid(Cone))
cout<<*dynamic_cast<Cone*>(s)<<endl;
else if(typeid(*s)==typeid(Triangle))
cout<<*dynamic_cast<Triangle*>(s)<<endl;

p = p->getNext();
}
}

Shape* get(int n)
{
Node *p=head;
int i=0;
while(i<n && p!=NULL)
{
p=p->getNext();
i++;
}
if(p==NULL)
return NULL;
else
return p->getShape();

}
~List()
{
Node *p=head,*q;
while(p!=NULL)
{
delete p->getShape();
q=p->getNext();
delete p;
p=q;
}
}

};
int main()
{
string command;
string type;
List list;
double n1,n2,n3;
Rectangle *r;
Circle *c;
Triangle *t;
Sphere *s;
Cubic *cb;
Cone *cn;
cout<<"Commands are: construct, add, print, quit"<<endl<<endl;
while(true)
{
cout<<"I: ";
cin>>command;
if(command=="construct")
{
cin>>type;
if(type=="Rectangle")
{
cin>>n1 >> n2;
r=new Rectangle(n1,n2);
list.add(r);
cout<<"O: "<<*r<<endl;
}
else if(type=="Cubic")
{
cin>>n1;
cb=new Cubic(n1);
list.add(cb);
cout<<"O: "<<*cb<<endl;
}
else if(type=="Circle")
{
cin>>n1;
c=new Circle(n1);
list.add(c);
cout<<"O: "<<*c<<endl;
}
else if(type=="Triangle")
{
cin>>n1>>n2;
t=new Triangle(n1,n2);
list.add(t);
cout<<"O: "<<*t<<endl;
}
else if(type=="Sphere")
{
cin>>n1;
s=new Sphere(n1);
list.add(s);
cout<<"O: "<<*s<<endl;
}
else if(type == "Cone")
{
cin>>n1>>n2>>n3;
cn=new Cone(n1,n2,n3);
list.add(cn);
cout<<"O: "<<*cn<<endl;
}
else
{
cout<<"O: Invalid shape!"<<endl;
}


}
else if(command=="add")
{
int idx1,idx2;
cin>>idx1>>idx2;
Shape *s1=list.get(idx1), *s2=list.get(idx2);
if(s1==NULL || s2==NULL)
{
cout<<"O: Index out of range!"<<endl;
}
else
{

if(typeid(*s1)==typeid(*s2))
{
if(typeid(*s1)==typeid(Rectangle))
{
Rectangle r=*(dynamic_cast<Rectangle*>(s1)) + (*(dynamic_cast<Rectangle*>(s2)));
cout<<r<<endl;
}
else if(typeid(*s1)==typeid(Triangle))
{
Triangle t=*(dynamic_cast<Triangle*>(s1)) + (*(dynamic_cast<Triangle*>(s2)));
cout<<t<<endl;
}
else if(typeid(*s1)==typeid(Circle))
{
Circle c=*(dynamic_cast<Circle*>(s1)) + (*(dynamic_cast<Circle*>(s2)));
cout<<c<<endl;

}
else if(typeid(*s1)==typeid(Cubic))
{
Cubic c=*(dynamic_cast<Cubic*>(s1)) + (*(dynamic_cast<Cubic*>(s2)));
cout<<c<<endl;

}
else if(typeid(*s1)==typeid(Cone))
{
Cone c=*(dynamic_cast<Cone*>(s1)) + (*(dynamic_cast<Cone*>(s2)));
cout<<c<<endl;

}
else if(typeid(*s1)==typeid(Sphere))
{
Sphere c=*(dynamic_cast<Sphere*>(s1)) + (*(dynamic_cast<Sphere*>(s2)));
cout<<c<<endl;

}
}
else
{
cout<<"O: Both should be of same type!"<<endl;
}
}

}
else if(command =="print")
{
cout<<"O: "<<endl;
list.print();
}
else if(command=="quit")
{
break;
}
else
{
cout<<"O: Invalid command"<<endl;
}

}
cout<<"Program terminated."<<endl;
}

output

Commands are: construct, add, print, quit

I: construct Rectangle 3 4
O: Rectangle    length: 3   breadth: 4   area: 12

I: construct Circle 5
O: Circle    radius: 5   area: 78.5

I: construct Cubic 4
O: Cubic    length: 4   area: 96   volume: 64

I: construct Triangle 8 5
O: Triangle    base: 8   height: 5   area: 20

I: construct Cone 5 6 7
O: Cone    radius: 5   height: 6   side: 7   area: 109.9   volume: 157

I: construct Sphere 7
O: Sphere    radius: 7   area: 615.44   volume: 1436.03

I: print
O:
Rectangle    length: 3   breadth: 4   area: 12

Circle    radius: 5   area: 78.5

Cubic    length: 4   area: 96   volume: 64

Triangle    base: 8   height: 5   area: 20

Cone    radius: 5   height: 6   side: 7   area: 109.9   volume: 157

Sphere    radius: 7   area: 615.44   volume: 1436.03

I: add 0 1
O: Both should be of same type!
I: construct Circle 9
O: Circle    radius: 9   area: 254.34

I: print
O:
Rectangle    length: 3   breadth: 4   area: 12

Circle    radius: 5   area: 78.5

Cubic    length: 4   area: 96   volume: 64

Triangle    base: 8   height: 5   area: 20

Cone    radius: 5   height: 6   side: 7   area: 109.9   volume: 157

Sphere    radius: 7   area: 615.44   volume: 1436.03

Circle    radius: 9   area: 254.34

I: add 1 6
Circle    radius: 10.2956   area: 332.84

I: quit
Program terminated.