C++类详解(public、private、protected)

二、C++类的声明

  类使用class关键字声明,声明方法如下:

class 类名:
{
   public://公有成员 
       int num;
   private:私有成员
       int age;
   protected:保护成员
       int sex;
};

三、类的属性public、private和protected

  • 类的public成员可以被任意实体访问,你可以认为它就是c语言中的struct结构体,可以直接用a.x这种形式访问;
  • 类的private成员不能直接被类的实体访问,也不能被子类的实体访问,但是可以被类的成员函数访问;
  • 类的protected成员不能直接被类的实体访问,但是可以被子类访问,也可以被类的成员函数访问;

假设有如下类:

class A
{
	public:
	    int age;
	private:
	    int weight;
	protected:
	    int hight;
};

3.1、首先我们尝试访问class A中的public成员:

int main()
{
	class A personA;
	class B personB;
	personA.age = 16;
	
	cout<<"personA age = "<<personA.age<<endl;
}

执行G++ class.cpp后执行./a.out得到的运行结果如下,从结果可以看出我们可以直接访问public成员变量:
在这里插入图片描述

3.2、接下来我们尝试直接访问private和protected的成员

int main()
{
	class A personA;
	class B personB;
	personA.weight = 16;
	personA.hight = 200;
}

执行结果如下,系统提示weight变量为private私有,hight变量为protected被保护,均不能被访问
在这里插入图片描述

3.2、可以在public成员中定义函数改变private和protected的数据

class A
{
	public:
	    int age;
		void SetWeight(int x){weight = x;}
		void SetHight(int x){hight = x;}
		int GetWeight(){return weight;}
		int GetHight(){return hight;}
	private:
	    int weight;
	protected:
	    int hight;
};
int main()
{
	class A personA;
	class B personB;
	personA.SetWeight(16);;
	personA.SetHight(200);
	cout<<"personA weight = "<<personA.GetWeight()<<endl;
	cout<<"personA hight = "<<personA.GetHight()<<endl;
}

执行结果:
在这里插入图片描述

四、类的成员函数

  类的成员函数,说白了就是定义在类中的函数,我们可以用访问类变量相同的方法访问类的成员函数。类的成员函数可以对类内部的protected数据和private数据进行访问。
4.1、类的成员函数声明方法

  • 类的成员函数可以直接在类的内部声明,这适用于函数相对简短的情况;
class A
{
	public:
	    int age;
		void SetWeight(int x){weight = x;}//类的成员函数,设置private数据
		void SetHight(int x){hight = x;}//类的成员函数,设置protected数据
		int GetWeight(){return weight;}
		int GetHight(){return hight;}
	private:
	    int weight;
	protected:
	    int hight;
};
  • 类的成员函数可以在类的内部声明,在类的外部定义,格式如下:

返回类型 类名::函数名(函数参数)

class A
{
	public:
	    int age;
		void SetWeight(int x);
		void SetHight(int x);
		int GetWeight(){return weight;}
		int GetHight(){return hight;}
	private:
	    int weight;
	protected:
	    int hight;
};

void A::SetWeight(int x)
{
	weight = x;
}
void A::SetHight(int x)
{
	hight = x;
}

五、类占用的内存

  • c++类占用的内存计算方法和c语言中的结构体计算方法相同,占用空间总大小等于其所有成员占用空间的总和;
  • 类中的成员函数不占用类的空间;
#include <iostream>
using namespace std;

class A
{
	public:
	    int age;//4byte
		void SetWeight(int x){weight = x;}
		void SetHight(int x){hight = x;}
		int GetWeight(){return weight;}
		int GetHight(){return hight;}
	private:
	    int weight;//4byte
	protected:
	    int hight;//4byte
};
class B:public A
{
	public:
	    int GetPersonAHight(){return hight;}
};
int main()
{
	class A personA;
	class B personB;
	personA.SetWeight(16);;
	personA.SetHight(200);
	cout<<"sizeof(class A) = "<<sizeof(class A)<<endl;
}

运行结果:
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-hpB1UsCq-1600421587495)(A33D22023249479C8E4F2CBF47E13C9E)]

  • 80
    点赞
  • 335
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值