20类—友元(全局函数做友元)
#include<iostream>
using namespace std;
#include<string>
class Buliding{
friend void myFriend01(Buliding *building);
public:
Buliding()
{
m_SittingRoom = "客厅";
m_BedRoom = "卧室";
}
public:
string m_SittingRoom;
private:
string m_BedRoom;
};
void myFriend01(Buliding *building)
{
cout << "好朋友(全局函数),要走进:" << building->m_SittingRoom << endl;
cout << "好朋友(全局函数),要走进:" << building->m_BedRoom << endl;
}
void Test01()
{
Buliding b1;
myFriend01(&b1);
}
int main(){
Test01();
system("pause");
return 0;
}
21类—友元(友元类)
#include<iostream>
using namespace std;
#include<string>
class Building{
friend class Myfriend;
public:
Building();
public:
string m_SittingRoom;
private:
string m_BedRoom;
};
Building::Building()
{
m_SittingRoom = "客厅";
m_BedRoom = "卧室";
}
class Myfriend{
public:
Myfriend(){
building = new Building;
}
Building *building;
void visit(){
cout << "好朋友(类),要走进:" << building->m_SittingRoom << endl;
cout << "好朋友(类),要走进:" << building->m_BedRoom << endl;
}
};
void Test01()
{
Myfriend m1;
m1.visit();
}
int main(){
Test01();
system("pause");
return 0;
}
22类—友元(成员函数做友元)
#include<iostream>
using namespace std;
#include<string>
class Building;
class MyFriend{
public:
MyFriend();
Building *building;
void visit01();
void visit02();
};
class Building{
friend void MyFriend::visit01();
public:
Building();
public:
string m_SittingRoom;
private:
string m_BedRoom;
};
MyFriend::MyFriend(){
building = new Building;
}
void MyFriend::visit01(){
cout << "好朋友(成员函数visit01),要走进:" << building->m_SittingRoom << endl;
cout << "好朋友(成员函数visit01),要走进:" << building->m_BedRoom << endl;
}
void MyFriend::visit02(){
cout << "好朋友(成员函数visit02),要走进:" << building->m_SittingRoom << endl;
}
Building::Building()
{
m_SittingRoom = "客厅";
m_BedRoom = "卧室";
}
void Test01()
{
MyFriend m1;
m1.visit01();
m1.visit02();
}
int main(){
Test01();
system("pause");
return 0;
}
23类—运算符重载(加号运算符重载)
#include<iostream>
using namespace std;
#include<string>
class Person{
public:
Person operator+(Person &p)
{
Person temp;
temp.m_A = this->m_A + p.m_A;
temp.m_B = this->m_A + p.m_B;
return temp;
}
int m_A;
int m_B;
};
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;
}
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;
Person p2;
p1.m_A = 10;
p1.m_B = 10;
p2.m_A = 20;
p2.m_B = 20;
Person p3 = p1.operator+(p2);
Person p4=operator+(p1,p2);
Person p5 = p1 + 10;
cout << "p3.m_A=" << p3.m_A << endl;
cout << "p3.m_B=" << p3.m_B << endl;
cout << "p4.m_A=" << p4.m_A << endl;
cout << "p4.m_B=" << p4.m_B << endl;
cout << "p5.m_A=" << p5.m_A << endl;
cout << "p5.m_B=" << p5.m_B << endl;
}
int main(){
Test01();
system("pause");
return 0;
}
24类—运算符重载(左移运算符重载)
#include<iostream>
using namespace std;
#include<string>
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 << "a=" << p.m_A << " b= " << p.m_B;
return cout;
}
void test() {
Person p1(10, 20);
operator<< (cout, p1) << endl;
cout << p1 << " hello world" << endl;
}
int main() {
test();
system("pause");
return 0;
}
25类—运算符重载(递增运算符重载)
#include<iostream>
using namespace std;
#include<string>
class MyInteger {
friend ostream& operator<<(ostream& out, MyInteger myint);
public:
MyInteger() {
m_Num = 0;
}
MyInteger & operator++() {
m_Num++;
return *this;
}
MyInteger operator++(int) {
MyInteger temp = *this;
m_Num++;
return temp;
}
private:
int m_Num;
};
ostream& operator<<(ostream& out, MyInteger myint) {
out << myint.m_Num;
return out;
}
void test01() {
MyInteger myInt;
cout << ++myInt << endl;
cout << myInt << endl;
}
void test02() {
MyInteger myInt;
cout << myInt++ << endl;
cout << myInt << endl;
}
int main()
{
test02();
system("pause");
return 0;
}
26类—运算符重载(赋值运算符)
#include<iostream>
using namespace std;
#include<string>
class Person
{
public:
Person(int age)
{
m_Age = new int(age);
}
Person& operator=(Person &p)
{
if (m_Age != NULL)
{
delete m_Age;
m_Age = NULL;
}
m_Age = new int(*p.m_Age);
return *this;
}
~Person()
{
if (m_Age != NULL)
{
delete m_Age;
m_Age = NULL;
}
}
int *m_Age;
};
void test01()
{
Person p1(18);
Person p2(20);
Person p3(30);
p3 = p2 = p1;
cout << "p1的年龄为:" << *p1.m_Age << endl;
cout << "p2的年龄为:" << *p2.m_Age << endl;
cout << "p3的年龄为:" << *p3.m_Age << endl;
}
int main() {
test01();
system("pause");
return 0;
}
27类—运算符重载(关系运算符)
#include<iostream>
using namespace std;
#include<string>
class Person
{
public:
Person(string name, int age)
{
this->m_Name = name;
this->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)
{
if (this->m_Name == p.m_Name && this->m_Age == p.m_Age)
{
return false;
}
else
{
return true;
}
}
string m_Name;
int m_Age;
};
void test01()
{
Person a("孙悟空", 18);
Person b("孙悟空", 18);
if (a == b)
{
cout << "a和b相等" << endl;
}
else
{
cout << "a和b不相等" << endl;
}
if (a != b)
{
cout << "a和b不相等" << endl;
}
else
{
cout << "a和b相等" << endl;
}
}
int main() {
system("pause");
return 0;
}
28类—运算符重载(函数调用运算符)
#include<iostream>
using namespace std;
#include<string>
class MyPrint
{
public:
void operator()(string text)
{
cout << text << endl;
}
};
void test01()
{
MyPrint myFunc;
myFunc("AISMALL");
}
class MyAdd
{
public:
MyAdd()
{
cout << "调用有参构造" << endl;
}
~MyAdd()
{
cout << "调用析构" << endl;
}
int operator()(int v1, int v2)
{
return v1 + v2;
}
};
void test02()
{
MyAdd add;
int ret = add(10, 10);
cout << "ret = " << ret << endl;
cout << "匿名对象特点:调用完就释放" << endl;
cout<<"匿名对象:"<< "MyAdd()(100,100) = " << MyAdd()(100, 100) << endl;
}
int main() {
test01();
test02();
system("pause");
return 0;
}