栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

黑马:对象特性2(113~117)

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

黑马:对象特性2(113~117)

静态成员变量

​
#include
using namespace std;
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include


class Person {
public:
	static int m_A;
};

int Person::m_A=100; //类内声明,类外初始化;


void test01() {
	Person p;
	cout << p.m_A << endl;

	Person p2;
	p2.m_A = 200;
	cout << p.m_A << endl; //输出的值为200,因为静态成员变量共享同一份数据
}

void test02() {
	//通过对象进行访问
	Person p;
	cout << p.m_A << endl;

	//通过类名进行访问
	cout << Person::m_A << endl;
}

int main(int argc,char**argv) {
	test01();
	test02();
	
	//静态成员变量也有访问权限,private下创建的变量类外仍然无法访问;
	

	system("pause");
	return  0;
}

​

静态成员函数:

#include
using namespace std;
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include

//静态成员函数
//所有对象共享同一个函数
//静态成员函数只能访问静态成员变量

class Person {
public:
	static void func() {
		m_A = 100;//静态成员函数可以访问静态成员变量;
		//m_B = 100;(报错) 静态成员函数不可以访问非静态成员变量,无法区分到底是哪个对象的m_B属性;
		cout << "static void func调用" << endl;
	}
	static int m_A;
	int m_B;
};

int Person::m_A = 0;

void test01() {
	//通过对象访问
	Person p;
	p.func();

	//通过类名访问
	Person::func();

}

int main(int argc,char**argv) {
	test01();
	
	//静态成员函数也有访问权限,private作用域下创建的静态成员函数类外仍然无法访问

	system("pause");
	return  0;
}

C++对象模型和this指针

1.成员变量和成员函数分开存储

c++中,类内的成员变量和成员函数分开存储

只有非静态成员变量才属于类的对象上

#include
using namespace std;
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include

//成员变量和成员函数分开存储

class Person {

	int m_A; //非静态成员变量,属于类的对象上 (输出为4,下面三个也是4)

	static int m_B; //静态成员变量,不属于类的对象上

	void func(){} //非静态成员函数,不属于类的对象上

	static void func(){}	//静态成员函数,不属于类的对象上
	
};

int Person::m_B=0;

void test01() {
	Person p;
	//空对象占用内存为1
	//c++编译器会给每个空对象分配一个内存空间,为了区分空对象所占内存的位置
	//每个空对象也应该有独一无二的内存地址
	cout << "size of p = " << sizeof(p) << endl;
}

void test02() {
	Person p;
	cout << "size of p = " << sizeof(p) << endl;
}

int main(int argc,char**argv) {
	
	test02();

	system("pause");
	return  0;
}

2.this指针概念

每一个非静态成员函数只会诞生一份函数实例,多个同类型的对象会共用一块代码

代码为了区分哪个对象调用自己,于是使用this指针

this指针指向被调用的成员函数所属的对象

this指针是隐含每一个非静态成员函数内的一种指针

不需要定义,直接使用。

this指针用途:

当形参和成员变量同名时,可用this指针区分

在类的非静态成员函数中返回对象本身,可使用return *this

#include
using namespace std;
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include



class Person {
public:
	Person(int age) {
		//this指针指向被调用的成员函数所属的对象
		this->age = age;
	}

	Person & addAage(Person& p) {  //如果开头Person后不加引用,就会以值的方式返回,会返回新的对象,最后输出的结果是20;
		this->age += p.age;

		//this指向p2的指针,而*this指向的就是p2这个对象本体
		return *this;
	}

	int age;
};

//解决名称冲突
void test01() {
	Person p1(18);
	cout << "p1的年龄为: " << p1.age << endl;
}

void test02() {
	Person p1(10);
	Person p2(10);
	
	//链式编程
	p2.addAage(p1).addAage(p1).addAage(p1);
	cout << "p2的年龄为: " << p2.age << endl;
}

int main(int argc,char**argv) {
	//test01();

	test02();
	system("pause");
	return  0;
}

3.空指针访问成员函数

空指针可以调用成员函数,但是要注意有没有用到this指针

如果用到this指针,需要加以判断保证代码健壮性

#include
using namespace std;
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include


class Person {
public:
	void showClassName() {
		cout << "this is a Person class" << endl;
	}

	
	void showPersonAge() {
		if (this == NULL) {
			return;
		}
		cout << "age = " << this->m_Age << endl; //会崩溃,因为让空指针访问了成员,解决办法是加限制条件↑
	}

	int m_Age;
};


void test01() {
	Person* p = NULL;

	//空指针可以访问成员
	p->showClassName();
	p->showPersonAge();
}


int main(int argc,char**argv) {
	test01();

	
	system("pause");
	return  0;
}

4.const修饰成员函数

常函数:

成员函数后加const称为常函数

常函数内不可以修改成员属性

成员属性声明时加关键字mutable后在常函数中依然可以修改

常对象:

声明对象前加const称该对象为常对象

常对象只能调用常函数

#include
using namespace std;
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include


class Person {
public:
	//this指针的本质 是指针常量 指向不可以修改
	//如果在成员函数后加const 那指针指向的值也不能被修改了
	void showPerson() const{
		this->m_B = 100; //正确 因为加了mutable
		//this->m_A = 100; 报错                 (m_A=100也一样,毕竟每个非静态成员函数里都有一个this指针)
		//this=NULL        报错,this指针不可以修改指针的指向
	}

	void func() {
		m_A = 100;  //普通函数内可以修改成员的值 不报错
	}

	int m_A;
	mutable int m_B;  //特殊变量,即使在常函数中,也可以修改这个值
};

void test01() {
	Person p;
	p.showPerson();
}

//常对象

void test02() {
	const Person p;
	//p.m_A = 100;  报错
	p.m_B = 100; // m_B上面定义了是特殊变量 

	p.showPerson();
	//p.func();  报错,因为普通函数内可以修改成员的值,但是常对象只能调用常函数,要是不报错的话常函数内岂不是也可以修改成员的值?
	//            那就矛盾了
}




int main(int argc,char**argv) {
	test01();
	test02(); 

	
	system("pause");
	return  0;
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/297760.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号