1.使用#include分离函数的定义与实现
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并不要求一定同名    
 
分开写函数实现时需要告诉编译器函数属于谁的,余姚在函数名前+类名::        
 
2.访问修饰符public和private
public的意思是,这个成员哪里都可以用,不用担心被修改,所以,一旦发布成public的成员,是不能够改名字的.                            
private的意思是,这个成员只用于内部使用,不要在其他的地方使用.                            
 
总结:
    1】对外提供的函数或者变量,发布成public的 但不能随意改动.                            
    2】可能会变动的函数或者变量,定义成private的 这样编译器会在使用的时候做检测.                            
    3】只有结构体内部的函数才可以访问private的成员.                            
    4】public/private可以修饰函数也可以修饰变量.                            
 
private修饰的成员与普通的成员没有区别 只是编译器会检测.                        
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(,); int* p = (int*)&t;
int n = *p;
int m = *(p+);
printf("%d %d\n",n,m);
3.class和struct
class关键字和struct一样也能用来定义结构;
class Base
{
int x;
int y;
}; int main(int argc, char* argv[])
{
Base base; base.x = ;
base.y = ; return ;
}
class和struct的区别:
    编译器默认class中的成员为private 而struct中的成员为public ;
 
class的继承:
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 = ; //无法访问
sub.y = ; //无法访问
sub.a = ;
sub.b = ; return ;
} 父类中的程序继承后变成private属性;
也就是默认:
class Sub:private Base
{
public:
int a;
int b;
};
如果不希望改变成员的属性,需要在继承时声明public:
class Sub:public Base
{
public:
int a;
int b;
};
如果父类class中的成员是private,也是能被继承的;
只是编译器不允许直接访问;
例如:强行访问父类private成员
class Base
{
public:
Base()
{
x = ;
y = ;
}
private:
int x;
int y;
}; class Sub:Base
{
public:
int a;
int b;
}; int main(int argc, char* argv[])
{
Sub sub;
sub.a = ;
sub.b = ; int* p = (int*)⊂ printf("%d\n",sizeof(sub));
printf("%d\n",*(p+));
printf("%d\n",*(p+));
printf("%d\n",*(p+));
printf("%d\n",*(p+)); return ;
}
 

随机推荐

  1. Feign【开启GIZP压缩】

    SpringCloudFeign支持对请求和响应进行gzip压缩,以此来提高通信效率. 1.搭建gzip-demo工程 1.1.工程依赖: <parent> <groupId> ...

  2. 串口(USART)通信-串口通讯协议简介

    物理层:规定通讯系统中具有机械.电子功能部分的特性,确保原始数据在物理媒体的传输.其实就是硬件部分. 协议层:协议层主要规定通讯逻辑,统一收发双方的数据打包.解包标准.其实就是软件部分. 简单来说物理 ...

  3. Yii2 设计模式——单例模式

    单例模式 模式定义 单例模式确保一个类只有一个实例,并提供一个全局访问点.当现实中只需要一个对象,或者为了节省系统资源,又或者是为了共享数据的时候可以使用单例模式. 代码实现 我们先来看看单例模式的标 ...

  4. 【百度之星2019】Strassen

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=6719 在本题中,我们只有两种方法计算两个的矩阵的乘积,第一种为定义法,需要次乘法和次加法.第二种为 ...

  5. 怎样理解数组的空元素empty与undefined的区别

    数组的空元素empty表示空位, 它不是一种数据类型, 而是由于人为修改arr.length 或者写入时多写了逗号造成的. var arr = [1,2,3,4,,,5]; arr.length; a ...

  6. sping+redis实现消息队列的乱码问题

    使用spring支持redis实现消息队列,参考官方样例:https://spring.io/guides/gs/messaging-redis/ 实现后在运行过程中发现消费者在接收消息时会出现乱码的 ...

  7. (三)使用Intent在活动中穿梭:显式和隐式Intent

    一.显式Intent @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstan ...

  8. “org/apache/commons/logging/LogFactory”错误的解决方式

    用spring-framework-4.2.6.RELEASE-dist时,发生了如下的错误: [java] view plain copy Exception in thread "mai ...

  9. luogu2657-Windy数题解--数位DP

    题目链接 https://www.luogu.org/problemnew/show/P2657 分析 第一道数位DP题,发现有点意思 DP求\([L,R]\)区间内的XXX个数,很套路地想到前缀和, ...

  10. springboot启动流程(十)springboot自动配置机制

    所有文章 https://www.cnblogs.com/lay2017/p/11478237.html 正文 在第七篇文章中我们了解到,refresh过程将会调用ConfigurationClass ...