参考示例:
class Shape{
protected:
string name,color;
public:
Shape(string n="",string c=""):name(n),color(c){
cout<<"create Shape: "<<name<<endl;
}
string getName(){return name;}
string getColor(){return color;}
virtual double getArea(){return 0;}
virtual double getPerimeter(){return 0;}
virtual void draw(){
cout<<"draw Shape, color: "<<color<<endl;
}
virtual ~Shape(){
cout<<"destroy Shape: "<<name<<endl;
}
};
class Square:public Shape{
int l;
public:
Square(string n,string c,int ll):Shape(n,c),l(ll){
cout<<"create Square: "<<name<<endl;
}
double getArea(){return l*l;}
double getPerimeter(){return 4*l;}
void draw(){
cout<<"draw Square, color: "<<color<<", length: "<<l<<endl;
}
~Square(){
cout<<"destroy Square: "<<name<<endl;
}
};
class Rect:public Shape{
int w,h;
public:
Rect(string n,string c,int ww,int hh):Shape(n,c),w(ww),h(hh){
cout<<"create Rect: "<<name<<endl;
}
double getArea(){return w*h;}
double getPerimeter(){return 2*(w+h);}
void draw(){
if(w==h)
cout<<"draw Square, color: "<<color<<", length: "<<h<<endl;
else
cout<<"draw Rect, color: "<<color<<", width: "<<w<<", height: "<<h<<endl;
}
~Rect(){
cout<<"destroy Rect: "<<name<<endl;
}
};
形状v2(第二部分)
分数 5
作者 向训文
单位 惠州学院
该题的Shape类与上一题一样。
Shape类用于描述形状,要求如下:- 包含两个数据成员:形状的名称、形状的颜色
- 构造函数,按规定格式输出形状的名称
- 析构函数,按规定格式输出形状的名称
- 成员函数
getName,返回该形状的名称 - 成员函数
getColor,返回该形状的颜色 - 成员函数
getArea,返回该形状的面积,默认返回0 - 成员函数
getPerimeter,返回该形状的周长,默认返回0 - 成员函数
draw,用于绘制图形,按规定格式输出形状的颜色
Square类用于描述正方形,继承自Shape类- 成员函数
draw,用于绘制图形,按规定格式输出正方形的颜色、边长
- 成员函数
Rect类用于描述长方形,继承自Shape类- 成员函数
draw,用于绘制图形,如果宽、高相等,按规定格式输出边长,否则按规定格式输出宽、高
- 成员函数
请完善Shape、Square、Rect三个类的定义。
裁判测试程序样例:
#include <iostream>
#include <string>
using namespace std;
// 请将代码填写在这里
void display(Shape *p) {
cout << "name: " << p->getName() << endl
<< "area: " << p->getArea() << endl
<< "perimeter: " << p->getPerimeter() << endl;
}
int main() {
Shape *p;
int choice;
int length;
int width, height;
string color;
cin >> choice; // 输入测试序号
cin >> color; // 输入形状的颜色
switch (choice) {
case 1:
// 测试Shape类
p = new Shape("1-Shape", color);
display(p);
p->draw();
delete p;
break;
case 2:
// 测试Square类
cin >> length; // 正方形的边长
p = new Square("2-Square", color, length);
display(p);
p->draw();
delete p;
break;
case 3:
// 测试Rect类
cin >> width >> height; // 长方形的宽和高
p = new Rect("3-Rect", color, width, height);
display(p);
p->draw();
delete p;
break;
default:
break;
}
return 0;
}
测试1: Shape类
输入序号1、形状的颜色:
1
red
输出样例:
第1行是构造函数的输出;第2、3、4行是display函数的输出;第5行是draw函数的输出;第6行是析构函数的输出:
create Shape: 1-Shape
name: 1-Shape
area: 0
perimeter: 0
draw Shape, color: red
destroy Shape: 1-Shape
测试2: Square类
输入测试序号2、正方形的颜色、边长:
2
red
3
输出样例:
第1、2行是构造函数的输出;第3、4、5行是display函数的输出;第6行是draw函数的输出;第7、8行是析构函数的输出:
create Shape: 2-Square
create Square: 2-Square
name: 2-Square
area: 9
perimeter: 12
draw Square, color: red, length: 3
destroy Square: 2-Square
destroy Shape: 2-Square
测试3:Rect类(宽、高不相等的情况)
输入测试序号3、长方形的颜色、宽、高。例如:
3
red
3 4
输出样例:
第1、2行是构造函数的输出;第3、4、5行是display函数的输出;第6行是draw函数的输出;第7、8行是析构函数的输出。例如:
create Shape: 3-Rect
create Rect: 3-Rect
name: 3-Rect
area: 12
perimeter: 14
draw Rect, color: red, width: 3, height: 4
destroy Rect: 3-Rect
destroy Shape: 3-Rect
测试4:Rect类(宽、高相等的情况)
输入测试序号3、长方形的颜色、宽、高。例如:
3
red
3 3
输出样例:
第1、2行是构造函数的输出;第3、4、5行是display函数的输出;第6行是draw函数的输出;第7、8行是析构函数的输出。例如:
create Shape: 3-Rect
create Rect: 3-Rect
name: 3-Rect
area: 9
perimeter: 12
draw Square, color: red, length: 3
destroy Rect: 3-Rect
destroy Shape: 3-Rect
3808


&spm=1001.2101.3001.11974&articleId=171899228&d=1&t=3&u=33c69c5250e54ae2b34bb99f21b14432)

被折叠的 条评论
为什么被折叠?



