1、静态成员
#include<iostream>
using namespace std;
#include<string>
//静态成员变量
class Person
{
public:
//1、所有对象都共享同一份数据
//2、编译阶段就分配内存
//3、类内声明,类外初始化操作(必须操作)
static int m_A;
//静态成员变量也是有访问权限的
private:
static int m_B;
};
int Person::m_A=100;
int Person::m_B = 300;
void test01()
{
Person p;
cout << p.m_A << endl;
//100
Person p2;
p2.m_A = 200;
cout << p.m_A << endl;
//200
}
void test02()
{
//静态成员变量 不属于某个对象上,所有对象都共享同一份数据
//因此静态成员变量有两种访问方式
//1、通过对象进行访问
//Person p;
//cout << p.m_A << endl;
//2、通过类名进行访问
cout << Person::m_A << endl;
//cout << Person::m_B << endl; 不可访问,因为为私有访问权限,类外访问不到
}
int main()
{
//test01();
test02();
system("pause");
return 0;
}
#include<iostream>
using namespace std;
//静态成员函数
//1、所有对象都共享同一个函数
//2、静态成员函数只能访问静态成员变量
class Person
{
public:
static void func()
{
m_A = 100;//静态成员函数 可以访问 静态成员变量
//m_B = 200;//静态成员函数 不可以访问 非静态成员变量 ,无法区分到底是哪个对象的m_B属性
cout << "static void func调用" << endl;
}
static int m_A;//静态成员变量
int m_B;
//静态成员函数也是有访问权限的
private:
static void func2()
{
cout<< "static void func2调用" << endl;
}
};
int Person::m_A=0;
//两种访问方式
void test01()
{
//1、通过对象访问
Person p;
p.func();
//2、通过类名访问
Person::func();
//Person::func2(); 访问出错 类外访问不到私有静态成员函数
}
int main()
{
test01();
system("pause");
return 0;
}
2、成员变量和成员函数分开存储
#include<iostream>
using namespace std;
//成员变量 和 成员函数 分开存储
class Person
{
public:
int m_A;//非静态成员变量 属于类的对象上的
static int m_B;//静态成员变量 不属于类的对象上
void func() {}//非静态成员函数 不属于类的对象上
static void func2() {}//静态成员函数 不属于类的对象上
};
int Person::m_B = 0;
void test01()
{
Person p;
//空对象占用内存空间为:1
//C++编译器会给每个空对象也分配一个字节的空间,是为了区分空对象占内存的位置
//每个空对象也应该有一个独一无二的内存地址
cout << "size of p = " << sizeof(p) << endl;
}
void test02()
{
Person p;
//int对象占用内存空间为:4
cout << "size of p = " << sizeof(p) << endl;
}
int main()
{
//test01();
test02();
system("pause");
return 0;
}
3、this指针的用途
#include<iostream>
using namespace std;
//this指针概念
class Person
{
public:
//创建一个成员函数
Person(int age)
{
//this指针指向的是 被调用的成员函数 所属的对象
this->age = age;
}
Person PersonAddAge(Person &p)
{
this->age += p.age;
//this 指向p2的指针,而*this指向的就是p2这个对象本体
return *this;
}
int age;
};
//1、解决名称冲突
void test01()
{
Person p1(18);
cout << "p1的年龄是: " << p1.age << endl;
}
//2、返回对象本身用*this
void test02()
{
Person p1(10);
Person p2(10);
//链式编程思想
p2.PersonAddAge(p1).PersonAddAge(p1).PersonAddAge(p1);
cout << "p2的年龄为: " <<p2.age<< endl;
}
int main()
{
test01();
test02();
system("pause");
return 0;
}
重点(引用返回):
值返回,会创建新对象
4、空指针访问成员函数
#include<iostream>
using namespace std;
//空指针调用成员函数
class Person
{
public:
void showClassName()
{
cout << "this is Person class" << endl;
}
void showPersonAge()
{
//报错原因是因为传入的指针是为NULL
if (this == NULL)
{
return;
}
cout << "age= " <<this->m_Age<< endl;
}
int m_Age;
};
void test01()
{
Person* p = NULL;
//p->showClassName();
p->showPersonAge();
}
int main()
{
test01();
system("pause");
return 0;
}
5、const修饰成员函数
-
#include<iostream> using namespace std; //常函数 class Person { public: //this指针的本质 是指针常量 指针的指向是不可以修改的 //const Person * const this; //在成员函数后面加const,修饰的是this指针,让指针指向的值也不可以修改 void showPerson()const { this->m_B=10; //this->m_A = 100; //this = NULL;//this指针不可以修改指针的指向 } void func() { } int m_A; mutable int m_B;//特殊变量,即使在常函数中,也可以修改这个值,加关键字mutable }; void test01() { Person p; p.showPerson(); } //常对象 void test02() { const Person p;//在对象前加const,变为常对象 //p.m_A = 100; p.m_B = 100;//m_B特殊变量,在常对象中,也可以修改这个值 //常对象只能调用常函数 p.showPerson(); //p.func();// } int main() { test01(); test02(); system("pause"); return 0; }