为 Rectangle 类实现构造函数,拷贝构造函数,赋值操作符,析构函数。
- class Shape
- {
- int no;
- };
- class Point
- {
- int x;
- int y;
- };
- class Rectangle: public Shape
- {
- int width;
- int height;
- Point * leftUp;
- public:
- Rectangle(int width, int height, int x, int y);
- Rectangle(const Rectangle& other);
- Rectangle& operator=(const Rectangle& other);
- ~Rectangle();
- };
思考:
我将所有所需的类写在了.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;
}