For each of the following classes -create default constructor and parameterized
ID: 3728597 • Letter: F
Question
For each of the following classes
-create default constructor and parameterized constructors(calling parent(s) parameterized constructors where it applies
-Add accessor and mutator function for the attribute inherit to the class
Create a Base class called Shape2d
-with the protected floating point attribute area
-operator overload the + & - and operations to return the float respective to the area
Derive from the a Base class from called Shape2d called Rectangle
-with the additional floating point attributes length & width
Derive from the a Base class from called Shape2d called Circle
-with the additional floating point attributes radius
Create the Base class called Shape3d
-with the protected floating point attribute volume
-operator overload the *(have it add) & /(have it subtract) and operations to return the float respective to the volume
Derive from both Rectangle and Shape3d a class called Box
-with the additional floating point attributes height
Derive from both Circle and Shape3d a class called Cylinder
-with the additional floating point attributes height
For each of following classes [Circle, Rectangle, Box & Cylinder] create 3 instances each (using the parameterized constructor {no interactive input required}
Demonstrate the + & - operation overloading using the following combinations
Demonstrate the * & / operation overloading using the following combinations
Explanation / Answer
#include<iostream>
using namespace std;
class Shape2D
{
protected:
float area;
public:
Shape2D()
{
area = 0;
}
Shape2D(float a)
{
area = a;
}
float getArea()
{
return area;
}
friend Shape2D operator + (Shape2D &s1, Shape2D &s2)
{
Shape2D temp;
temp.area = s1.getArea()+ s2.getArea();
return temp;
}
};
class Rectangle : public Shape2D
{
float length, width;
public:
Rectangle()
{
length = width = 0;
}
Rectangle(float l, float w) : Shape2D(0.0f)
{
length = l;
width = w;
area = length * width;
}
};
class Circle : public Shape2D
{
float radius;
public:
Circle()
{
radius = 0;
}
Circle(float r) : Shape2D(0.0f)
{
radius = r;
area = 3.141f * r * r;
}
};
int main()
{
Rectangle rr1(2, 3), rr2(3, 4);
Circle c1(5), c2(3), c3;
Shape2D &r1 = rr1, &r2 = rr2, r3;
r3 = r1 + r2;
cout<<" Rectangle + Rectangle: "<<r3.getArea();
r1 = c1; r2 = c2;
r3 = r1 + r2;
cout<<" Circle + Circle: "<<r3.getArea();
r1 = rr1; r2 = c1;
r3 = r1 + r2;
cout<<" Circle + Rectangle: "<<r3.getArea();
}
Sample Output:
Rectangle + Rectangle: 18
Circle + Circle: 106.794
Circle + Rectangle: 157.05
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.