C++ help! Create classes for a Shape, Circle, and Rectangle. All shapes have a n
ID: 3914661 • Letter: C
Question
C++ help!
Create classes for a Shape, Circle, and Rectangle. All shapes have a name and color and they take up some area. However, a circle's area is dependent upon its radius, while a rectangle's area is dependent on a length and width. You are to create these classes with the correct attributes in the correct class, with mutators and accessors for all the attributes, You are to create these classes with the correct attributes in the correct class, with mutators and accessors for all the attributes. For your design be certain to answer these questions. What data members will you need in each class? What functions will you need in each class? Are the functions the same or different? Which function will you use to create the polymorphism?Explanation / Answer
class Shape{
protected:
char[] name;
char[] color;
int area;
public :
char[] getName(){
return name;
}
void setName(char[] n){
name = n;
}
char[] getColor(){
return color;
}
void setColor(char[] c){
color = c;
}
int getArea(){
return area;
}
void setArea(int a){
area = a;
}
Shape(){
area=0;
}
}
class Rectangle: public Shape{
protected:
int width;
int height;
public:
int getWidth(){
return width;
}
void setWidth(int w){
width = w;
}
int getHeight(){
return height;
}
void setHeight(int h){
height = h;
}
void setArea(){
area = width*height;
}
}
class Circle: public Shape{
protected:
int radius;
public :
int getRadius(){
return radius;
}
void setRadius(int r){
radius = r;
}
void setArea(){
area = 3.14*radius*radius;
}
}
//Data members for shape class will be name, color and area.
//Rectangle will have width and height, and Circle will have radius.
//Functions would be getters and setters for datamembers, and setArea() will be different for different
//classes.
//setArea function is different while the rest is same. Since, different defination for setArea(), it follows
//polymorphism. All The Best.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.