对象链式调用
类、对象、引用、this指针
#include <iostream>
using namespace std;
#include <string>
class Person
{
public:
Person(){}
~Person(){}
Person(int age)
{
this->age = age;
}
// 如果没有引用,链式调用后会生成新的对象如p2',再次调用生成p2'',依此类推
Person& personAddAge(Person &p)
{
this->age += p.age;
// 返回的是对象本体
return *this;
}
int age;
};
void object_chained_func()
{
Person p = Person(10);
cout << "age is: " << p.age << endl;
Person p2 = Person(10);
// 链式调用
p2.personAddAge(p).personAddAge(p).personAddAge(p);
cout << "p2 age is: " << p2.age;
}
int main()
{
object_chained_func();
return 0;
}
引用
int a = 10;
int * const ref = &a ——> int & ref = a;
编译时,自动解引用 *ref,获取值10
this指针的本质:指针常量,指针的指向是不可以修改的
Person * const this;
成员函数常函数
void test() const{} 不允许修改成员属性
特殊变量 mutable
即使在常函数中也可以修改值
mutable int a;
常对象 const Class object;
常对象不可以调用普通成员函数,普通成员函数可以修改属性; 常对象只能调用常函数
友元
全局函数做友元
friend void func(类 * 指针对象);
#include <iostream>
using namespace std;
#include <string>
class Person
{
// 全局函数有友元,可以访问类中私有属性
friend void GlobalFunc(Person * person);
private:
string m_PrivateRoom;
public:
Person(/* args */)
{
m_PrivateRoom = "私有";
m_PublicRoom = "共有";
};
~Person(){};
string m_PublicRoom;
};
// 全局函数
void GlobalFunc(Person * person)
{
// 没有使用友元,无法访问私有属性
cout << "输出:" << person->m_PublicRoom << endl;
cout << "友元 输出:" << person->m_PrivateRoom << endl;
}
int main()
{
Person cus_person;
GlobalFunc(&cus_person);
return 0;
}
类做友元
指定类中 friend class 类名 当前类名是指定类的友元,可以访问指定类中的私有内容
#include <iostream>
using namespace std;
#include <string>
class Person
{
// FriendPerson类要访问Person中的私有属性,需要在Person中添加类做友元
// 确保FriendPerson类可以访问
friend class FriendPerson; // 类做友元
private:
string m_PrivateInfo;
public:
Person(/* args */);
Person(string pub_info, string pri_info);
~Person();
string m_PublicInfo;
};
Person::Person(/* args */)
{
}
Person::Person(string pub_info, string pri_info)
{
m_PublicInfo = pub_info;
m_PrivateInfo = pri_info;
}
Person::~Person()
{
}
class FriendPerson
{
private:
/* data */
public:
FriendPerson(/* args */);
~FriendPerson();
void Visit();
Person *per;
};
FriendPerson::FriendPerson(/* args */)
{
// Person * per = new Person;
per = new Person("人物公共信息", "人物私有信息");
}
FriendPerson::~FriendPerson()
{
}
void FriendPerson::Visit()
{
cout << "访问友元类中的公共属性 " << per->m_PublicInfo << endl;
cout << "访问友元类中的私有属性 " << per->m_PrivateInfo << endl;
}
int main()
{
// Person p1("人物公共信息", "人物私有信息");
FriendPerson fp;
fp.Visit();
return 0;
}
成员函数做友元
指定类中 friend void 类::func();
运算符重载operator
加号 operator+
Person operator+(const Person& p)
{
Person temp;
temp.m_A = this->m_A + p.m_A;
temp.m_B = this->m_B + p.m_B;
return temp;
}
左移 operator<<
只能利用全局函数重载左移运算符
void operator<<(ostream & cout, Person & p) // 本质:operator<<(ostream& cout,Person& p) cout << p
{
cout << "A=" << p.m_A << " B=" << p.m_B << endl;
}
#include <iostream>
using namespace std;
#include <string>
class Person
{
private:
/* data */
public:
Person();
Person(int a, int b)
{
this->m_A = a;
this->m_B = b;
}
~Person();
// Person operator+(Person &p1, Person &p2) Person p3 = operator+(p1, p2)
Person operator+(const 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;
};
Person::Person(){}
Person::~Person(){}
void operator<<(ostream & cout, Person & p) // 本质:operator<<(ostream& cout,Person& p) cout << p
{
cout << "A=" << p.m_A << " B=" << p.m_B << endl;
}
int main()
{
Person p1(10, 20);
Person p2(10, 20);
// Person p3 = p1.operator+(p2)
Person p3 = p1 + p2;
// cout << p3.m_A << " " << p3.m_B << endl;
cout << p3;
return 0;
}
递增 operator++ 递减operator--
实现自己的整型数据
#include <iostream>
using namespace std;
#include <string>
// 自定义整型数据
class MyInteger
{
friend ostream &operator<<(ostream &cout, MyInteger integer);
private:
int my_integer;
public:
MyInteger(/* args */);
~MyInteger();
// 前置递增
MyInteger& operator++()
{
my_integer++;
return *this;
}
// 后置递增
MyInteger operator++(int)
{
MyInteger temp = *this;
my_integer++;
return temp;
}
};
MyInteger::MyInteger(/* args */){}
MyInteger::~MyInteger(){}
ostream& operator<<(ostream& cout, MyInteger integer)
{
cout << integer.my_integer;
return cout;
}
int main()
{
MyInteger my_integer;
cout << ++(++my_integer) << endl;
cout << my_integer << endl;
cout << my_integer++ << endl;
cout << my_integer << endl;
return 0;
}