黑马程序员匠心之作-4.3对象模型和this指针

博客围绕C++展开,介绍了类的成员变量和成员函数分开存储,非静态成员变量在类上,空类占一个字节内存以区分不同空类。还提及this指针概念、空指针访问成员函数以及const修饰成员函数等内容。

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

1、成员变量与成员函数分开存储;

2、只有非静态成员变量在类上

3、当一个类中没有成员变量和成员函数时,仍然占一个字节的内存空间,目的是区分不同的空类

4.3.2this指针概念

#include<iostream>
#include<string>
using namespace std;
//class Person {
//	int a;//非静态成员变量不属于类上
//	static int b;//静态成员变量属于类
//	void fun();//非静态成员函数不属于类
//	static void fun1();//静态成员函数不属于类;
//
//};
class Person
{
public:

	int age;
	Person(int age)
	{
		//age = age;//形参与成员变量名称相同的时候,名称冲突赋值出错;
		//this指针用来解决名称冲突问题;
		this->age = age;
		
	}
	//注意这里要以引用的方式返回,才能一直返回P1本身;若以值的方式返回,
    //则每次都会在另一个内存创建一个和传入相同的对象,不能改动P1原本的值;
	Person& Personaddage(Person& P)
	{
		this->age += P.age;
		return *this;
	}
};
void test01()
{
	Person P(18);

	cout << "P年龄:" << P.age << endl;

}
void test02()
{
	Person P1(10);
	Person P2(20);
	//链式编程的思想:可以不断的返回本身并且追加操作;
	P1.Personaddage(P2).Personaddage(P2).Personaddage(P2).Personaddage(P2).Personaddage(P2);
	cout << "P1年龄:" <<P1.age<< endl;
}
int main()
{
	test02();
	system("pause");
	return 0;
}

 4.3.3空指针访问成员函数

#include<iostream>
#include<string>
#include"point.h"
#include"Circle.h"
using namespace std;
//class Person {
//	int a;//非静态成员变量不属于类上
//	static int b;//静态成员变量属于类
//	void fun();//非静态成员函数不属于类
//	static void fun1();//静态成员函数不属于类;
//
//};
class Person
{
public:
	void showname()
	{
		cout << "打印名称" << endl;
	}
	void showage()

	{
		if (this == NULL)//加判断来增强代码的健壮性,当指针为NULL时,return不返回任何值
		{
			return;
		}
		cout << "年龄:" << m_age << endl;//相当于this->m_age:当this为NULL时,不能访问成员变量
	}
	int m_age;
};
void test01()
{
	Person *P=NULL;
	P->showage();
}

int main()
{
	test01();
	system("pause");
	return 0;
}

4.3.4const修饰成员函数

 

#include<iostream>
#include<string>
#include"point.h"
#include"Circle.h"
using namespace std;

class Person
{
public:
	
	void showage()const//加const的成员函数被称为常函数

	{
		b = 90;
		//m_age = 100;//成员函数后面加指针修饰的是this,故此时this指向的值也不能改变
		//系统默认是this->m_age=100;
		//this=NULL;//错误写法,this本质为指针常量。即指针的指向不能修改;Person*const this
		//若想让指针指向的值也不能修改,要在前面加一个const修饰变为const Person*const this,即在函数的后面加const变为常函数
	}
	void fun()
	{

	}
	int m_age;
	mutable int b;//mutable关键字可以使得常函数改变该成员变量;
};
void test01()
{
	Person p;
}
void test02()
{
	const Person P;
	//P.m_age = 10;
	P.b = 100;//常对象只能访问mutable修饰的成员变量;
	//P.fun();//常对象只能调用常函数


}

int main()
{
	test02();
	system("pause");
	return 0;
}

 

 

