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 ...
随机推荐
- PatentTips - Wear Leveling for Erasable Memories
BACKGROUND Erasable memories may have erasable elements that can become unreliable after a predeterm ...
- gerrit ssh 登陆设置
[root@web ~]# cat ~/.ssh/config Host gerrit User deploy-gerrit Port Hostname gerrit.demo.com Identit ...
- 免费第三方API平台整合
各大平台免费接口,非常适用 http://developer.51cto.com/art/201412/458778.htm 绝对干货:供个人开发者赚钱免费使用的一些好的API接口http://www ...
- 洛谷—— P1462 通往奥格瑞玛的道路
https://www.luogu.org/problem/show?pid=1462 题目背景 在艾泽拉斯大陆上有一位名叫歪嘴哦的神奇术士,他是部落的中坚力量 有一天他醒来后发现自己居然到了联盟的主 ...
- topcoder 650 srm
500 遇到这种构造题 就给跪了 比赛的时候想很多方法 DP,贪心,模拟 发现越写越烦琐.看到别人出这么快,肯定又是奇葩思路. 后来居然想到 2^50的暴力 +剪枝 不过暴力肯定卡你 IDEA: 只要 ...
- 地球Gauss_Kruger中央0度经线图
- 共享内存mmap学习 及与 shmxxx操作的区别
上一篇学习了共享内存: http://www.cnblogs.com/charlesblc/p/6142139.html 根据这个 http://blog.chinaunix.net/uid-2633 ...
- 数据库(Mysql)背后的数据结构-学习
来吧,用这三篇文章夯实对Mysql的理解吧. 关于数据库索引及其优化,更多可参见此文:http://www.cnblogs.com/pkuoliver/archive/2011/08/17/mass- ...
- VB程序逆向反汇编常见的函数(修改版)
VB程序逆向常用的函数 1) 数据类型转换: a) __vbaI2Str 将一个字符串转为8 位(1个字节)的数值形式(范围在 0 至 255 之间) 或2 个字节的数值形式(范围在 -32,7 ...
- [Analytics] Add Tealium debugger in Chrome
It would be helpful once you can see what information have been tracking inside you web application, ...