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 ...
随机推荐
- CodeForces - 320B Ping-Pong (Easy Version)
题目最开始 完全不懂 配合案例也看不懂-_- 总之就是用传递性 问能否从a区间到b区间 dfs(x,y) 走遍与第x区间所有的 联通区间 最后检验 第y区是否被访问过 是一道搜索好题 搜索还需加强 # ...
- BZOJ1126: [POI2008]Uci
$n \leq 100,m \leq 100$,$n*m$的01矩形,问从左下角开始往上走,每次转弯只能向右,不能经过重复点,不能撞到1,到达点$(x,y)$的方案数,$mod \ \ k$. 感人肺 ...
- App竞品技术分析 (3)减小安装包的体积(转)
http://blog.csdn.net/JspAndAsp/article/details/49339403 1 从几件小事说起 春节在家帮姐姐的iPhone手机安装市面上形形色色的App,忘记她是 ...
- python学习之-- 面向对象
面向对象(简写:OOP) 面向对象编程定义:利用类和对象来创建各种模型,来实现对真实世界的描述. 优点:使程序更容易理解和维护以及扩展代码. 类定义:用来描述具有相同的属性和方法的对象的集合.(简单讲 ...
- [Bzoj3131][Sdoi2013]淘金(数位dp)(优先队列)
3131: [Sdoi2013]淘金 Time Limit: 30 Sec Memory Limit: 256 MBSubmit: 847 Solved: 423[Submit][Status][ ...
- arcengine 文件夹连接
Provides access to members that manages a GX catalog. Product Availability Available with ArcGIS Des ...
- Google C++ style guide——C++类
1.构造函数的职责 构造函数中仅仅进行那些没有实际意义的初始化.由于成员变量的"有意义"的值大多不在构造函数中确定. 能够的话,使用Init()方法集中初始化为有意义的数据. 长处 ...
- Visual Studio VS如何卸载Visual assistant
1 点击工具-扩展管理器 2 选中Visual Assist X,点击卸载即可.
- Pacemaker 安装与使用
Pacemaker 仅仅做资源管理器(CRM).底下的消息系统採用 corosync. 安装 以 ubuntu 为例, sudo aptitude install -y pacemaker coros ...
- SQL2012 尝试读取或写入受保护的内存。这通常指示其它内存已损坏
今天打开SQL2012,突然就连接不了数据库.一開始还以为是某个server崩溃了.结果试了好几个.都还是如此,弹出提演示样例如以下: 尝试读取或写入受保护的内存.这通常仅仅是其它内存已损坏.(Sys ...