C++学习(十)—类和对象(五)

类的大小

#include<iostream>
#pragma pack(show)	//  对齐模式


using namespace std;


class Person
{
public:
	int a;  //  成员属性  算在类的大小中


	//	成员函数  并不算在类的大小中
	void f()
	{

	}

	static int b;  //  静态成员变量  也不属于类的大小中

	static void f2()  //  静态成员函数  不属于类的大小中
	{

	}

	double c;
};

int Person::b = 0;


void test01()
{
	//  空类的大小为:1
	//  空也是可以实例化对象的,每个对象在内存中都应该有独一无二的地址
	//  Person p[10];
	cout << sizeof(Person) << endl;

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

	Person p1;

	p1.f();

	Person p2;

	p2.f();
}



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

this指针

#include<iostream>

using namespace std;

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

	void  showage()
	{
		cout << "年龄:" << this->age << endl;
	}

	Person& Addage(Person &p)
	{
		this->age += p.age;

		return *this;
	}
};

void test01()
{ 
	Person p1(18);

	//	cout << "p1的年龄为:" << p1.age << endl;

	p1.showage();

	Person p2(10);

	p1.Addage(p2).Addage(p2).Addage(p2);  //  链式编程

	p1.showage();
}

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

空指针访问成员函数‘

#include<iostream>

using namespace std;


//  空指针可以访问没有this的一些成员函数  如果函数中用到了this指针  程序会崩掉

class Person
{
public:

	void showClassName()
	{
		cout << "class Name is Person" << endl;
	}

	void showAge()
	{
		//  NULL -> p_age
		cout << "age = " << this->p_age << endl;
	}



	int p_age;
};


void test01()
{
	/*Person p1;
	p1.p_age = 18;
	p1.showClassName();
	p1.showAge();*/

	Person *p1 = NULL;
	//p1->showClassName();
	p1->showAge();

}




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

常函数和常对象

#include<iostream>

using namespace std;

class Person
{
public:
	void showPerson()const	//  成员函数声明后面加 const  代表常函数,不可以修改成员属性了
	{
		//  Person *  const this  
		cout << this->m_a << endl;
		//	this = NULL;  //  this指针的本质  是一个指针常量,指针的指向是不可以被修改的,指针指向的值是可以修改的

		//  const Person *  const this

		this->m_a = 10;
		this->m_b = 10;
	
	}
	void showPerson2()
	{
		;
	}


	int m_a;
	mutable int m_b;  //  即使是常函数  想要修改的话要在前面加  mutable
protected:
private:
};


//	常函数
void test01()
{
	Person p1;
	p1.m_a = 20;
	p1.showPerson();
}

//	常对象
void test02()
{
	const Person p1;//  常对象  常对象是不可以修改的

	p1.m_a = 10;

	p1.m_b = 20;  //  加上mutable关键字修饰的m_b是特殊属性,即使是常函数或者是常对象  都是可以修改的

	p1.showPerson();	//  常对象只能调用常函数

	p1.showPerson2();  //  常对象是不可以调用普通的成员函数的

}


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

this指针的本质是一个指针常量,指针的指向是不可以被修改的,指针指向的值是可以修改的

友元

全局函数作为友元函数

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

//  全局函数做友元函数

class Building
{
	//  有一个全局函数  作为本类的友元函数,可以访问到私有内容
	friend void goodGay(Building &buliding);
public:
	Building()
	{
		this->m_bedrom = "卧室";
		this->m_sittingRom = "客厅";
	}

	string m_sittingRom;

private:
	
	string m_bedrom;
	
};


void goodGay(Building &buliding)
{
	cout << "好基友正在访问" << buliding.m_sittingRom << endl;

	cout << "好基友正在访问" << buliding.m_bedrom << endl;
}



void test01()
{
	Building b;
	goodGay(b);
}



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

有一个全局函数 作为本类的友元函数,可以访问到私有内容
friend void goodGay(Building &buliding);

类作为友元类

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


class Building;


class goodGay
{
public:
	goodGay();

	void visit();
private:
	Building *buliding;
};

class Building
{
	//  告诉编译器  goodGay类是本类的有元,可以访问到里面私有内容
	friend class goodGay;
public:
	Building();
	string m_sittingrom;
private:
	string m_beding;
};

void test01()
{
	goodGay g1;
	g1.visit();
}



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

goodGay::goodGay()
{
	this->buliding = new Building;
}

void goodGay::visit()
{
	cout << "好基友类正在访问" << this->buliding->m_sittingrom << endl;
	cout << "好基友类正在访问" << this->buliding->m_beding << endl;
}


Building::Building()
{
	this->m_beding = "卧室";
	this->m_sittingrom = "客厅";
}

类中的成员函数作为有元函数

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


class Building;


class goodGay
{
public:
	goodGay();
	void visit();//  visit可以访问Building中私有属性
	void visit2();  //  visit2 不可以访问Building中私有属性
private:
	Building *buliding;
};

class Building
{
	//  告诉编译器  goodGay类中的成员函数可以访问本类中的私有内容
	friend void goodGay::visit();
public:
	Building();
	string m_sittingrom;
private:
	string m_beding;
};

void test01()
{
	goodGay g1;
	g1.visit();
	g1.visit2();
}



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

goodGay::goodGay()
{
	this->buliding = new Building;
}

void goodGay::visit()
{
	cout << "好基友类正在访问" << this->buliding->m_sittingrom << endl;
	cout << "好基友类正在访问" << this->buliding->m_beding << endl;
}

void goodGay::visit2()
{
	cout << "坏基友类正在访问" << this->buliding->m_sittingrom << endl;
	cout << "坏基友类正在访问" << this->buliding->m_beding << endl;
}


Building::Building()
{
	this->m_beding = "卧室";
	this->m_sittingrom = "客厅";
}
发布了31 篇原创文章 · 获赞 8 · 访问量 3667

猜你喜欢

转载自blog.csdn.net/qq_42689353/article/details/104590640