十年網(wǎng)站開發(fā)經(jīng)驗 + 多家企業(yè)客戶 + 靠譜的建站團隊
量身定制 + 運營維護+專業(yè)推廣+無憂售后,網(wǎng)站問題一站解決
目錄
一、加法運算符重載
二、左移運算符重載
三、前置遞增運算符重載
四、后置遞增運算符重載
五、指針運算符重載
六、等號運算符重載
七、關(guān)系運算符重載
八、函數(shù)調(diào)用運算符重載
九、函數(shù)重載注意事項
#define _CRT_SECURE_NO_WARNINGS
#includeusing namespace std;
class Person {
public:
Person()
{
}
Person(int a, int b):m_A(a),m_B(b){}
//利用成員函數(shù)實現(xiàn)加法運算符重載
//Person operator+(Person& p)
//{
// Person temp;
// temp.m_A = this->m_A + p.m_A;
// temp.m_B = this->m_B + p.m_B;
// return temp;
//}
int m_A;
int m_B;
};
//利用全局函數(shù)實現(xiàn)加法運算符重載
Person operator+(Person& p1, Person& p2)
{
Person temp;
temp.m_A = p1.m_A + p2.m_A;
temp.m_B = p1.m_B + p2.m_B;
return temp;
}
//運算符重載可以發(fā)生函數(shù)重載
Person operator+(Person& p1, int num)
{
Person temp;
temp.m_A = p1.m_A + num;
temp.m_B = p1.m_B + num;
return temp;
}
void test01()
{
Person p1(10,10);
Person p2(20, 20);
//Person p3 = operator+(p1, p2); //全局函數(shù)本質(zhì)
//Person p3 = p1.operator+(p2); //成員函數(shù)本質(zhì)
Person p3 = p1 + p2; //簡化
cout<< "p3.m_A = "<< p3.m_A<< " p3.m_B = "<< p3.m_B<< endl;
}
void test02()
{
Person p1(10, 10);
Person p2 = p1 + 10;
cout<< "p2.m_A = "<< p2.m_A<< " p2.m_B = "<< p2.m_B<< endl;
}
int main()
{
//test01();
test02();
system("pause");
return EXIT_SUCCESS;
}
二、左移運算符重載#define _CRT_SECURE_NO_WARNINGS
#includeusing namespace std;
class Person {
friend ostream& operator<<(ostream& cout, Person& p);
public:
Person(int a, int b)
{
this->m_A = a;
this->m_B = b;
}
private:
int m_A;
int m_B;
};
ostream& operator<<(ostream &cout,Person &p)
{
cout<< "m_A = "<< p.m_A<< " m_B = "<< p.m_B;
return cout;
}
void test01()
{
Person p1(10, 10);
cout<< p1<< endl;
}
int main()
{
test01();
system("pause");
return EXIT_SUCCESS;
}
三、前置遞增運算符重載#define _CRT_SECURE_NO_WARNINGS
#includeusing namespace std;
class MyInt {
friend ostream& operator<<(ostream& cout, MyInt& p);
public:
MyInt()
{
this->m_A = 0;
}
//前置++ 重載
MyInt& operator++()
{
this->m_A++;
return *this;
}
private:
int m_A;
};
ostream& operator<<(ostream& cout, MyInt& p)
{
cout<< p.m_A;
return cout;
}
void test01()
{
MyInt p1;
cout<< ++(++p1)<< endl;
cout<< p1<< endl;
}
int main()
{
test01();
system("pause");
return EXIT_SUCCESS;
}
四、后置遞增運算符重載#define _CRT_SECURE_NO_WARNINGS
#includeusing namespace std;
class MyInt {
friend ostream& operator<<(ostream& cout, MyInt p);
public:
MyInt()
{
this->m_A = 0;
}
//后置++ 重載
MyInt operator++(int)
{
MyInt temp = *this;
this->m_A++;
return temp;
}
private:
int m_A;
};
ostream& operator<<(ostream& cout, MyInt p)
{
cout<< p.m_A;
return cout;
}
void test02()
{
MyInt p;
cout<< p++<< endl;
cout<< p++<< endl;
//cout<< p<< endl;
}
int main()
{
test02();
system("pause");
return EXIT_SUCCESS;
}
五、指針運算符重載#define _CRT_SECURE_NO_WARNINGS
#includeusing namespace std;
class Person {
public:
Person(int a)
{
cout<< "Person的有參構(gòu)造函數(shù)調(diào)用"<< endl;
m_Age = a;
}
void showAge()
{
cout<< "年齡 : "<< this->m_Age<< endl;
}
~Person()
{
cout<< "Person的析構(gòu)函數(shù)調(diào)用"<< endl;
}
private:
int m_Age;
};
class SmartPoint {
public:
SmartPoint(Person *Person)
{
this->m_Person = Person;
}
//重載->Person* operator->()
{
return this->m_Person;
}
//重載*
Person& operator*()
{
return *m_Person;
}
~SmartPoint()
{
if (this->m_Person)
{
delete this->m_Person;
this->m_Person = NULL;
}
}
private:
Person *m_Person;
};
void test01()
{
//Person *p1 = new Person(18);
//p1->showAge();
//(*p1).showAge();
//delete p1;
//利用智能指針 管理new出來的Person的釋放操作
SmartPoint sp(new Person(18));
sp->showAge(); //本質(zhì)sp->->showAge() 編譯器優(yōu)化sp->showAge()
(*sp).showAge();
}
int main()
{
test01();
system("pause");
return EXIT_SUCCESS;
}
六、等號運算符重載#define _CRT_SECURE_NO_WARNINGS
#includeusing namespace std;
//編譯器默認給一個類添加四個函數(shù) 默認構(gòu)造 析構(gòu)函數(shù) 拷貝構(gòu)造(值拷貝) operator=(值拷貝)
class Person {
public:
Person(const char* name, int age)
{
this->m_Name = new char[strlen(name) + 1];
strcpy(m_Name, name);
this->m_Age = age;
}
//重載=
Person& operator=(const Person& p)
{
if (this->m_Name != NULL)
{
delete [] this->m_Name;
this->m_Name = NULL;
}
this->m_Name = new char[strlen(p.m_Name) + 1];
strcpy(this->m_Name, p.m_Name);
this->m_Age = p.m_Age;
return *this;
}
~Person()
{
if (this->m_Name != NULL)
{
delete [] this->m_Name;
this->m_Name = NULL;
}
}
char* m_Name;
int m_Age;
};
void test01()
{
Person p1("Tom",18);
Person p2("Jerry",20);
Person p3("", 30);
p3 = p2 = p1;
cout<< "姓名: "<< p1.m_Name<< " 年齡 = "<< p2.m_Age<< endl;
cout<< "姓名: "<< p2.m_Name<< " 年齡 = "<< p2.m_Age<< endl;
cout<< "姓名: "<< p3.m_Name<< " 年齡 = "<< p2.m_Age<< endl;
}
int main()
{
test01();
system("pause");
return EXIT_SUCCESS;
}
七、關(guān)系運算符重載#define _CRT_SECURE_NO_WARNINGS
#includeusing namespace std;
class Person {
public:
Person(string name, int age)
{
m_Name = name;
m_Age = age;
}
bool operator==(Person& p)
{
if (this->m_Name == p.m_Name && this->m_Age == p.m_Age)
return true;
else
return false;
}
bool operator!=(Person& p)
{
return !(this->m_Name == p.m_Name && this->m_Age == p.m_Age);
}
string m_Name;
int m_Age;
};
void test01()
{
Person p1("Tom", 18);
Person p2("Tom", 18);
if (p1 == p2)
cout<< "p1 == p2"<< endl;
else
cout<< "p1 != p2"<< endl;
if (p1 != p2)
cout<< "p1 != p2"<< endl;
else
cout<< "p1 == p2"<< endl;
}
int main()
{
test01();
system("pause");
return EXIT_SUCCESS;
}
八、函數(shù)調(diào)用運算符重載#define _CRT_SECURE_NO_WARNINGS
#includeusing namespace std;
class MyPrint {
public:
void operator()(string test)
{
cout<< test<< endl;
}
};
void myPrint2(string str)
{
cout<< str<< endl;
}
void test01()
{
MyPrint myPrint;
myPrint("hello world"); //仿函數(shù) 本質(zhì)是一個對象 函數(shù)對象
myPrint2("hello world"); //普通函數(shù)
}
class MyAdd {
public:
int operator()(int a,int b)
{
return a + b;
}
};
void test02()
{
//MyAdd myAdd;
//cout<< myAdd(1, 2)<< endl;
cout<< MyAdd()(1, 2)<< endl; //匿名函數(shù)對象 特點 :當前執(zhí)行完立即釋放
}
int main()
{
//test01();
test02();
system("pause");
return EXIT_SUCCESS;
}
九、函數(shù)重載注意事項不要重載&&和||
你是否還在尋找穩(wěn)定的海外服務(wù)器提供商?創(chuàng)新互聯(lián)www.cdcxhl.cn海外機房具備T級流量清洗系統(tǒng)配攻擊溯源,準確流量調(diào)度確保服務(wù)器高可用性,企業(yè)級服務(wù)器適合批量采購,新人活動首月15元起,快前往官網(wǎng)查看詳情吧