Boolean第二周作业

为 Rectangle 类实现构造函数,拷贝构造函数,赋值操作符,析构函数。


  1. class Shape
  2. {                  
  3.    int no;
  4. };             
  5. class Point
  6. {
  7.    int x;
  8.    int y;
  9. };             
  10. class Rectangle: public Shape
  11. {
  12.    int width;
  13.    int height;
  14.    Point * leftUp;
  15. public:
  16.    Rectangle(int width, int height, int x, int y);
  17.    Rectangle(const Rectangle& other);
  18.    Rectangle& operator=(const Rectangle& other);
  19.    ~Rectangle();        
  20. };

思考:
我将所有所需的类写在了.h 中。并将调用函数写在.cpp 中。这样方便日后对本例功能完善或改变。

class Shape用不到,所以不用处理
class Point 应为后面会指针深度cope所以必须要有构造函数

public:
		Point(int x1, int y1) : x(x1), y(y1)    {  }    //  构造函数,支持指针类型深度拷贝  
		int get_x() const { return x; }
		int get_y() const { return y; }
	};

在class Rectangle:public shape 中,因为后期要获取长宽和指针所指的值,所以需添加函数返回其值,设计位成员函数即可,因为复杂度低

int get_width() const { return width; }
		int get_height() const { return height; }
		Point* get_leftUp() const { return leftUp; }
下面为各个类的实现

构造函数的实现
inline Rectangle::Rectangle(int width1, int height1, int x1, int y1) : width(width1), height(height1), leftUp(new Point(x1, y1))    //深度拷贝,需要Point有构造函数  
	{
		;

因为有指针的创建,所以要用析构函数来释放空间


inline Rectangle::~Rectangle()
	{
		delete[] leftUp;
	}

拷贝构造的实现

inline Rectangle::Rectangle(const Rectangle& other) : leftUp(new Point(other.leftUp->get_x(), other.leftUp->get_y()))     //深度拷贝,需要Point有构造函数  
	{
		width = other.width;
		height = other.height;
	}

拷贝赋值的实现

inline Rectangle& Rectangle::operator=(const Rectangle& other)              //拷贝赋值函数  
	{
		if (this == &other)     //重复自我赋值  
		{
			return *this;
		}

		delete this->leftUp;
		this->leftUp = new Point(other.leftUp->get_x(), other.leftUp->get_y());
		this->width = other.width;
		this->height = other.height;
		return *this;
	}

对cout的实现

#include <iostream>
	using namespace std;

	ostream& operator<<(ostream& os, Rectangle& str)
	{

	
		return os << '(' << str.get_height() << ',' << str.get_width() << ',' << str.get_leftUp()->get_x() << ',' << str.get_leftUp()->get_y() << ')' << endl;
	}

主函数的类的调用

#include<iostream>  
#include"Rectangle.h"  


using namespace std;



int main()
{
	Rectangle r1(1, 1, 1, 1);     //构造函数定义法  
	Rectangle r2(2, 2, 1, 1);      //构造函数定义法  
	Rectangle r3(r2);      //拷贝构造函数定义法  
	Point p1(1, 2);        //构造函数定义法  

	//cout << r1.get_height();  

	cout << "r1=" << r1;
	cout << "r2=" << r2;
	cout << "r3=" << r3;

	r1 = r3;

	cout << "r1=r3(拷贝赋值测试)" << endl;
	cout << "r1=" << r1;                        //拷贝赋值测试  


	          
	return 0;
}

下面为完整的代码

#ifndef __RECTANGLE_H__  
#define __RECTANGLE_H__  



	class Shape
	{
		int no;
	};
	class Point
	{
	private:
		int x;
		int y;
	public:
		Point(int x1, int y1) : x(x1), y(y1)    {  }    //  构造函数,支持指针类型深度拷贝  
		int get_x() const { return x; }
		int get_y() const { return y; }
	};
	class Rectangle : public Shape
	{
	private:
		int width;
		int height;
		Point * leftUp;
	public:
		Rectangle(int width1, int height1, int x1, int y1);         //构造函数    
		Rectangle(const Rectangle& other);                          //拷贝构造函数  
		Rectangle& operator=(const Rectangle& other);               //拷贝赋值函数  
		~Rectangle();                                               //析构函数 只声明不定义会出无法解析的错误  
		int get_width() const { return width; }
		int get_height() const { return height; }
		Point* get_leftUp() const { return leftUp; }
	};

	inline Rectangle::Rectangle(int width1, int height1, int x1, int y1) : width(width1), height(height1), leftUp(new Point(x1, y1))    //深度拷贝,需要Point有构造函数  
	{
		;
	}

	inline Rectangle::~Rectangle()
	{
		delete[] leftUp;
	}

	inline Rectangle::Rectangle(const Rectangle& other) : leftUp(new Point(other.leftUp->get_x(), other.leftUp->get_y()))     //深度拷贝,需要Point有构造函数  
	{
		width = other.width;
		height = other.height;
	}

	inline Rectangle& Rectangle::operator=(const Rectangle& other)              //拷贝赋值函数  
	{
		if (this == &other)     //重复自我赋值  
		{
			return *this;
		}

		delete this->leftUp;
		this->leftUp = new Point(other.leftUp->get_x(), other.leftUp->get_y());
		this->width = other.width;
		this->height = other.height;
		return *this;
	}
#include <iostream>
	using namespace std;

	ostream& operator<<(ostream& os, Rectangle& str)
	{

	
		return os << '(' << str.get_height() << ',' << str.get_width() << ',' << str.get_leftUp()->get_x() << ',' << str.get_leftUp()->get_y() << ')' << endl;
	}



#endif  
#include<iostream>  
#include"Rectangle.h"  


using namespace std;



int main()
{
	Rectangle r1(1, 1, 1, 1);     //构造函数定义法  
	Rectangle r2(2, 2, 1, 1);      //构造函数定义法  
	Rectangle r3(r2);      //拷贝构造函数定义法  
	Point p1(1, 2);        //构造函数定义法  

	//cout << r1.get_height();  

	cout << "r1=" << r1;
	cout << "r2=" << r2;
	cout << "r3=" << r3;

	r1 = r3;

	cout << "r1=r3(拷贝赋值测试)" << endl;
	cout << "r1=" << r1;                        //拷贝赋值测试  


	          
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值