Day30.C++04
001.静态成员变量和静态成员函数
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
class Person
{
public:
static int m_Age;//加入static就是 静态成员变量,会共享数据
//静态成员变量,在类内声明,类外进行初始化
int m_A;
//静态成员函数
//静态成员函数不可以访问 普通成员变量
//普通成员函数 可以访问普通成员变量,也可以访问静态成员变量
static void func()
{
//m_A = 10;//不可以访问
m_Age = 10;//可以访问静态成员变量
cout << "静态成员函数 func() 的调用" << endl;
}
//静态成员变量也是有权限的
//静态成员函数也是有权限的
private:
static int m_other;
void func2()
{
}
};
int Person::m_Age = 0;//类外初始化实现
int Person::m_other = 0;
void test01()
{
//1.通过对象访问属性
Person p1;
p1.m_Age = 10;
Person p2;
p2.m_Age = 20;
cout << "p1.m_Age:" << p1.m_Age << endl;//20
cout << "p2.m_Age:" << p2.m_Age << endl;//20
//2.通过类名访问属性
cout << "通过类名访问m_Age:" << Person::m_Age << endl;
//cout << "通过类名访问m_other:" << Person::m_other << endl;//私有权限在类外无法访问
//静态成员函数的调用
p1.func();
p2.func();
Person::func();
//静态成员函数也是有权限的
//Person::func2();
}
int main(void)
{
test01();
system("pause");
return EXIT_SUCCESS;
}
002.单例模式简介
简介:
单例模式是一种常用的软件设计模式。
在它的核心结构中只包含一个被称为单例的特殊类,通过单例模式可以保证系统中一个类只有一个实例而且该实例易于外界访问,从而方便对实例个数的控制并节约系统资源。
如果希望在系统中某个类的对象只能存在一个,单例模式是最好的解决方案。
003.单例模式案例——主席案例
代码实例:
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
//创建主席类
class ChairMan
{
private:
//构造函数私有化
ChairMan() {
cout << "主席构造函数调用" << endl;
}
//拷贝构造函数私有化
ChairMan(const ChairMan& c) {}
public:
//提供get方法访问主席
static ChairMan* getInstance()
{
return singleMan;
}
private:
static ChairMan* singleMan;
};
ChairMan* ChairMan::singleMan = new ChairMan;
//需求 单例模式 为了创建类中的对象,并且保证只有一个对象实例
void test01()
{
/*ChairMan c1;
ChairMan* c2 = new ChairMan;*/
/*ChairMan* cm1 = ChairMan::singleMan;
ChairMan* cm2 = ChairMan::singleMan;*/
//ChairMan::singleMan = NULL;
ChairMan* cm1 = ChairMan::getInstance();
ChairMan* cm2 = ChairMan::getInstance();
if (cm1 == cm2)
{
cout << "cm1和cm2相同" << endl;
}
else
{
cout << "cm1和cm2不相同" << endl;
}
/*ChairMan* cm3 = new ChairMan(*cm1);
if (cm3 == cm2)
{
cout << "cm3和cm2相同" << endl;
}
else
{
cout << "cm3和cm2不相同" << endl;
}*/
}
int main(void)
{
//cout << "main函数调用" << endl;//主席先于main的调用
test01();
system("pause");
return EXIT_SUCCESS;
}
总结:
- 目的:为了让类中只有一个实例,实例不需要自己释放
- 将默认构造和拷贝构造私有化
- 内部维护一个 对象指针
- 私有化这个唯一指针
- 对外提供 getInstance 方法来访问这个指针
- 保证类中只能实例化唯一一个对象
004.单例模式案例——打印机案例
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<string>
using namespace std;
class Printer
{
private:
Printer(){//:printCount(0){
//printCount = 0;
}
Printer(const Printer& p) {}
public:
static Printer* getInstance()
{
return singlePrinter;
}
void doPrintText(string text)
{
cout << text << endl;
printCount++;
}
int getPrintCount()
{
return printCount;
}
private:
static Printer* singlePrinter;
static int printCount;
};
Printer* Printer::singlePrinter = new Printer;
int Printer::printCount = 0;
void test01()
{
Printer* printer = Printer::getInstance();
printer->doPrintText("入职报告");
printer->doPrintText("加薪申请");
printer->doPrintText("升级申请");
printer->doPrintText("退休申请");
printer->doPrintText("辞职报告");
cout << "公司打印机使用次数: " << printer->getPrintCount() << endl;
}
int main(void)
{
test01();
system("pause");
return EXIT_SUCCESS;
}
005.C++对象模型初探(内含this指针的简单介绍)
总结:
- 成员变量和成员函数是分开存储的
- 空类的大小为1
- 只有非静态成员变量才属于对象身上
- this指针的简单介绍
代码证明:
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
class Person
{
public:
int personAge;
static int a;
void func(){}
};
void test01()
{
cout << "Person 类的大小: " << sizeof(Person) << endl;
}
//成员函数通过this指针即可直到操作的是哪个对象的数据。
//This指针是一种隐含指针,它隐含于每个类的非静态成员函数中
//注:静态成员函数内部没有this指针吗,静态成员函数不能操作非静态成员变量
void test02()
{
//this指针指向被调用的成员函数所属的对象
Person p1;
p1.func();//编译器会默认加一个隐含指针 this指针 在这是Person* this
//this指针永远指向当前对象
Person p2;
p2.func();
}
int main(void)
{
test01();
system("pause");
return EXIT_SUCCESS;
}
006.this指针的使用
总结:
- this指针永远指向当前对象
- 解决命名冲突问题
- *this 当前对象本体(可以作为返回值)
- 非静态成员属性才有this指针
代码:
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
class Person
{
public:
//this可以解决命名冲突问题
Person(int age)
{
this->age = age;
}
//对比年龄
void compareAge(Person& p)
{
if (this->age == p.age)
{
cout << "年龄相等" << endl;
}
else
{
cout << "年龄不相等" << endl;
}
}
//年龄相加
Person& plusAge(Person& p)
{
this->age += p.age;
return *this;//this指向对象本体
}
int age;
};
void test01()
{
Person p1(10);
cout << "p1.age = " << p1.age << endl;
Person p2(10);
p1.compareAge(p2);
p1.plusAge(p2).plusAge(p2);//链式编程
cout << "p1.age = " << p1.age << endl;
}
int main(void)
{
test01();
system("pause");
return EXIT_SUCCESS;
}
007.空指针访问成员函数
总结:
- 若成员函数没有用到this指针,那么空指针可以直接访问
- 如果成员函数用到this指针,就要注意,可以加一个if判断,判断this是否为空
代码:
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
class Person
{
public:
void personShow()
{
cout << "Person_show" << endl;
}
void showAge()
{
if (this == NULL)
{
return;
}
cout << "personAge:" << personAge << endl;
}
int personAge;
};
void test01()
{
Person* p1 = NULL;
p1->personShow();
p1->showAge();
}
int main(void)
{
test01();
system("pause");
return EXIT_SUCCESS;
}
008.常函数与常对象
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
class Person
{
public:
Person()
{
this->m_A = 0;
this->m_B = 0;
}
void showInfo() const//常函数 不允许修改this指针指向的值
{
//this->m_A = 10;
this->m_B = 10;
cout << "m_A:" << this->m_A << endl;
cout << "m_B:" << this->m_B << endl;
}
int m_A;
mutable int m_B;//就算是常函数,这个变量还是有修改的需求
};
void test01()
{
Person p1;
p1.showInfo();
//常对象 不允许修改属性
const Person p2;
//p2.m_A = 1000;
//常对象 不可以调用普通成员函数
//常对象 可以调用常函数
}
int main(void)
{
test01();
system("pause");
return EXIT_SUCCESS;
}
009.友元函数
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<string>
using namespace std;
class Building
{
//将全局函数变为 友元函数
friend void goodGay(Building* building);
public:
Building()
{
this->m_bedRoom = "卧室";
this->m_SittingRoom = "客厅";
}
//客厅 卧室
public:
string m_SittingRoom;
private:
string m_bedRoom;
};
void goodGay(Building* building)
{
cout << "好基友正在访问 " << building->m_SittingRoom << endl;
cout << "好基友正在访问 " << building->m_bedRoom << endl;
}
//友元函数的目的就是 访问类中的私有成员属性
void test01()
{
Building* building = new Building;
goodGay(building);
delete building;
}
int main(void)
{
test01();
system("pause");
return EXIT_SUCCESS;
}
010.类作友元类
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
class Building;
class goodGay
{
public:
goodGay();
void visit();
private:
Building* m_building;
};
class Building
{
//让goodGay 作为Building的友元类
friend class goodGay;
public:
Building();
public:
string m_SittingRoom;
private:
string m_BedRoom;
};
void test01()
{
goodGay gg;
gg.visit();
}
int main(void)
{
test01();
system("pause");
return EXIT_SUCCESS;
}
Building::Building()
{
this->m_BedRoom = "卧室";
this->m_SittingRoom = "客厅";
}
goodGay::goodGay()
{
this->m_building = new Building;
}
void goodGay::visit()
{
cout << "好基友正在访问 " << m_building->m_SittingRoom << endl;
cout << "好基友正在访问 " << m_building->m_BedRoom << endl;
}
友元类注意:
- 友元关系不能被继承
- 友元关系是单向的
- 友元关系不具有传递性
011.让成员函数做友元函数
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
//只让visit可以作为Building的好朋友 ,visit2不可以访问私有成员属性
class Building;
class goodGay
{
public:
goodGay();
void visit();
void visit2();
private:
Building* m_building;
};
class Building
{
//让成员函数 visit作为友元函数
friend void goodGay::visit();
public:
Building();
public:
string m_SittingRoom;
private:
string m_BedRoom;
};
void test01()
{
goodGay gg;
gg.visit();
}
int main(void)
{
test01();
system("pause");
return EXIT_SUCCESS;
}
Building::Building()
{
this->m_BedRoom = "卧室";
this->m_SittingRoom = "客厅";
}
goodGay::goodGay()
{
this->m_building = new Building;
}
void goodGay::visit()
{
cout << "好基友正在访问 " << m_building->m_SittingRoom << endl;
cout << "好基友正在访问 " << m_building->m_BedRoom << endl;
}
void goodGay::visit2()
{
cout << "好基友正在访问 " << m_building->m_SittingRoom << endl;
//cout << "好基友正在访问 " << m_building->m_BedRoom << endl;
}