class Person
{
public:
Person(int age){
this->age = age;
}
Person PersonAdd(Person p){
this->age += p.age;
cout << this->age <<endl;
return *this;
}
int age;
};
void test01()
{
Person p(10);
Person q(10);
Person r(1);
//链式编程思想
r = q.PersonAdd(p).PersonAdd(p).PersonAdd(p);
cout << q.age << endl;
cout << r.age << endl;
}
输出:
20
30
40
20
40
空指针访问成员函数:
class Person
{
public:
void show(){
cout << "this" << endl;
}
void showAge(){
//防止出错加上if为NULL判断
cout << m_Age << endl;//this->m_Age,无对象实体
}
int m_Age;
};
void test01()
{
Person *p = NULL;
p->show();
p->showAge();//错
}
const
普通函数是可以修改普通变量的,但是常对象不可以被修改,所以常对象不能调用普通函数
//this = Person * const this 指针常量 指向不可修改 加上const之后,值也不可
//const Person * const this
///常函数
void showPerson() const{
//this->m_A = 100;//不可修改
}
int m_A;
//关键字:在常函数中可修改
mutable int m_B;
友元:让一个函数或类访问另一个类中的私有成员
全局函数做友元:
将函数声明放入类首,并加入friend关键字
class Building
{
//
friend void goodGay(Building *building);
public:
Building(){
m_SittingRoom = "客厅";
m_BedRoom = "卧室";
}
public:
string m_SittingRoom;
private:
string m_BedRoom;
};
void goodGay(Building *building)
{
cout << building->m_BedRoom;
}
void test01(){
Building building;
goodGay(&building);
}
类做友元:把自己设置为别人的friend
class Building
{
friend class goodGay;
public:
Building();
public:
string m_SittingRoom;
private:
string m_BedRoom;
};
Building::Building(){
m_SittingRoom = "hihhhhhhhhh";
m_BedRoom = "2";
}
class goodGay{
public:
goodGay();
void visit();
Building * building;
};
goodGay::goodGay(){
//
building = new Building;
}
void goodGay::visit(){
cout << building->m_SittingRoom << endl;
cout << building->m_BedRoom << endl;
}
void test01(){
goodGay g;
g.visit();
}
成员函数作友元:(只让某一个函数可以访问)
friend void goodGay::visit;
运算符重载:
加号:
左移运算符<<重载:
class Person
{
public:
//成员函数本质:object1.operator<<(object2)—— p << cout
void operator<<(ostream &cout){
}
int a;
int b;
};
//只有全局寒素才能重载左移运算符
//cout 要用引用的形式保证全局只有一个
ostream & operator<<(ostream &cout, Person p){
cout << p.a << "\t" << p.b << endl;
return cout;
}
void test01(){
Person p;
p.a = 10;
p.b = 10;
//基于链式编程规则,如果函数是void,没有void << endl这种语法,所以返回cout
cout << p << endl;
}
class MyInteger
{
friend ostream &operator<<(ostream &cout, MyInteger myint);
public:
//成员函数本质:object1.operator<<(object2)—— p << cout
MyInteger(){
m_Num = 0;
}
//前置
//一定要返回引用,
MyInteger &operator++(){
m_Num++;
return *this;
}
//后置
MyInteger operator++(int){
MyInteger temp = *this;
m_Num++;
return temp;
}
int m_Num;
};
ostream &operator<<(ostream &cout, MyInteger myint){
cout << myint.m_Num << endl;
return cout;
}
void test01(){
MyInteger myint;
cout << (myint++)++ << endl;
cout << myint.m_Num <<endl;
MyInteger myint2;
cout << ++(++myint2) <<endl;
cout << myint2 << endl;
}
浅拷贝:(堆区内存重复释放
深拷贝:
赋值运算符重载:
//为了能实现连等操作,返回本身
Person & operator=(Person &p){
//编译器提供浅拷贝:m_Age = p.m_Age
//先判断堆区是否有属性,先释放再深拷贝
if(m_Age != NULL){
delete m_Age;
m_Age = NULL;
}
//解引用,深拷贝
m_Age = new int(*p.m_Age);
return *this;
}
关系运算符重载:
class Person
{
public:
Person(string name, int age){
m_Age = age;
}
bool operator==(Person &p){
if(this->m_Age == p.m_Age && this->m_Name == p.m_Name){
return true;
}else return false;
}
int m_Age;
string m_Name;
};
void test01(){
Person p1("T", 18);
Person p2("T", 18);
if(p1 == p2) cout << "true";
}
函数调用运算符重载:()
由于使用起来类似于函数调用,因此称为仿函数
class MyPrint
{
public:
void operator()(string test){
cout << test;
}
};
void test01(){
MyPrint myPrint;
myPrint("Hello");
}
继承:减少重复代码
语法:class 子类 :继承方式 父类
子类=派生类,父类=基类
class BasePage
{
public:
void header(){
cout << "标题" << endl;
}
};
class Jave: public BasePage{
public :
void concent(){
cout << "Jave" << endl;
}
}
继承方式:
同名静态成员变量
同名静态函数:
同名问题,只要子类出现,父类所有的同名都被隐藏,包括重载的函数
class CPU{
public:
virtual void calculate() = 0;
};
class VideoCard{
public:
virtual void dispaly() = 0;
};
class Memory{
public:
virtual void storage() = 0;
};
class Computer{
public:
Computer(CPU * cpu ,VideoCard * vc, Memory * mem){
m_cpu = cpu;
m_vc = vc;
m_mem = mem;
}
void work(){
m_cpu->calculate();
m_vc->dispaly();
m_mem->storage();
}
private:
CPU * m_cpu;
VideoCard * m_vc;
Memory * m_mem;
};
class IntelCPU : public CPU{
public:
virtual void calculate(){
cout << "InterCPU" << endl;
}
};
class IntelVideoCard : public VideoCard{
public:
virtual void dispaly(){
cout << "Inter显卡" << endl;
}
};
class IntelMemory : public Memory{
public:
virtual void storage(){
cout << "Intel内存条" << endl;
}
};
class LenoveCPU : public CPU{
public:
virtual void calculate(){
cout << "LenoveCPU" << endl;
}
};
class LenovoVideoCard : public VideoCard{
public:
virtual void dispaly(){
cout << "Lenovo显卡" << endl;
}
};
class LenovoMemory : public Memory{
public:
virtual void storage(){
cout << "Lenovo内存条" << endl;
}
};
void test01(){
CPU * icpu = new IntelCPU;
VideoCard * ivc = new IntelVideoCard;
Memory * imem = new IntelMemory;
Computer * computer1 = new Computer(icpu, ivc, imem);
computer1->work();
delete computer1;
}
文件:
void test01(){
//创建流对象
ofstream ofs;
//指定打开方式
ofs.open("text.txt",ios::out);
//写内容
ofs << "张三" <<endl;
ofs << "18" << endl;
//关闭文件:
ofs.close();
}
二进制读写:
//二进制:ios::binary
class Person{
public:
char m_Name[64];
int m_Age;
};
void test03(){
ofstream ofs;
ofs.open("Person.txt", ios::out | ios::binary);
Person p={"张三", 18};
//ostream& write(const char * buffer, int len)
ofs.write((const char *)&p, sizeof(Person));
ofs.close();
}
//二进制读文件
void test04(){
ifstream ifs;
ifs.open("person.txt", ios::in | ios::binary);
if(!ifs.is_open()) return;
Person p;
//函数原型:istream& read(char * buffer, int len)
ifs.read((char *)&p, sizeof(Person));
cout << p.m_Age << endl << p.m_Name;
ifs.close();
}