c语言struct和c++的class的暧昧
c语言风格的封装 数据放在一起,以引用和指针的方式传给行为
c++ 认为封装不彻底
1数据和行为分开 对外提供接口
2没有权限设置
看看struct的一个例子
//data.h //c语言风格的封装 数据放在一起,以引用和指针的方式传给行为
//c++ 认为封装不彻底
//1数据和行为分开 对外提供接口
//2没有权限设置 struct Date
{
int year;
int month;
int day;
}; void init(Date &d);
void print(Date &d);
bool isLeapVear(Date &d);
//data.cpp #include <iostream>
#include "data.h"
using namespace std; void init(Date &d)
{
cin>>d.year;
cin>>d.month;
cin>>d.day;
} void print(Date &d)
{
cout<<"year="<<d.year<<"month="<<d.month<<"day="<<d.day<<endl;
}
bool isLeapVear(Date &d)
{
if(d.year%==||d.year%!=||d.year%==)
return true;
else
return false;
}
//strut.cpp #include <iostream>
#include "data.h"
using namespace std;
//2017/1/14 c语言风格的封装 数据放在一起,以引用和指针的方式传给行为 int main()
{
Date d;//此时才会开辟空间
init(d);
print(d);
if(isLeapVear(d))
{
cout<<"d.year"<<"is a leap year"<<endl;
}else
cout<<"d.year"<<"is not a leap"<<endl;
return ;
}
然后再看看c++的class
//data.h #ifndef DATE_H
#define DATE_H namespace space
{
class Date{
public:
void init();
void print();
bool isLeapVear();
int getyear();
int getday();
int getmonth(); private:
int year;
int month;
int day;
};
} #endif
1 //data.cpp
2
3 #include <iostream>
4 #include "data.h"
5 using namespace std;
6 namespace space
7 {
8 void Date:: init()
9 {
10 cin>>year;
11 cin>>month;
12 cin>>day;
13 }
14 int Date::getyear()
15 {
16 return year;
17 }
18 int Date:: getmonth()
19 {
20 return month;
21 }
22 int Date:: getday()
23 {
24 return day;
25 }
26 bool Date:: isLeapVear()
27 {
28 if(year%4==0||year%100!=0||year%400==0)
29 return true;
30 else
31 return false;
32 }
33
34 void Date:: print()
35 {
36 cout<<"year:"<<year<<"month"<<month<<"day"<<day<<endl;
37 }
38 }
1 //main.cpp
2
3 #include <iostream>
4 #include "data.h"
5 using namespace std;
6 using namespace space;
7 //2017/1/14
8
9 //1 增加了权限控制 相对于c
10 //private:
11 //public:
12 //2 数据和行为在一起,对本类是开放的 对外提供接口
13
14 //声明很实现要分开
15
16
17 //class MM
18 //{
19 //public:
20 // void init();
21 //};
22 //void MM::init();这两个类都有init 所以需要域名
23 int main()
24 {
25 Date d;
26 //d.year = 300;//直接访问不到 默认为私有的 修改为共有 则可以访问
27 d.init();
28 d.isLeapVear();
29 d.print();
30 if(d.isLeapVear())
31 {
32 cout<<d.getyear()<<"is a leap year"<<endl;
33 }else
34 cout<<d.getyear()<<"is not a leap"<<endl;
35 return 0;
36 }
再不用各种传参。。。。。
c语言struct和c++的class的暧昧的更多相关文章
- C语言struct小知识
1.C语言里的struct是不能包含成员函数的,只能有数据成员2.C语言struct定义变量只能用一下两种方式:struct { ... } x, y, z;struct point pt;直接poi ...
- C语言struct类型
在实际问题中,一组数据往往具有不同的数据类型.例如, 在学生登记表中,姓名应为字符型:学号可为整型或字符型: 年龄应为整型:性别应为字符型:成绩可为整型或实型. 显然不能用一个数组来存放这一组数据. ...
- C语言 Struct 结构体在 Java 中的体现
大一整个学期完成了 C 语言的学习,大二就进入了Java 的学习. 和C语言一样,我们都会尝试写一个小小的学生管理系统什么的,学习过 C 语言同学知道,在管理系统中 Struct 结构体是个很好用的东 ...
- c语言struct和c++struct的区别
1.定义 c语言中struct是用户自定义数据类型(UDT),是一些变量的集合体:c++中struct是抽象数据类型(ADT),能给用户提供接口,能定义成员函数,能继承,能实现多态 2.成员权限设置 ...
- 关于c语言struct和typedef
C++中使用: struct test{ int x, y;};就可以定义一个名为 test的结构体,但C中很可能编译通不过.C语言并不支持在struct后使用标示符定义结构体的名字,test将 ...
- CC+语言 struct 深层探索——CC + language struct deep exploration
1 struct 的巨大作用 面对一个人的大型C/C++程序时,只看其对struct 的使用情况我们就可以对其编写者的编程经验进行评估.因为一个大型的C/C++程序,势必要涉及一些(甚至 ...
- C 语言Struct 实现运行类型识别 RTTI
通过RTTI,能够通过基类的指针或引用来检索其所指对象的实际类型.c++通过下面两个操作符提供RTTI. (1)typeid:返回指针或引用所指对象的实际类型. (2)dynamic_cast: ...
- c语言 struct 的初始化
转自:http://www.cnblogs.com/silentjesse/archive/2013/07/30/3225212.html struct数据有3中初始化方法:顺序,C风格及C++风格的 ...
- 内存对齐-C语言struct内存占用问题
转1个写的比较全面的. http://hubingforever.blog.163.com/blog/static/17104057920122256134681/ 本文编辑整理自:http://hi ...
随机推荐
- 楼房重建(bzoj 2957)
Description 小A的楼房外有一大片施工工地,工地上有N栋待建的楼房.每天,这片工地上的房子拆了又建.建了又拆.他经常无聊地看着窗外发呆,数自己能够看到多少栋房子. 为了简化问题,我们考虑这些 ...
- [Bzoj1297][Scoi2009 ]迷路 (矩阵乘法 + 拆点)
1297: [SCOI2009]迷路 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 1385 Solved: 993[Submit][Status] ...
- 寒武纪camp Day6
补题进度:10/10 A(树形dp) 略 B(dp) 题意: 给出一个n个关键节点的机械手臂,最开始是竖直的,即关键点在二维平面上的坐标分别是(0,0) (0,100) (0,200) (0,300) ...
- 转:SIP相关的RFC文档索引
索引来源于http://www.packetizer.com/ipmc/sip/standards.html SIP Standards Core SIP Documents RFC Document ...
- Win7中你所应该知道的强制计划关机操作
有时候更新系统补丁时,需要很长时间,为了能让电脑在你晚上睡觉后,扔然能做些枯燥费时类的这种工作,你可以用到强制计划关机.cmd命令是: shutdown -f -s -t 3600 上面的意思是,强制 ...
- 代理服务器squid简介
Squid 是一个高性能.开源的代理缓存服务器和 Web 缓存进程,支持 FTP.Internet Gopher.HTTPS 和 SSL 等多种协议.它通过一个非阻塞的.I/O 事件驱动的单一进程处理 ...
- Error Code: 2006 - MySQL 鏈嶅姟鍣ㄥ凡绂荤嚎
将sql文件导入到mysql时候,就一直报这个错误. 我试过网上各种方法都行不通. 最后将以下一句运行了一下就能够了,并且没有重新启动mysql. SET GLOBAL max_allowed_pac ...
- react 路由 react-router@3.2.1
react路由,4.x的差异还是比较大,暂时还是3.x的版本 安装: npm install -S react-router@3.x 配置: import { Router, Route, hashH ...
- M公司入职记
非常遗憾,我又跳槽了,到传说中的M公司,第一天就体会到了,神马叫差距. 要求9点30到,提前10分钟到了前台.前台MM懵懂的跟我说入职找人事,好吧. 电话联系相关人等,等到10点左右,被引导到一位不知 ...
- 在开发过程中,如何在手机上测试vue-cli构建的项目
由于有时候谷歌手机调试与真是的手机环境还是有一定的差距,所以需要在手机上测试项目. 手机上测试vue-cli构建项目方法: 打开项目config/index.js文件,找到module.exports ...