加号运算符重载: 重载加号运算符,让 p3 = p1 + p2 改成 p3.mage = p1.mage + p2.mage 实现两个数据成员的相加。
告诉编译器,两个类中的数据成员应该怎么相加。
成员函数相加 +号运算符重载 成员函数 二元
#include <iostream> #include <string>
using namespace std;
class Person { public: int m_x; int m_y; Person(){}; Person(int x, int y) :m_x(x), m_y(y) {}
Person operator + (Person &p) { Person tmp; tmp.m_x = this->m_x + p.m_x; tmp.m_y = this->m_y + p.m_y; return tmp; } };
int main(int argc, char *argv[]) { Person p1(10, 10); Person p2(20, 20);
Person p3 = p1 + p2; cout << "p3 m_x = > " << p3.m_x << endl; cout << "p3 m_y = > " << p3.m_y << endl;
system("pause"); return 0; }
|
全局函数相加,实现运算符重载
#include <iostream> #include <string>
using namespace std;
class Person { public: int m_x; int m_y; Person(){}; Person(int x, int y) :m_x(x), m_y(y) {} };
Person operator +(Person &p1, Person &p2) { Person tmp; tmp.m_x = p1.m_x + p2.m_x; tmp.m_y = p1.m_y + p2.m_y; return tmp; }
int main(int argc, char *argv[]) { Person p1(10, 30); Person p2(20, 50);
Person p3 = p1 + p2; cout << "p3 m_x = > " << p3.m_x << endl; cout << "p3 m_y = > " << p3.m_y << endl;
system("pause"); return 0; }
|
左移运算符重载: 使用 << 重载左移运算符,让cout 直接输出两个变量。重载左移运算符不可以写成成员函数,只能写全局运算符。
#include <iostream> #include <string>
using namespace std;
class Person { public: int m_x; int m_y; Person(){}; Person(int x, int y) :m_x(x), m_y(y) {} };
ostream& operator << (ostream &cout, Person &ptr) { cout << "m_x = " << ptr.m_x << " ----> " << "m_y = " << ptr.m_y << endl; return cout; }
int main(int argc, char *argv[]) { Person p1(10, 30); Person p2(20, 10);
cout << p1 << endl; cout << p2 << endl;
system("pause"); return 0; }
|
使用友元函数,访问私有的数据。
#include <iostream> #include <string>
using namespace std;
class Person {
friend ostream& operator<<(ostream &cout, Person &ptr); private: int m_x; int m_y;
public: Person(){}; Person(int x, int y) :m_x(x), m_y(y) {} };
ostream& operator << (ostream &cout, Person &ptr) { cout << "m_x = " << ptr.m_x << " ----> " << "m_y = " << ptr.m_y << endl; return cout; }
int main(int argc, char *argv[]) { Person p1(10, 30); Person p2(20, 10);
cout << p1 << endl; cout << p2 << endl;
system("pause"); return 0; }
|
前置/后置运算符的重载:
#include <iostream> #include <string>
using namespace std;
class MyInteger { friend ostream& operator<<(ostream& cout, MyInteger & myInt);
public: int m_count; public: MyInteger() { m_count = 0; }
MyInteger& operator ++ () { this->m_count++; return *this; } MyInteger operator ++ (int) { MyInteger tmp = *this; m_count++; return tmp; } };
ostream& operator<<(ostream& cout, MyInteger & myInt) { cout << myInt.m_count; return cout; }
int main(int argc, char *argv[]) { MyInteger myInt;
cout << ++myInt << endl; cout << myInt++ << endl;
cout << ++(++myInt) << endl;
system("pause"); return 0; }
|
指针运算符重载(智能指针) 用来托管自定义的对象,让对象可以自动的释放数据,
当我们使用一个对象结束以后,无需手动释放堆空间,智能指针会帮助我们完成这个过程。
#include <iostream> #include <string>
using namespace std;
class Student { public: char *m_name; int m_age;
public: Student(char *name, int age) { this->m_name = name; this->m_age = age; } void Print() { cout << "Name: " << this->m_name << endl; cout << "Age: " << this->m_age << endl; } };
class Smart_Pointer { private: Student *ptr; public: Smart_Pointer(Student *ptr) { this->ptr = ptr; }
Student * operator -> () { return this->ptr; }
Student & operator * () { return *this->ptr; }
~Smart_Pointer() { if (this->ptr != NULL) { delete this->ptr; this->ptr = NULL; } } };
int main(int argc, char *argv[]) { Student *stu = new Student("lyshark", 10); stu->Print(); delete stu;
Smart_Pointer ptr(new Student("lyshark", 10)); ptr->Print(); (*ptr).Print();
system("pause"); return 0; }
|
赋值运算符重载: 我们将等于号进行重载,实现对类中数据成员的赋值拷贝。
#include <iostream> #include <string>
using namespace std;
class Student { public: int m_uid; char *m_name; public: Student(int uid, char *name) { this->m_uid = uid; this->m_name = new char[strlen(name) + 1]; strcpy(this->m_name, name); } Student& operator = (const Student &ptr) { if (this->m_name != NULL) { this->m_uid = 0; delete[] this->m_name; this->m_name = NULL; } this->m_name = new char[strlen(ptr.m_name) + 1]; strcpy(this->m_name, ptr.m_name); this->m_uid = ptr.m_uid;
return *this; } ~Student() { if (this->m_name != NULL) { this->m_uid = 0; delete[] this->m_name; this->m_name = NULL; } } };
int main(int argc, char *argv[]) {
Student stu1(1,"lyshark"); Student stu2(2, "admin"); Student stu3(0, "");
stu3 = stu2 = stu1;
cout << stu3.m_name << endl; cout << stu2.m_name << endl; cout << stu1.m_name << endl;
system("pause"); return 0; }
|
关系运算符重载:
#include <iostream> #include <string>
using namespace std;
class Student { public: int m_uid; char * m_name;
public: Student(int uid,char *name) { this->m_uid = uid; this->m_name = name; }
bool operator == (Student &ptr) { if (this->m_uid == ptr.m_uid && this->m_name == ptr.m_name) return true; return false; } bool operator != (Student &ptr) { if (this->m_uid != ptr.m_uid && this->m_name != ptr.m_name) return true; return false; } };
int main(int argc, char *argv[]) { Student stu1(1, "lyshark"); Student stu2(1, "lyshark"); Student stu3(2, "admin");
if (stu1 == stu2) cout << "stu1 = stu2" << endl;
if (stu1 != stu3) cout << "stu1 != stu3" << endl;
system("pause"); return 0; }
|
重载与仿函数:
#include <iostream> #include <string>
using namespace std;
class MyPrint { public: void operator()(string text) { cout << text << endl; } };
class MyAdd { public: int operator()(int x, int y) { return x + y; } };
int main(int argc, char *argv[]) { MyPrint print;
print("hello lyshark"); cout << MyAdd()(100, 200) << endl;
system("pause"); return 0; }
|