return (0) 2. Write the prototypes (where necessary) and the definitions of the
ID: 3733016 • Letter: R
Question
return (0) 2. Write the prototypes (where necessary) and the definitions of the following hierarchy: (total 78 points) Shape is an abstract base class. Define two private integer variables, center x and center y and a virtual method called area in that class. Shape should have only one constructor initializer that takes center_x and center y as arguments. It should also have two public methods, getCenter X) that returns center x and geiCenter Y that returns center y. Shape also contains function print (10 points) Define two derived classes, Rectangle and Cirele from Shape which use public inheritance. Also define two private double variables length and width in Rectangle and one private double variable, radius in Circle. Override method area of class Shape as well as print() Rectangle should have one default constructor without parameters and one constructor initializer Include a copy constructor in the class Rectangle as well. Include two get methods. (10 points) Circle has one default constructor without parameters and one constructor initializer. Override method area of class Shape as well as well as print(). Include a copy constructor in the class Circle. Include one get method. (10 points) A copy constructor is a constructor that takes an object of the same class as an argument and creates a new object by copying all the attributes of the object. The example below shows the copy constructor in a class called myClass. . class MyClass public: myClass (const myClass& obj) [ Code // Copy constructor (10 points for each copy constructors-total 20 points) Design two non-member functions print (do not change the name print) for class Reetangle and Circle. (for two non-member functions print - total 10 points) In the main() program, (20 points) The requirements for main() are specified in the partial codeExplanation / Answer
// C++ program to define the Shape , Rectangle and Circle classes
#include <iostream>
using namespace std;
// Shape class definition
class Shape{
private:
int center_x, center_y;
public:
Shape(int center_x,int center_y)
{
this->center_x = center_x;
this->center_y = center_y;
}
virtual double area();
int getCenterX() const
{
return center_x;
}
int getCenterY() const
{
return center_y;
}
void print()
{
cout<<" Center : ("<<center_x<<","<<center_y<<",)"<<endl;
}
};
// Rectangle class definition
class Rectangle:public Shape
{
private:
double length,width;
public:
Rectangle():Shape(0,0)
{
length =0;
width=0;
}
Rectangle(int center_x, int center_y,double length,double width) : Shape(center_x,center_y)
{
this->length = length;
this->width = width;
}
Rectangle(const Rectangle &rec ) : Shape(rec.getCenterX(),rec.getCenterY())
{
this->length = rec.length;
this->width = rec.width;
}
double getLength() const
{
return length;
}
double getWidth() const
{
return width;
}
double area()
{
return(length*width);
}
void print()
{
Shape::print();
cout<<"Length : "<<length<<endl;
cout<<"Width : "<<width<<endl;
cout<<"Area : "<<area()<<endl;
}
};
// Circle class definition
class Circle : public Shape
{
private:
double radius;
public:
Circle():Shape(0,0)
{
radius =0;
}
Circle(int center_x, int center_y, double radius) : Shape(center_x,center_y)
{
this->radius = radius;
}
Circle(const Circle &circle):Shape(circle.getCenterX(),circle.getCenterY())
{
radius = circle.radius;
}
double area()
{
return((22*radius*radius)/7);
}
void print()
{
Shape::print();
cout<<"Radius : "<<radius<<endl;
cout<<"Area : "<<area()<<endl;
}
double getRadius() const
{
return radius;
}
};
// non-member print functions
void print(Rectangle rectangle)
{
cout<<"Center : ("<<rectangle.getCenterX()<<","<<rectangle.getCenterY()<<")"<<endl;
cout<<"Length : "<<rectangle.getLength() <<endl;
cout<<"Width : "<<rectangle.getWidth() <<endl;
cout<<"Area : "<<rectangle.area()<<endl;
}
void print(Circle circle)
{
cout<<"Center : ("<<circle.getCenterX()<<","<<circle.getCenterY()<<")"<<endl;
cout<<"Radius : "<<circle.getRadius() <<endl;
cout<<"Area : "<<circle.area()<<endl;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.