c++权限控制

文章讲述了C语言中如何在头文件中定义函数并在源文件中实现,强调了函数定义与实现分离对代码可读性的提升。同时,介绍了public和private关键字在类中的作用,public成员对外公开,private成员限制访问,以及struct和class的区别,特别是继承时的权限差异。
摘要由CSDN通过智能技术生成

定义与实现分离

c语言可以在xxx.h中定义函数,然后在xxx.cpp中实现函数;

在需要用到这些函数时,只要用#include引入xxx.h即可,这样就不用将所有代码全部写在一个cpp中;

将函数定义与实现分离,代码会有更好的可读性但不是必须的 ;

	xxx.h 头文件中				
					
	struct Test				
	{				
		int x;			
		int y;			
		int z;			
					
		void Init(int x,int y,int z);			
		void Function1();			
		void Function2();			
		void Function3();			
	};				
					

	xxx.cpp					
						
	void Test::Init(int x,int y,int z)					
	{					
		this->x = x;				
		this->y = y;				
		this->z = z;				
	}					
	void Test::Function1()					
	{					
		printf("Function1:%x\n",x);				
	}					
	void Test::Function2()					
	{					
		printf("Function2:%x\n",y);				
	}					
	void Test::Function3()					
	{					
		printf("Function3:%x\n",z);				
	}					
						
						

特别说明:

1、xxx.h 只是一个文件,可以是任何的后缀名,如果你愿意,
可以叫xxx.exe

2、#include 的作用只是把里面的内容复制过来 仅此而已.

如:#include “abc.exe”

3、xxx.h 与 xxx.cpp并不要求一定同名

4、分开写函数实现时需要告诉编译器函数属于谁的,需要在函数名前+类名::

public和private

使用

public的意思是,这个成员哪里都可以用,不用担心被修改,所以,一旦发布成public的成员,是不能够改名字的.

private的意思是,这个成员只用于内部使用,不要在其他的地方使用.

总结:

1、对外提供的函数或者变量,发布成public的 但不能随意改动
2、可能会变动的函数或者变量,定义成private的 这样编译器会在使用的时候做检测.
3、只有结构体内部的函数才可以访问private的成员.
4、public/private可以修饰函数也可以修饰变量.

private成员的访问

	struct Test				
	{				
	private:				
		int x;			
	public:				
		int y;			
		void Init(int x,int y)			
		{			
			this->x = x;		
			this->y = y;		
		}			
	};				
					
					
					
					
	Test t;				
	t.Init(1,2);				
					
					
	int* p = (int*)&t;				
					
	int n = *p;				
					
	int m = *(p+1);				
					
	printf("%d %d\n",n,m);				
					

可以通过指针访问

(img-R4VyGqgW-1681133752358)(C:\Users\whl\AppData\Roaming\Typora\typora-user-images\image-20230224125216174.png)]

总结:

private修饰的成员与普通的成员没有区别 只是编译器会检测.

private修饰的成员只要自己的其他成员才能访问

class和struct

class与struct的区别:

  1. 成员中的区别
    编译器默认class中的成员为private 而struct中的成员为public
  2. 继承中的区别
class Base            
{            
public:            
    int x;        
    int y;        
}; 
           
class Sub:Base            
{            
public:            
    int a;        
    int b;        
}; 
           
int main(int argc, char* argv[])            
{            
    Sub sub;        
            
    sub.x = 1;  //无法访问        
    sub.y = 2;  //无法访问        
    sub.a = 3;        
    sub.b = 4;        
            
    return 0;        
}            
 
 
父类中的程序继承后变成private属性;    
也就是默认:
class Sub:private Base    
{    
public:    
    int a;
    int b;
};    
如果不希望改变成员的属性,需要在继承时声明public:   
class Sub:public Base    
{    
public:    
    int a;
    int b;
};

父类class中的私有成员也会被继承,只是编译器不允许直接访问

使用指针访问private:

#include"c++test.h"
class Base
{
private:
	int x;
	int y;
public:
	Base()
	{
		x = 10;
		y = 20;
	}
};
class Sub :public Base
{
public:
	int a;
	int b;
public:
	Sub()
	{
		a = 1;
		b = 2;
	}
};

int main()
{
	Sub ss;
	int* p = (int*)&ss;
	printf("%d\n", sizeof(Sub));
	printf("%d\n", *(p + 0));
	printf("%d\n", *(p + 1));
	printf("%d\n", *(p + 2));
	printf("%d\n", *(p + 3));

}

练习

把上节的作业中struct换成class

#include"c++test.h"
class DateInfo
{
private:
	int year;
	int month;
	int day;
public:
	DateInfo(int year, int month, int day) {
		this->year = year;
		this->month = month;
		this->day = day;
		printf("%d\n", month);
	}
	DateInfo()
	{
		this->year = 2015;
		this->month = 4;
		this->day = 2;
		putchar(month);
	}
	void SetDay(int day)
	{
		this->day = day;
	}
	int GetDay()
	{
		return this->day;
	}
	void Setmonth(int month)
	{
		this->month = month;
	}
	int Getmonth()
	{
		return this->month;
	}
	void Setyear(int year)
	{
		this->year = year;
	}
	int Getyear()
	{
		return this->year;
	}
};
struct TimeInfo :public DateInfo
{
private:
	int hour;
	int minute;
	int second;
public:
	TimeInfo()
	{
		this->hour = 60;
		this->minute = 50;
		this->second = 40;
	}
	void Sethour(int hour)
	{
		this->hour = hour;
	}
	int Gethour()
	{
		return this->hour;
	}
	void Setminute(int minute)
	{
		this->minute = minute;
	}
	int Getminuteh()
	{
		//printf("%d",this->month);
		return this->minute;
	}
	void Setsecond(int second)
	{
		this->second = second;
	}
	int Getsecond()
	{
		return this->second;
	}
};
void test()
{
	TimeInfo time;

	DateInfo* dp = &time;
	printf("%d %d %d\n", dp->GetDay(), dp->Getmonth(), dp->Getyear());
	TimeInfo* tp = &time;
	printf("%d %d %d\n", tp->GetDay(), tp->Getmonth(), tp->Getyear());
	printf("%d %d %d\n", tp->Gethour(), tp->Getminuteh(), tp->Getsecond());

}
int main(int argc, char* argv[])
{
	test();
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值