思维导图
深拷贝和浅拷贝
1> 如果类中有指针成员时,如果没有显性的定义拷贝构造和拷贝赋值函数,系统默认提供的都只能实现浅拷贝,需要显性定义出深拷贝函数,为了完成指针成员的独立赋值,如果类中没有指针成员,使用系统提供的浅拷贝函数就可以
2> 深拷贝指的是,两个类对象中的指针成员指向不同的空间,但是空间内存的值是相同的
3> 浅拷贝指的是,两个类对象中的指针成员指向同一片空间,在析构时会发生对内存空间的二次释放问题,一个类对象中指针成员指向空间的值修改过也会导致另一个类对象的也被修改
运算符重载
#include <iostream>
using namespace std;
class Person
{
private:
int age;
int weight;
public:
//无参构造
Person():age(18),weight(100){}
//有参构造
Person(int age,int weight)
{
this->age = age;
this->weight = weight;
}
/*
//拷贝构造函数
Person(Person &other)
{
age = other.age;
p = new int(*(other.p));
cout << "拷贝构造函数" << endl;
}
//拷贝赋值函数
Person &operator = (Person &other)
{
if(this != &other)
{
age = other.age;
*p = *(other.p);
cout << "拷贝赋值函数" << endl;
}
return *this;
}
//析构函数
~Person()
{
delete p;
p = nullptr;
cout << "析构函数" << endl;
}
*/
void show()
{
cout << "age = " << age << " weight = " << weight << endl;
}
//赋值运算符
Person operator =(const Person &a)
{
Person temp;
temp.age = a.age;
temp.weight = a.weight;
return temp;
}
Person &operator +=(const Person &a)
{
this->age = this->age + a.age;
this->weight = this->weight + a.weight;
cout << "******* += *******" << endl;
return *this;
}
Person &operator -=(const Person &a)
{
this->age = this->age - a.age;
this->weight = this->weight - a.weight;
cout << "******* -= *******" << endl;
return *this;
}
Person &operator *=(const Person &a)
{
this->age = this->age * a.age;
this->weight = this->weight * a.weight;
cout << "******* *= *******" << endl;
return *this;
}
Person &operator /=(const Person &a)
{
this->age = this->age / a.age;
this->weight = this->weight / a.weight;
cout << "******* /= *******" << endl;
return *this;
}
Person &operator %=(const Person &a)
{
this->age = this->age % a.age;
this->weight = this->weight % a.weight;
cout << "******* %= *******" << endl;
return *this;
}
//关系运算符
bool operator &&(Person &a)
{
cout << "******* && *******" << endl;
return this->weight && a.weight;
}
bool operator ||(Person &a)
{
cout << "******* || *******" << endl;
return this->weight || a.weight;
}
bool operator <(Person &p2)
{
cout << "******* < *******" << endl;
return this->weight < p2.weight;
}
bool operator <=(Person &p2)
{
cout << "******* <= *******" << endl;
return this->weight <= p2.weight;
}
bool operator >(Person &p2)
{
cout << "******* > *******" << endl;
return this->weight > p2.weight;
}
bool operator >=(Person &p2)
{
cout << "******* >= *******" << endl;
return this->weight >= p2.weight;
}
bool operator ==(Person &a)
{
cout << "******* == *******" << endl;
return this->weight == a.weight;
}
bool operator !=(Person &p2)
{
cout << "******* != *******" << endl;
return this->weight != p2.weight;
}
//算数运算符
Person operator *(Person &a)
{
Person temp;
temp.age = this->age * a.age;
temp.weight = this->weight * a.weight;
cout << "******* * *******" << endl;
return temp;
}
Person operator /(Person &a)
{
Person temp;
temp.age = this->age / a.age;
temp.weight = this->weight / a.weight;
cout << "******* / *******" << endl;
return temp;
}
Person operator %(Person &a)
{
Person temp;
temp.age = this->age % a.age;
temp.weight = this->weight % a.weight;
cout << "******* % *******" << endl;
return temp;
}
Person operator +(Person &a)
{
Person temp;
temp.age = this->age + a.age;
temp.weight = this->weight + a.weight;
cout << "******* + *******" << endl;
return temp;
}
Person operator -(Person &a)
{
Person temp;
temp.age = this->age - a.age;
temp.weight = this->weight - a.weight;
cout << "******* - *******" << endl;
return temp;
}
bool operator !()
{
cout << "******* ! *******" << endl;
return !this->weight;
}
Person operator ~()
{
Person temp;
temp.age = ~this->age;
temp.weight = ~this->weight;
cout << "******* ~ *******" << endl;
return temp;
}
Person &operator ++()//前自增
{
this->age++;
this->weight++;
cout << "******* 前++ *******" << endl;
return *this;
}
Person &operator ++(int)//后自增
{
static Person temp;
temp.age = this->age++;
temp.weight = this->weight++;
cout << "******* 后++ *******" << endl;
return temp;
}
Person &operator --()//前自减
{
this->age--;
this->weight--;
cout << "******* 前-- *******" << endl;
return *this;
}
Person &operator --(int)//后自减
{
static Person temp;
temp.age = this->age--;
temp.weight = this->weight--;
cout << "******* 后-- *******" << endl;
return temp;
}
Person operator -()//负号
{
static Person temp;
temp.age = -this->age;
temp.weight = -this->weight;
cout << "******* - *******" << endl;
return temp;
}
};
int main()
{
/*
//-------无参构造-------
Person p1;
p1.show();
//-------有参构造-------
int *num = new int(100);
Person p2(10, 100);
p2.show();
//-------拷贝构造函数-------
Person p3 = p1;
p3.show();
//-------拷贝赋值函数-------
Person p4;
p4 = p1;
p4.show();
//-------析构函数-------
*/
Person p1(18,100);
Person p2(20,150);
Person p3;
cout << (p1<=p2) << endl;
cout << endl;
cout << (!p1) << endl;
cout << endl;
p3 = ~p1;
p3.show();
cout << endl;
cout << "自加前: ";
p1.show();
p3 = p1++;
cout << "自加后: ";
p1.show();
p3.show();
cout << endl;
cout << "自加前: ";
p1.show();
p3 = ++p1;
cout << "自加后: ";
p1.show();
p3.show();
cout << endl;
return 0;
}