2021.8.5~8.6---3/7、4/7---C++对象模型和this指针、友元

一、C++对象模型和this指针

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

在这里插入图片描述

#include<iostream>
using namespace std;

class Person 
{
	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;
	cout << "size of p=" << sizeof(p) << endl;
}

int main()
{
	//test01();
	test02();
	return 0;
}

2.this指针概念

在这里插入图片描述

#include<iostream>
using namespace std;

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();
	return 0;
}

3.空指针访问成员函数

在这里插入图片描述

#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();
	return 0;
}

4.const修饰成员函数

在这里插入图片描述

#include <iostream>
using namespace std;

//常函数
class Person
{
public:
	//this指针的本质 是指针常量 指针的指向是不可以修改的
	//const Person * const this
	//在成员函数后加const,修饰的是this指针,让指针指向的值也不可以更改
	void showPerson() const
	{
		this->m_B = 100;
		//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();
	return 0;
}

二、友元

在这里插入图片描述

1.全局函数做友元

#include <iostream>
using namespace std;
#include <string>

//建筑物类
class Building
{
	//goodGay全局函数是 Building好朋友,可以访问Building中私有成员
	friend void goodGay(Building* building);
public:
	Building()
	{
		m_SittingRoom = "客厅";
		m_BedRoom = "卧室";
	}


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;
	goodGay(&building);
}

int main()
{
	test01();
	return 0;
}

2.类做友元

#include <iostream>
using namespace std;
#include <string>

class Building;

class GoodGay
{
public:
	GoodGay();
	void visit();//参观函数 访问Building中的属性
	Building* building;
};

class Building
{
	//GoodGay类是本类的好朋友,可以访问本类中私有成员
	friend class GoodGay;
public:
	Building();
public:
	string m_SittingRoom;//客厅
private:
	string m_BedRoom;//卧室
};

//类外写成员函数
Building::Building()
{
	m_SittingRoom = "客厅";
	m_BedRoom = "卧室";
}

GoodGay::GoodGay()
{
	//创建建筑物对象
	building = new Building;
}

void GoodGay::visit()
{
	cout << "好基友类 正在访问" << building->m_SittingRoom << endl;

	cout << "好基友类 正在访问" << building->m_BedRoom << endl;

}

void test01()
{
	GoodGay gg;
	gg.visit();
}

int main()
{
	test01();
	return 0;
}

3.成员函数做友元

#include <iostream>
using namespace std;

class Building;
class GoodGay
{
public:
	GoodGay();
	void visit();//让visit函数可以访问Building中私有成员
	void visit2();//让visit2函数不可以访问Building中私有成员

	Building* building;
};

class Building
{
	friend void GoodGay::visit();
public:
	Building();
public:
	string m_SittingRoom;//客厅
private:
	string m_BedRoom;//卧室
};

//类外实现成员函数
Building::Building()
{
	m_SittingRoom = "客厅";
	m_BedRoom = "卧室";
}

GoodGay::GoodGay()
{
	building = new Building;
}

void GoodGay::visit()
{
	cout << "visit函数正在访问" << building->m_SittingRoom << endl;
	cout << "visit2函数正在访问" << building->m_BedRoom << endl;

}
void GoodGay::visit2()
{
	cout << "visit函数正在访问" << building->m_SittingRoom << endl;
	//cout << "visit2函数正在访问" << building->m_BedRoom << endl;
}

void test01()
{
	GoodGay gg;
	gg.visit();
	gg.visit2();
}
int main()
{
	test01();
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值