目录
一、静态成员
包括静态成员变量和静态成员函数
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
using namespace std;
class Person {
public:
//1、静态成员变量
//静态成员变量 在编译阶段就分配了内存
//类内声明 类外初始化
//静态成员变量 所有对象都共享同一份数据
static int m_A;
//2、静态成员函数
static void fun()
{
//m_C = 100; //静态成员函数无法使用非静态成员变量
m_A = 100; //静态成员函数可以使用静态成员变量
cout << "fun函数调用" << endl;
}
int m_C;
private:
static int m_B;
static void fun2()
{
}
};
int Person::m_A = 0;
void test01()
{
//1、通过对象进行访问
Person p1;
cout << p1.m_A << endl;
Person p2;
p2.m_A = 100;
cout << p1.m_A << endl;
//2、通过类名进行访问
cout << Person::m_A << endl;
//3、静态成员变量也是有访问权限的 私有的静态成员变量访问不到
//cout << Person::m_B << endl;
}
void test02()
{
//1、通过对象进行访问
Person p1;
p1.fun();
//2、通过类名进行访问
Person::fun();
//3、静态成员函数也有访问权限
//Person::fun2();
}
int main()
{
//test01();
test02();
system("pause");
return EXIT_SUCCESS;
}
二、单例模式
2.1、主席类案例
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
using namespace std;
class ChairMan
{
public:
static ChairMan* getInstance()
{
return singleMan;
}
private:
//先将构造函数私有化 不可创建多个对象
ChairMan()
{
}
ChairMan(const ChairMan&) {
}
//public:
//将主席指针私有化 对外提供接口
static ChairMan* singleMan; //类内声明 类外初始化
};
ChairMan* ChairMan::singleMan = new ChairMan;
void test01()
{
//ChairMan* c1 = ChairMan::singleMan;
//ChairMan* c2 = ChairMan::singleMan;
ChairMan* c1 = ChairMan::getInstance();
ChairMan* c2 = ChairMan::getInstance();
//ChairMan* c3 = new ChairMan(*c1);
if (c1 == c2)
{
cout << "c1 = c2" << endl;
}
else if(c1 != c2)
{
cout << "c1 != c2" << endl;
}
//if (c1 == c3)
//{
// cout << "c1 = c3" << endl;
//}
//else if (c1 != c3)
//{
// cout << "c1 != c3" << endl;
//}
}
int main()
{
test01();
system("pause");
return EXIT_SUCCESS;
}
2.2、打印机案例
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
using namespace std;
#include <string>
class Printer {
public:
static Printer* getInstance()
{
return printer;
}
void printText(string text)
{
m_Count++;
cout << text << endl;
}
int m_Count;
private:
Printer()
{
m_Count = 0;
//cout << "打印机构造调用" << endl;
}
Printer(const Printer& p) {};
static Printer* printer; //编译阶段分配内存
};
Printer* Printer::printer = new Printer;
void test01()
{
Printer* p1 = Printer::getInstance();
p1->printText("入职证明");
p1->printText("离职证明");
p1->printText("假条");
cout << "打印机使用次数 : " << p1->m_Count << endl;
Printer* p2 = Printer::getInstance();
p2->printText("病假");
cout << "打印机使用次数 : " << p2->m_Count << endl;
}
int main()
{
//cout << "main函数调用" << endl;
test01();
system("pause");
return EXIT_SUCCESS;
}
三、c++模型初探
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
using namespace std;
class Person {
public:
int m_A;//只有非静态成员变量 属于类对象上
void fun(){}//成员函数 并不属于类对象上
static int m_B;//静态成员变量 并不属于类对象上
static void fun02() {};//静态成员函数 并不属于类对象上
double m_C;
};
int Person::m_B = 0;
void test01()
{
//空类的sizeof结果是1 每个对象在内存上都有独一无二的地址
Person p;
cout << "sizeof(p) = " << sizeof(p) << endl;
}
int main()
{
test01();
system("pause");
return EXIT_SUCCESS;
四、this指针的简单使用
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
using namespace std;
class Person {
public:
Person(int age)
{
//用途1:解决名称冲突
this->age = age;
}
//this指针隐式得加在每个成员函数中
Person& AgeAdd(Person &p)
{
this->age += p.age;
return *this;
}
int age;
};
void test01()
{
//this指针 指向 被调用成员函数 所属的对象
Person p1(18);
cout << p1.age << endl;
Person p2(10);
cout << p2.age << endl;
p1.AgeAdd(p2).AgeAdd(p2).AgeAdd(p2);//链式
cout << p1.age << endl;
}
int main()
{
test01();
system("pause");
return EXIT_SUCCESS;
五、空指针访问成员函数
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
using namespace std;
class Person {
public:
void showName()
{
cout << "类名是Person" << endl;
}
void showAge()
{
if (this == NULL)
{
return;
}
cout << "m_Age = " << m_Age << endl;
}
int m_Age;
};
void test01()
{
Person *p = NULL;
p->showName();
p->showAge();
}
int main()
{
test01();
system("pause");
return EXIT_SUCCESS;
}
六、常函数和常对象
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
using namespace std;
class Person {
public:
Person(int age)
{
this->m_Age = age;
}
//常函数 修饰函数中的this指针
void showPerson() const
{
//m_Age = 100;
m_A = 100;
//this 指针的本质 :const Person *const this
//this = NULL 指针的指向不可以修改,指针指向的值可以修改
cout << "Person.age = " << this->m_Age << endl;
}
void fun()
{
}
int m_Age;
mutable int m_A; //常函数中有些特殊属性依然想更改 加入关键字 mutable
};
void test01()
{
//常对象
const Person p1(10);
p1.m_A = 10;
//p1.m_Age = 20;
p1.showPerson();
//p1.fun(); //常对象只能调用常函数
}
int main()
{
test01();
system("pause");
return EXIT_SUCCESS;
}
七、全局函数做友元函数
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
using namespace std;
#include <string>
class Building {
//利用friend关键字让全局函数 goodGuy作为本类好友 可以访问私有成员
friend void goodGuy(Building* building);
public:
Building()
{
this->m_SittingRoom = "客厅";
this->m_BedRoom = "房间";
}
string m_SittingRoom;
private:
string m_BedRoom;
};
//好基友全局函数 可以访问到私有属性
void goodGuy(Building* building)
{
cout << "好基友正在访问:" << building->m_SittingRoom << endl;
cout << "好基友正在访问:" << building->m_BedRoom << endl;
}
void test01()
{
Building building;
goodGuy(&building);
}
int main()
{
test01();
system("pause");
return EXIT_SUCCESS;
}
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
using namespace std;
#include <string>
class Building;
class goodGuy {
public:
goodGuy();
void vistor();
Building* building;
};
class Building {
//让goodGuy类作为Building的好朋友 可以访问到私有成员
friend class goodGuy;
public:
Building();
string m_SittingRoom;
private:
string m_BedRoom;
};
Building::Building()
{
m_BedRoom = "卧室";
m_SittingRoom = "客厅";
}
goodGuy::goodGuy()
{
this->building = new Building;
}
void goodGuy::vistor()
{
cout << "好基友正在访问:" << this->building->m_BedRoom << endl;
cout << "好基友正在访问:" << this->building->m_SittingRoom << endl;
}
void test01()
{
goodGuy gg;
gg.vistor();
}
int main()
{
test01();
system("pause");
return EXIT_SUCCESS;
}
九、类中的成员函数作为友元函数
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
using namespace std;
#include <string>
class Building;
class goodGuy {
public:
goodGuy();
void vistor(); //可以访问building的私有
void vistor2(); //不可以访问building的私有
Building* building;
};
class Building {
//让goodGuy中的visit成员函数作为友元
friend void goodGuy::vistor();
public:
Building();
string m_SittingRoom;
private:
string m_BedRoom;
};
Building::Building()
{
m_BedRoom = "卧室";
m_SittingRoom = "客厅";
}
goodGuy::goodGuy()
{
this->building = new Building;
}
void goodGuy::vistor()
{
cout << "好基友正在访问:" << this->building->m_BedRoom << endl;
cout << "好基友正在访问:" << this->building->m_SittingRoom << endl;
}
void goodGuy::vistor2()
{
//cout << "好基友正在访问:" << this->building->m_BedRoom << endl;
cout << "好基友正在访问:" << this->building->m_SittingRoom << endl;
}
void test01()
{
goodGuy gg;
gg.vistor();
gg.vistor2();
}
int main()
{
test01();
system("pause");
return EXIT_SUCCESS;
}