类和对象——实验题一
作者:miaomiao
题目出自某高校实验作业
一、实验内容
编程示例:设计并测试一个矩形类
(Rectangle)。属性为矩形的左上角与右下角的坐标。
矩形水平放置。操作为计算矩形的周长和面积。
示例程序
#include<iostream>
#include<cmath>
using namespace std;
class Rectangle {
private:
double left, top;
double right, bottom;
public:
Rectangle(double l=0, double t=0, double r=0, double b=0);
~Rectangle(){}
void Assign(double l, double t, double r, double b);
void Show();
double Area();
double Perimeter();
};
Rectangle::Rectangle(double l, double t, double r, double b) {
left = l; top = t;
right = r; bottom = b;
}
void Rectangle::Assign(double l, double t, double r, double b) {
left = l; top = t;
right = r; bottom = b;
}
void Rectangle::Show() {
cout << "left-top point is (" << left << "," << top << ")" << '\n';
cout << "right-bottom point is (" << right << "," << bottom << ")" << '\n';
}
double Rectangle::Area() {
return fabs((right - left)*(bottom - top));
}
double Rectangle::Perimeter() {
return 2 * (fabs(right - left) + fabs(bottom - top));
}
int main() {
Rectangle rect;
rect.Show();
rect.Assign(100, 200, 300, 400);
rect.Show();
Rectangle rect1(0, 0, 200, 400);
rect1.Show();
rect1.Assign(100, 200, 300, 400);
rect1.Show();
cout << "面积" << rect.Area() << '\t' << "周长" << rect.Perimeter() << endl;
cout << "面积" << rect1.Area() << '\t' << "周长" << rect1.Perimeter() << endl;
return 0;
}
思考题
(1)将Rectangle(double l=0, double t=0, double r=0, double b=0);
改为
Rectangle(double l, double t, double r, double b);
程序仍能正确运行吗?为什么?
(2)注意成员函数 void Show()、double Area()、double Perimeter() 的使用,因为在以前如果需编写类似功能的一般函数是需要带参数(形参)的,而在此处作为类的成员函数又不需要带参数。思考为什么?
(3)理解 Assign(double l, double t, double r, double b);
函数的作用。
将 Rectangle(double l=0, double t=0, double r=0, double b=0);
改为:
Rectangle(double l, double t, double r, double b);
这时有人认为 Rectangle(double l, double t, double r, double b);
和 Assign(double l, double t, double r, double b);
的功能相同,那么 Assign 函数能否去掉呢?试一试,结果会怎样?
(4)请为矩形类补充复制构造函数,并加以测试。
二、题目解答
程序运行结果
(1)
不能,但是可对程序做更改使其再次正确运行。
将程序第42行改为 Rectangle rect(0, 0, 0, 0);
程序就能正确运行了。
结果与原始程序运行结果一致。
(2)
因为类的成员函数能直接使用本类中的数据成员。
(3)
改变矩形左上角和右下角的点坐标,来改变矩形的位置。
程序不能运行,因为构造函数只能在创建对象时使用。
(4)
将类的声明中加入 Rectangle(Rectangle &r);
如下。
class Rectangle {
private:
double left, top;
double right, bottom;
public:
Rectangle(double l, double t, double r, double b);
Rectangle(Rectangle &r);
~Rectangle(){}
void Assign(double l, double t, double r, double b);
void Show();
double Area();
double Perimeter();
};
并在函数定义中加入以下代码
Rectangle::Rectangle(Rectangle &r) {
left = r.left; top = r.top;
right = r.right; bottom = r.bottom;
}
而后在主函数中添加创建对象和函数调用的代码
int main() {
Rectangle rect(0, 0, 0, 0);
rect.Show();
rect.Assign(100, 200, 300, 400);
rect.Show();
Rectangle rect1(0, 0, 200, 400);
rect1.Show();
rect1.Assign(100, 200, 300, 400);
rect1.Show();
Rectangle rect2(rect1);
rect2.Show();
cout << "面积" << rect.Area() << '\t' << "周长" << rect.Perimeter() << endl;
cout << "面积" << rect1.Area() << '\t' << "周长" << rect1.Perimeter() << endl;
cout << "面积" << rect2.Area() << '\t' << "周长" << rect2.Perimeter() << endl;
return 0;
}
程序运行结果
三、更改后程序
#include<iostream>
#include<cmath>
using namespace std;
class Rectangle {
private:
double left, top;
double right, bottom;
public:
Rectangle(double l, double t, double r, double b);
Rectangle(Rectangle &r);
~Rectangle(){}
void Assign(double l, double t, double r, double b);
void Show();
double Area();
double Perimeter();
};
Rectangle::Rectangle(double l, double t, double r, double b) {
left = l; top = t;
right = r; bottom = b;
}
Rectangle::Rectangle(Rectangle &r) {
left = r.left; top = r.top;
right = r.right; bottom = r.bottom;
}
void Rectangle::Assign(double l, double t, double r, double b) {
left = l; top = t;
right = r; bottom = b;
}
void Rectangle::Show() {
cout << "left-top point is (" << left << "," << top << ")" << '\n';
cout << "right-bottom point is (" << right << "," << bottom << ")" << '\n';
}
double Rectangle::Area() {
return fabs((right - left)*(bottom - top));
}
double Rectangle::Perimeter() {
return 2 * (fabs(right - left) + fabs(bottom - top));
}
int main() {
Rectangle rect(0, 0, 0, 0);
rect.Show();
rect.Assign(100, 200, 300, 400);
rect.Show();
Rectangle rect1(0, 0, 200, 400);
rect1.Show();
rect1.Assign(100, 200, 300, 400);
rect1.Show();
Rectangle rect2(rect1);
rect2.Show();
cout << "面积" << rect.Area() << '\t' << "周长" << rect.Perimeter() << endl;
cout << "面积" << rect1.Area() << '\t' << "周长" << rect1.Perimeter() << endl;
cout << "面积" << rect2.Area() << '\t' << "周长" << rect2.Perimeter() << endl;
return 0;
}