code:
#include
using namespace std;
class Horse
{
public:
int age = 3;
string color = "white";
// 用普通的成员函数实现自定义类型相加
Horse horse_plus(Horse& h1) // 定义成员函数,实现Horse类型相加,返回Horse类型
{
Horse result;
result.age = this->age + h1.age;
result.color = this->color + h1.color;
return result;
}
// 通过成员函数重载+,只是系统给自定义了名称operator+,使用+运算符和调用operator+及horse_plus函数是一样的
Horse operator+ (Horse& h1)
{
Horse result;
result.age = this->age + h1.age;
result.color = this->color + h1.color;
return result;
}
};
int main()
{
Horse h1;
Horse h2;
h1.age = 10;
h2.age = 20;
h1.color = "brown";
h2.color = "white";
cout << "horse_plus, age:" << h1.horse_plus(h2).age << endl; // 使用了成员函数实现加法
cout << "horse_plus, color: " << h1.horse_plus(h2).color << endl; // 使用了成员函数实现加法
cout << "+, age: " << (h1 + h2).age << endl; // + 运算符重载
cout << "+, color: " << (h1 + h2).color << endl; // + 运算符重载
cout << "operator+, age: " << h1.operator+(h2).age << endl; // + 运算符重载调用的本质其实是成员函数调用
cout << "operator+, color : " << h1.operator+(h2).color << endl;// + 运算符重载调用的本质其实是成员函数调用
system("pause");
return 0;
}
result:
horse_plus, age:30
horse_plus, color: brownwhite
+, age: 30
+, color: brownwhite
operator+, age: 30
operator+, color : brownwhite
code:
#include
using namespace std;
class Horse
{
public:
int age = 3;
string color = "white";
};
// 通过全局函数重载+,系统给自定义了名称operator+,使用+运算符和调用operator+是一样的
Horse operator+ (Horse& h1, Horse& h2) // 全局函数重载,参数要传两个
{
Horse result;
result.age = h1.age + h2.age;
result.color = h1.color + h2.color;
return result;
}
int main()
{
Horse h1;
Horse h2;
h1.age = 10;
h2.age = 20;
h1.color = "brown";
h2.color = "white";
// h1 + h2相当于是operator+(h1, h2)
cout << "+, age: " << (h1 + h2).age << endl;
cout << "+, color: " << (h1 + h2).color << endl;
cout << "operator+, age: " << operator+(h1, h2).age << endl;
cout << "operator+, color : " << operator+(h1, h2).color << endl;
system("pause");
return 0;
}
result:
+, age: 30
+, color: brownwhite
operator+, age: 30
operator+, color : brownwhite
code:
#include
using namespace std;
class Horse
{
public:
int age = 3;
string color = "white";
Horse horse_plus(Horse& h1)
{
Horse result;
result.age = this->age + h1.age;
result.color = this->color + h1.color;
return result;
}
// 通过成员函数重载+,只是系统给自定义了名称operator+,和调用operator+及horse_plus函数是一样的。
Horse operator+ (Horse& h1)
{
Horse result;
result.age = this->age + h1.age;
result.color = this->color + h1.color;
return result;
}
// 运算符重载也可以实现函数重载
Horse operator+ (int num)
{
Horse result;
result.age = this->age + num;
return result;
}
};
int main()
{
Horse h1;
Horse h2;
h1.age = 10;
h2.age = 20;
h1.color = "brown";
h2.color = "white";
cout << "horse_plus, age:" << h1.horse_plus(h2).age << endl;
cout << "horse_plus, color: " << h1.horse_plus(h2).color << endl;
cout << "+, age: " << (h1 + h2).age << endl; // 运算符重载实现的函数重载
cout << "+, color: " << (h1 + h2).color << endl; // 运算符重载实现的函数重载
cout << "operator+, age: " << h1.operator+(h2).age << endl; // 运算符重载调用的本质是函数调用
cout << "operator+, color : " << h1.operator+(h2).color << endl;// 运算符重载调用的本质是函数调用
cout << "operator+2, age: " << (h1 + 100).age << endl; // 运算符重载实现的函数重载
system("pause");
return 0;
}
result:
horse_plus, age:30
horse_plus, color: brownwhite
+, age: 30
+, color: brownwhite
operator+, age: 30
operator+, color : brownwhite
operator+2, age: 110
在int,float等类型的加法运算中,是可以实现连加的,那么自定义类型的对象的连加如何实现?
code:
#include
using namespace std;
class Horse
{
public:
int age = 3;
string color = "white";
Horse& operator+ (Horse& h1) // 运算符+重载,返回引用
{
Horse result;
this->age = this->age + h1.age;
this->color = this->color + h1.color;
return *this; // this是对象本身的指针,*this是对象本身,返回值:自定义类型& 是对象本身的引用
}
};
int main()
{
Horse h1;
Horse h2;
Horse h3;
h1.age = 10;
h2.age = 20;
h3.age = 30;
h1.color = "black";
h2.color = "white";
h3.color = "brown";
cout << "+, age: " << (h1 + h2 + h3).age << endl;
cout << "h1.color: " << h1.color << endl;
cout << "+, color: " << (h1 + h2 + h3).color << endl;
system("pause");
return 0;
result:
+, age: 60
h1.color: blackwhitebrown
+, color: blackwhitebrownwhitebrown
}
code:
#include
using namespace std;
class Horse
{
public:
int age = 3;
string color = "white";
};
Horse& operator+ (Horse& h1, Horse& h2)
{
Horse result;
h1.age = h1.age + h2.age;
h1.color = h1.color + h2.color;
return h1;
}
int main()
{
Horse h1;
Horse h2;
Horse h3;
h1.age = 10;
h2.age = 20;
h3.age = 30;
h1.color = "black";
h2.color = "white";
h3.color = "brown";
cout << "+, age: " << (h1 + h2 + h3).age << endl;
cout << "h1.color: " << h1.color << endl;
cout << "+, color: " << (h1 + h2 + h3).color << endl;
system("pause");
return 0;
}
result:
+, age: 60
h1.color: blackwhitebrown
+, color: blackwhitebrownwhitebrown