版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/FAKER_0X0/article/details/89309555
- 成员函数方法
- 友元函数方法
下例重载 + - 前置++ 后置++ 前置-- 后置-- << >> 8种运算符:
//运算符重载
#include <iostream>
using namespace std;
class Complex
{
public:
Complex(int a = 0,int b = 0)
{
this->a = a;
this->b = b;
}
friend Complex operator+(Complex& c1,Complex& c2);
friend Complex& operator++(Complex& c1);
friend Complex operator++(Complex& c1,int);
friend ostream& operator<<(ostream& out,Complex& c1);
friend istream& operator>>(istream& in,Complex& c1);
//成员函数重载-
Complex operator-(Complex& c2)
{
Complex tmp(this->a-c2.a,this->b-c2.b);
return tmp;
}
//成员函数重载前置--
Complex& operator--() //返回引用,即对象本身
{
this->a--;
this->b--;
return *this;
}
//成员函数重载后置--
Complex operator--(int)
{
Complex tmp = *this;
this->a--;
this->b--;
return tmp;
}
void printfC()
{
cout << a << "+" << b << "i" << endl;
}
private:
int a;
int b;
};
//全局函数重istream& operator>>(istream& in,Complex& c1)载+运算符
Complex operator+(Complex& c1,Complex& c2)
{
Complex tmp(c1.a+c2.a,c1.b+c2.b);
return tmp;
}
//全局函数重载前置++
//当没有定义局部类对象时应该返回引用??
//注意:前置++需要返回引用,因为重载自加运算符后可以返回对象的引用,
//以方便在表达式中"连续使用"。而后置++返回的不是引用,所以不能进行连续使用。
Complex& operator++(Complex& c1) //返回引用,即对象本身
{
c1.a++;
c1.b++;
return c1;
}
//全局函数重载后置++
Complex operator++(Complex& c1,int) //占位参数int区分前置和后置
{
Complex tmp = c1;
c1.a++;
c1.b++;
return tmp;
}
//<<和>>运算符只能由友元函数重载,因为程序员拿不到cout和cin类的源代码
//实现链式编程(即同时输出或输入多个数据),要返回类对象的引用
//友元函数重载<<
ostream& operator<<(ostream& out,Complex& c1) //重点注意机制$$
{
out << c1.a << "+" << c1.b << "i" << endl;
return out; //注意基本类型输出中cout是返回值(本体),则这里是out
}
//友元函数重载>>
istream& operator>>(istream& in,Complex& c1)
{
in >> c1.a >> c1.b;
return in;
}
int main()
{
Complex c1(1,2),c2(3,4);
Complex c3 = c1+c2;
c3.printfC();
Complex c4 = c1-c2;
c4.printfC();
++c1;
c1.printfC();
--c1;
c1.printfC();
c2++;
c2.printfC();
c2--;
c2.printfC();
cout << "test << and >>" << endl;
cout << c1;
Complex c5;
cin >> c5;
c5.printfC();
return 0;
}
重点:
- 前置++需要返回引用,因为重载自加运算符后可以返回对象的引用,以方便在表达式中"连续使用"。而后置++返回的不是引用,所以不能进行连续使用。
- 占位参数int区分前置和后置 。
- <<和>>运算符只能由友元函数重载,因为程序员拿不到cout和cin类的源代码,实现链式编程(即同时输出或输入多个数据),要返回类对象的引用。
下例重载运算符 = (VS编译)
//浅拷贝与深拷贝问题;重载运算符=
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
using namespace std;
//定义Teacher类
class Teacher
{
public:
Teacher(int _id = 0,const char *_name = "lxn") // 这里加const则不会出现warning
{
cout << "调用构造函数" << endl;
id = _id;
//复制字符串的内容,最后有空字符‘/0’
int len = strlen(_name);
name = new char[len + 1]; //动态分配内存
strcpy(name, _name);
}
//重载=运算符,有字符指针
Teacher& operator=(Teacher& obj)
{
//1 先释放旧内存
if (this->name != NULL)
{
delete [] name;
}
//2 分配新的内存
this->id = obj.id;
int len = strlen(obj.name);
this->name = new char[len + 1];
strcpy(this->name, obj.name);
return *this;
}
void printT()
{
cout << "id: " << id << endl;
//cout << "name: " << *name << endl; //输出第一个字符的值
cout << "name: " << name << endl; //输出字符串的值
}
~Teacher()
{
if (name != NULL)
delete [] name; //释放占用的内存
name = NULL;
cout << "调用析构函数释放内存" << endl;
}
private:
int id;
char *name;
};
int main()
{
Teacher t1(25, "lvxiaoning");
t1.printT();
Teacher t2(20, "noname");
Teacher t3;
t3 = t2 = t1;
t3.printT();
return 0;
}
结论:
1 先释放旧的内存
2 返回一个引用
3 =操作符 从右向左