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 ...
随机推荐
- [Usaco2006 Nov] Fence Repair 切割木板
Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 1356 Solved: 714[Submit][Status][Discuss] Description ...
- MITM Proxy环境搭建
MITM_Proxy环境搭建 环境要求 系统环境要求: Ubuntu 14.04 x64,CentOS 7 x64以上版本系统(建议使用xubuntu 14.04 x64,稳定硬件要求低) Pytho ...
- 修改flex chart中Legend的字体样式
最近在弄FLEX的图表, 发现CHART 中的Legend 的字体通过直接设置Style 并没有办法改变字体大小. google 了下, 发现了这个方法: 通过派生LegendItem类,并设置Leg ...
- Last Defence - UVA7045
https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_probl ...
- 洛谷——P1038 神经网络
P1038 神经网络 题目背景 人工神经网络(Artificial Neural Network)是一种新兴的具有自我学习能力的计算系统,在模式识别.函数逼近及贷款风险评估等诸多领域有广泛的应用.对神 ...
- eclipse中安装maven插件
原文:http://blog.csdn.net/wode_dream/article/details/38052639 当自己越来越多的接触到开源项目时,发现大多数的开源项目都是用maven来够建的. ...
- TeamCity - Docker创建
// 创建Server docker run -it --name teamcity-server-instance \-v /home/tc_datadir:/data/teamcity_serve ...
- Python中文GBK编码解决实例
http://eatsalt.blog.163.com/blog/static/879402662009420508748/ #coding:gbk l=['我'.decode('gbk'),'我'. ...
- 【独立开发人员er Cocos2d-x实战 008】BMFont生成位图字体工具和Cocos2dx使用载入fnt文件
1.首先我们须要下载而且安装BMFont工具,下载地址例如以下:http://download.csdn.net/detail/chenqiai0/8899353(里面还有具体的使用文档,假设使用中有 ...
- MySQL基础笔记(一) SQL简介+数据类型
MySQL是一个关系型数据库管理系统(RDBMS),它是当前最流行的 RDBMS 之一.MySQL分为社区版和企业版,由于其体积小.速度快.总体拥有成本低,尤其是开放源码这一特点,一般中小型网站的开发 ...