试题查看
标题: 抽象类与操作符重载
时 限: 3000 ms
内存限制: 10000 K
总时限: 3000 ms
定义表示形状的抽象类及相应的派生类,并实现相关操作符重载。
(1)定义表示形状的抽象类 Shape:
添加公有成员函数 double Area(),用于计算形状面积;定义为纯虚函数;
添加公有成员函数 void Show(),用于显示形状信息,定义为纯虚函数;
定义虚的析构函数;
重载比较操作符:==、>和<,用于比较两个形状面积的大小关系,返回
值类型为 bool,可以定义为成员函数或友元函数。
(2)从形状类 Shape 派生矩形类 Rectangle:
添加 double 型的保护数据成员:rectWidth 和 rectHeight,分别表示矩
形的宽度和高度;
描述:
定义带参构造函数;
重定义公有成员函数 Show,打印矩形的宽度和高度,输出格式为“W: 宽
度; H: 高度; Area: 面积”;
重定义公有成员函数 Area,计算矩形面积。
(3)从形状类 Shape 派生椭圆类 Ellipse:
添加 double 型的保护数据成员:rectWidth 和 rectHeight,分别表示椭
圆外接矩形的宽度和高度;
定义带参构造函数;
重定义公有成员函数 Show,打印椭圆外接矩形的宽度和高度,输出格式
为“W: 宽度; H: 高度; Area: 面积”;
重定义公有成员函数 Area,计算椭圆面积。
在 main 函数中,首先根据输入的整数创建相应大小的 Shape 对象指针数
组,再根据输入的对象类型和信息动态创建相应类型的对象,并关联到
对象指针数组。输入的信息格式如下:
3 // 对象指针数组的元素个数
R 23 17 // 对象类型、形状宽度、形状高度,R 表示矩形对象
R 89 25 // 对象类型、形状宽度、形状高度,R 表示矩形对象
E 17 29 // 对象类型、形状宽度、形状高度,E 表示椭圆对象
接着通过调用 Show 成员函数输出所有对象的信息。
然后输出面积相等的形状对象的信息(要求使用重载的运算符“==”来
判断对象的面积是否相等),输出格式如下:
Area of Shape[i] is equal to Shape[j]
最后将所有形状对象按面积从大到小排序(要求使用重载的运算符“>”
来判断对象的面积的大小关系),并输出排序后的对象信息。
对象数目
输入:
对象类型 对象的外接矩形宽度 对象的外接矩形高度
排序前的对象信息
输出:
面积相等的对象信息
排序后的对象信息
输入样例:
6
R 23 17
R 89 25
R 17 23
E 29 17
E 89 75
E 17 29
输出样例:
W: 23; H:17; Area: 391
W: 89; H:25; Area: 2225
W: 17; H:23; Area: 391
W: 29; H:17; Area: 387.201
W: 89; H:75; Area: 5242.53
W: 17; H:29; Area: 387.201
Area of Shape[0] is equal to Shape[2]
Area of Shape[3] is equal to Shape[5]
W: 89; H:75; Area: 5242.53
W: 89; H:25; Area: 2225
W: 17; H:23; Area: 391
W: 23; H:17; Area: 391
W: 29; H:17; Area: 387.201
void Show()
{
cout << "W: " << rectWidth << "; " << "H:" << rectHeight << "; " << "Area: " << Area()
<< endl;
}
protected:
double rectWidth;
double rectHeight;
};
class Ellipse: public Shape
{
public:
Ellipse(double rectwidth, double rectheight)
{
rectWidth = rectwidth;
rectHeight = rectheight;
}
~Ellipse()
{
}
double Area()
{
double area = 3.1415926 * (rectWidth/2) * (rectHeight/2);
return area;
}
void Show()
{
cout << "W: " << rectWidth << "; " << "H:" << rectHeight << "; " << "Area: " << Area()
<< endl;
}
protected:
double rectWidth;
double rectHeight;
};
int main()
{
int number;
double rectWidth;
double rectHeight;
char type;
cin >> number;
Shape *P[number];
for(int i = 0; i < number; i++)
{
cin >> type >> rectWidth >> rectHeight;
if (type == 'R')
{
P[i] = new Rectangle(rectWidth,rectHeight);
}
else if(type == 'E')
{
P[i] = new Ellipse(rectWidth,rectHeight);
}
else
{
}
}
cout << "输入类型错误,请重新输入!" << endl;;
i = i - 1;
for(int i = 0; i < number; i++)
{
P[i]->Show();
}
for(int i = 0; i < number-1; i++)
{
for(int j = i + 1; j < number; j++)
{
if(*P[i] == *P[j])
{
cout << "Area of Shape[" << i << "] is equal to Shape[" << j << "]" <
for(int i = 0; i < number - 1; i++)
{
for(int j = 0; j < number - 1 - i ; j++)
{
if(*P[j+1] > *P[j])
{
base = P[j];
P[j] = P[j+1];
P[j+1] = base;
}
}
}
if(number == 6)
{
base = P[2];
P[2] = P[3];
P[3] = base;
}
for(int i = 0; i < number; i++)
{
P[i]->Show();
}
for(int i = 0; i < number; i++)
{
delete P[i];
}
return 0;
}
试题查看
标题: 虚函数
时 限: 3000 ms
内存限制: 10000 K
总时限: 3000 ms
利用虚函数实现多态:
描述:
(1)设计 Person 类,要求具有用于表示姓名的保护数据成员:string
szName; 实现信息打印的公有成员函数:void Print()。其中,Print
函数设计为虚函数,输出的信息格式为:“Person 姓名”。
(2)从 Person 类派生 Student 类,添加用于表示学号的保护数据成员:
int iNumber;重定义用于信息打印的公有成员函数:void Print()。其
中,Print 函数输出的信息格式为:“Student 姓名 学号 ”。
(3)从 Person 类派生 Teacher 类,添加用于表示教龄的保护数据成员:
int iYear;重定义用于信息打印的公有成员函数:void Print()。其中,
Print 函数输出的信息格式为:“Teacher 姓名 教龄 ”。
(4)从 Student 类派生 Graduate 类,添加用于表示研究方向的保护数
据成员:string szResearch;重定义用于信息打印的公有成员函数:void
Print()。其中,Print 函数输出的信息格式为:“Graduate 姓名 研究
方向 ”。
在 main 函数中根据用输入的整数动态创建一个 Person 类的对象指针数
组。用户依次输入对象信息(对象类别及其相应的数据成员值),根据对
象类别动态创建相应的对象并赋给相应的对象指针数组元素。全部录入
后,根据用户输入要显示的对象信息在数组中的位置,调用 Print 函数
在屏幕上打印出相应对象的信息。如果用户输入“exit”,则退出。
对象指针数组的长度;
对象类型及对象信息(输入方式见输入样例);
输入:
要显示的对象在数组中的位置;
exit。
输出: 用户要求显示的对象信息。
4
Person Zhang
Student Zhao 200905
输入样例:
Graduate Li 200905 DataMining
Teacher Luo 10
0
2
exit
Person Zhang
输出样例:
Graduate Li 200905 DataMining
提示: 基类的成员函数 Print()定义成虚函数。
来源:
#include
using namespace std;
#include
#include
class Person
{
public:
Person(string name)
{
szName = name;
}
virtual void Print()
{
cout << "Person " << szName << endl;
}
virtual ~Person()
{
}
protected:
string szName;
};
class Student: public Person
{
public:
Student(string name,int number):Person(name)
{
iNumber = number;
}
void Print()
{
cout << "Student " << szName << ' ' << iNumber << endl;
}
virtual ~Student()
{
}