黑马教程学习笔记——对象特性(2)

1、类对象作为类成员

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

class Phone
{
public:
	Phone(string pName)
	{
		cout << "Phone构造函数调用" << endl;
		m_PName = pName;
	}
	~Phone()
	{
		cout << "Phone析构函数调用" << endl;
	}
	string m_PName;

};
//当其他类对象作为本类成员,构造时先构造类对象,再构造自身。析构的顺序相反
class Person
{
public:
	//Phone m_Phone =pName  隐式转换法
	Person(string name, string pName) :m_Name(name), m_Phone(pName)
	{
		cout << "Person构造函数调用" << endl;
	}
	~Person()
	{
		cout << "Person析构函数调用" << endl;
	}
	string m_Name;
	Phone m_Phone;
	
};

void test01()
{
	Person p("张三", "苹果");
	cout << p.m_Name << p.m_Phone.m_PName << endl;
}
int main()
{
	test01();
	system("pause");
	return 0;
}

2、静态成员访问权限

#include <iostream>
#include<string>
using namespace std;
class Person
{
public:
	//静态成员函数特点:
	//1、程序共享一个函数
	//2、静态成员函数只能访问静态成员变量
	static void func()
	{
		cout << "func调用" << endl;
		m_A = 100;//静态成员函数可以访问 静态成员变量
		cout << m_A << endl;
		//m_B = 100;//错误,不可以访问非静态成员变量
	}
	static int m_A;
	//int m_B;

private:

	//静态成员函数也是有访问权限的
	static void func2()
	{
		cout << "func2的调用" << endl;
	}
};
int Person::m_A = 0;
void test01()
{
	//1、通过对象
	Person p1;
	p1.func();
	//2、通过类名访问
	Person::func();

	//Person::func2();//错误 类外 私有权限访问不到静态成员函数
}
int main()
{
	test01();
	system("pause");
	return 0;
}

3、成员变量和成员函数分开储存

#include <iostream>
#include<string>
using namespace std;
class Person
{

	int m_A;//           非静态成员变量 属于类的对象上 sizeof输出结果为 4
	static int m_B;//    静态成员变量   不属于类对象上                 4
	void func(){}//      非静态成员函数 不属于类对象         输出结果为 4
	static void func(){}//静态成员函数  不属于类对象              结果  4
};

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

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

4、this指针的用途

#include <iostream>
#include<string>
using namespace std;
class Person
{
public:
	Person(int age)
	{
		//this 指针指向 被调用的成员函数 所属的对象
		this->age = age;//m_Age=age;是正确的;用age需要加this
	}
	//PersonAddAge(Person &p)
	Person & PersonAddAge(Person &p)//链式相加需改变
	{
		this->age += age;
		return *this;
	}

	int age;//int m_Age;是正确的
	
};
//1、解决名称冲突

//2、返回对象本身*this

void test01()
{
	Person p1(18);
	cout << p1.age << endl;
}
void test02()
{
	Person p2(10);
	Person p3(10);
	//p3.PersonAddAge(p2);//加一次
	p3.PersonAddAge(p2).PersonAddAge(p2);
	cout << p3.age << endl;
}
int main()
{
	//test01();
	test02();
	system("pause");
	return 0;
}

5、空指针访问成员函数

#include <iostream>
#include<string>
using namespace std;
class Person
{
public:
	void showClassName()
	{
		cout << "this is Person class" << endl;
	}

	void showPersonAge()
	{
		//报错原因 因为传入的指针为NULL
		//措施
		if (this == NULL)
		{
			return;
		}
		cout << m_Age << endl;;
	}
	int m_Age;
};
void test01()
{
	Person *p = NULL;
	//p->showClassName();
	p->showPersonAge();
}
int main()
{
	test01();
	system("pause");
	return 0;
}

6、const修饰成员函数

#include<iostream>
using namespace std;

//常函数
class Person
{
public:
	//this指针的本质 不可改变指向
	//Person * const this;  const Person * const this
	//在成员函数后面加const 修饰的是this指向 让指针指向的值也不可以修改
	void showPerson() const
	{
		//this->m_A = 100;//出错
	}
	int m_A;//加mutable可以修改

};
void test01()
{
	const Person p;//在对象前加const 变为常对象
	p.showPerson();
}
int main()
{


	system("pause");
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值