设计带拷贝构造函数和析构函数的矩形类

描述

定义屏幕矩形类RECT,边和坐标轴平行,两个对角顶点坐标是其数据成员,int类型,私有;成员函数有:默认构造设置所有数据成员值为0、四参数构造函数设置两个顶点坐标、拷贝构造函数、set()设置两个顶点坐标、area()计算面积、perimeter()计算周长、showinfo()显示两个顶点信息、show()显示由“*”组成的矩形(宽是矩形的宽度,高是矩形的高度)、析构函数。成员函数均为公有,析构函数中显示两个顶点信息和字符串“deconstruct”。‪‬‪‬‪‬‪‬‪‬‮‬‭‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‫‬

编写主函数,使用RECT类定义一个矩形类对象a(默认构造),输入并设置矩形的两个顶点,显示矩形的静态成员信息,计算并显示矩形的周长和面积,显示由“*”组成的矩形;由刚输入的顶点向右平移2单位构造对象b,再由对象b构造对象c,显示矩形c的静态成员信息,计算并显示矩形c的周长和面积,显示由“*”组成的矩形c。主函数如下:‪‬‪‬‪‬‪‬‪‬‮‬‭‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‫‬

int main() {
	RECT a;
	double x1, x2, y1, y2;
	cin >> x1 >> y1 >> x2 >> y2;
	a.set(x1, y1, x2, y2);
	a.showinfo();
	cout << endl;
	cout << a.perimeter() << endl;
	cout << a.area() << endl;
	a.show();
	RECT b(x1 + 2, y1, x2 + 2, y2);
	RECT c(b);//拷贝构造函数
	c.showinfo();
	cout << endl;
	cout << c.perimeter() << endl;
	cout << c.area() << endl;
	c.show();
	return 0;
}

 输入

两个顶点的坐标,用空格隔开。‪‬‪‬‪‬‪‬‪‬‮‬‭‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‫‬

输出

见样例,其中最后三行是析构函数的输出,每行是一个对象的析构。析构函数不需调用。‪‬‪‬‪‬‪‬‪‬‮‬‭‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‫‬

输入输出示例

输入输出
示例 1
1 1 10 5
1 1 10 5
(1,1,10,5)
26
36
*********
*********
*********
*********
(3,1,12,5)
26
36
*********
*********
*********
*********
(3,1,12,5)deconstruct
(3,1,12,5)deconstruct
(1,1,10,5)deconstruct

参考答案如下

 

#include<iostream>
#include<cmath>
using namespace std;
class RECT{
	private:
	int x1,y1,x2,y2;
	public:
		RECT(){
			
		}
		RECT(int x1,int y1,int x2,int y2){
			this->x1=x1;
			this->y1=y1;
			this->x2=x2;
			this->y2=y2;
		}
		RECT(RECT &zll){
			x1=zll.x1;
			y1=zll.y1;
			x2=zll.x2;
			y2=zll.y2;
		}
		void set(double a1=0,double b1=0,double a2=0,double b2=0){
			this->x1=a1;
			this->y1=b1;
			this->x2=a2;
			this->y2=b2;
		}
		double area(){
			return abs(x2-x1)*abs(y2-y1);
		}
		double perimeter(){
			return (2*abs(x2-x1))+(2*abs(y2-y1));
		}
		void showinfo(){
			cout<<"("<<x1<<","<<y1<<","<<x2<<","<<y2<<")";
		}
		void show(){
			for(int i=0;i<(abs(y2-y1));i++){
				for(int j=0;j<(abs(x2-x1));j++){
					cout<<"*";
				}
				cout<<endl;
			}
		}
		~RECT(){
			cout<<"("<<x1<<","<<y1<<","<<x2<<","<<y2<<")"<<"deconstruct"<<endl;
		}
};
int main() {
	RECT a;
	double x1, x2, y1, y2;
	cin >> x1 >> y1 >> x2 >> y2;
	a.set(x1, y1, x2, y2);
	a.showinfo();
	cout << endl;
	cout << a.perimeter() << endl;
	cout << a.area() << endl;
	a.show();
	RECT b(x1 + 2, y1, x2 + 2, y2);
	RECT c(b);//拷贝构造函数
	c.showinfo();
	cout << endl;
	cout << c.perimeter() << endl;
	cout << c.area() << endl;
	c.show();
	return 0;
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

张立龙666

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值