黑马程序员匠心的笔记涉及多个方面的内容,具体如下: - **职工管理系统**:在`workerManager.cpp`构造函数中可追加测试代码,用于显示职工信息。代码如下: ```cpp //测试代码 for(int i=0;i<this->m_EmpNum;i++) { cout << "职工编号: " << this->m_EmpArray[i]->m_Id << " 姓名: " << this->m_EmpArray[i]->m_Name << " 部门编号: " << this->m_EmpArray[i]->m_DeptId << endl; } ``` 该代码的功能为显示职工信息 [^1]。 - **对象成员的构造析构顺序**:当类中成员是其他类对象时,该成员被称为对象成员。构造顺序是先调用对象成员的构造,再调用本类构造;析构顺序与构造相反。示例代码如下: ```cpp class Phone { public: Phone(string name) { m_PhoneName = name; cout << "Phone构造" << endl; } ~Phone() { cout << "Phone析构" << endl; } string m_PhoneName; }; class Person { public: //初始化列表可以告诉编译器调用哪一个构造函数 Person(string name, string pName) :m_Name(name), m_Phone(pName) { cout << "Person构造" << endl; } ~Person() { cout << "Person析构" << endl; } void playGame() { cout << m_Name << " 使用" << m_Phone.m_PhoneName << " 牌手机! " << endl; } string m_Name; Phone m_Phone; }; void test01() { //当类中成员是其他类对象时,我们称该成员为 对象成员 //构造的顺序是 :先调用对象成员的构造,再调用本类构造 //析构顺序与构造相反 Person p("张三" , "苹果X"); p.playGame(); } int main() { test01(); system("pause"); return 0; } ``` 此代码展示了包含对象成员的类在构造析构时的顺序情况 [^2]。 - **结构体数据分配与打印**:通过结构体嵌套的方式,为教师学生分配数据并打印。示例代码如下: ```cpp struct Student { string name; int score; }; struct Teacher { string name; Student sArray[5]; }; void allocateSpace(Teacher tArray[] , int len) { string tName = "教师"; string sName = "学生"; string nameSeed = "ABCDE"; for (int i = 0; i < len; i++) { tArray[i].name = tName + nameSeed[i]; for (int j = 0; j < 5; j++) { tArray[i].sArray[j].name = sName + nameSeed[j]; tArray[i].sArray[j].score = rand() % 61 + 40; } } } void printTeachers(Teacher tArray[], int len) { for (int i = 0; i < len; i++) { cout << tArray[i].name << endl; for (int j = 0; j < 5; j++) { cout << "\t姓名:" << tArray[i].sArray[j].name << " 分数:" << tArray[i].sArray[j].score << endl; } } } int main() { srand((unsigned int)time(NULL)); //随机数种子 头文件 #include <ctime> Teacher tArray[3]; //老师数组 int len = sizeof(tArray) / sizeof(Teacher); allocateSpace(tArray, len); //创建数据 printTeachers(tArray, len); //打印数据 system("pause"); return 0; } ``` 该代码实现了为教师学生分配数据,并将其打印输出 [^3]。 - **构造函数与拷贝构造函数**:用户定义有参构造函数无参构造函数,若不提供拷贝构造函数,编译器会提供默认构造函数。示例代码如下: ```cpp #include <iostream> using namespace std; class Person { public: int m_age; Person(){ cout << "Person 默认构造函数" << endl; } Person(int age){ m_age = age; cout << "Person 有参构造函数" << endl; } // Person(const Person &p){ // m_age = p.m_age; // cout << "Person 拷贝构造函数" << endl; // } ~Person(){ cout << "Person 析构函数" << endl; } }; void func1(){ Person p; p.m_age = 10; Person p1(p); // 注释掉自己写的拷贝构造函数,查看p1的年龄属性是否正确 cout << p1.m_age << endl; } int main(){ func1(); system("pause"); return 0; } ``` 此代码展示了在不同构造函数定义情况下对象的创建情况 [^4]。 - **敲桌子案例**:从 1 开始数到数字 50,如果数字个位含有 7,或者数字十位含有 7,或者该数字是 7 的倍数,打印敲桌子及其对应的数字。示例代码如下: ```cpp #include <iostream> using namespace std; int main() { for (int i = 1; i <= 50; i++) { if (i % 10 == 7 || i / 10 % 10 == 7 || i % 7 == 0) { cout << "敲桌子,此时的数字为:"<<i << endl; } } system("pause"); return 0; } ``` 该代码实现了敲桌子案例的功能 [^5]。
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值