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的暧昧的更多相关文章

  1. C语言struct小知识

    1.C语言里的struct是不能包含成员函数的,只能有数据成员2.C语言struct定义变量只能用一下两种方式:struct { ... } x, y, z;struct point pt;直接poi ...

  2. C语言struct类型

    在实际问题中,一组数据往往具有不同的数据类型.例如, 在学生登记表中,姓名应为字符型:学号可为整型或字符型: 年龄应为整型:性别应为字符型:成绩可为整型或实型. 显然不能用一个数组来存放这一组数据. ...

  3. C语言 Struct 结构体在 Java 中的体现

    大一整个学期完成了 C 语言的学习,大二就进入了Java 的学习. 和C语言一样,我们都会尝试写一个小小的学生管理系统什么的,学习过 C 语言同学知道,在管理系统中 Struct 结构体是个很好用的东 ...

  4. c语言struct和c++struct的区别

    1.定义 c语言中struct是用户自定义数据类型(UDT),是一些变量的集合体:c++中struct是抽象数据类型(ADT),能给用户提供接口,能定义成员函数,能继承,能实现多态 2.成员权限设置 ...

  5. 关于c语言struct和typedef

    C++中使用: struct test{    int x, y;};就可以定义一个名为 test的结构体,但C中很可能编译通不过.C语言并不支持在struct后使用标示符定义结构体的名字,test将 ...

  6. CC+语言 struct 深层探索——CC + language struct deep exploration

    1        struct 的巨大作用 面对一个人的大型C/C++程序时,只看其对struct 的使用情况我们就可以对其编写者的编程经验进行评估.因为一个大型的C/C++程序,势必要涉及一些(甚至 ...

  7. C 语言Struct 实现运行类型识别 RTTI

    通过RTTI,能够通过基类的指针或引用来检索其所指对象的实际类型.c++通过下面两个操作符提供RTTI. (1)typeid:返回指针或引用所指对象的实际类型.    (2)dynamic_cast: ...

  8. c语言 struct 的初始化

    转自:http://www.cnblogs.com/silentjesse/archive/2013/07/30/3225212.html struct数据有3中初始化方法:顺序,C风格及C++风格的 ...

  9. 内存对齐-C语言struct内存占用问题

    转1个写的比较全面的. http://hubingforever.blog.163.com/blog/static/17104057920122256134681/ 本文编辑整理自:http://hi ...

随机推荐

  1. hdu 4046 Panda [线段树]

    Panda Time Limit: 10000/4000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Subm ...

  2. SQL SERVER 2012 第四章 连接 JOIN の INNER JOIN

    所有JOIN语句的共同点是:将一个记录与另外一个或多个记录匹配,从而生成一个新记录,这个记录是由两个记录的合并列所产生的一个超集. 内部连接: 内部连接语法结构:SELECT <select l ...

  3. Mysql数据库的事物

    一 .事物的特性:ACID 数据库的事务必须具备ACID特性,ACID是指 Atomicity(原子性).Consistensy(一致性).Isolation(隔离型)和Durability(持久性) ...

  4. Linux下使用Curl调用Java的WebService接口

    其实只要是标准的WSDL的SOA接口WebService都可以用. 调用方式: 注意:上面的方式不包括加密或者登录的,其实SOA有一套完整的加密方式. curl -H'Content-Type: te ...

  5. chapter1:using neural nets to recognize handwritten digits

    two important types of artificial neuron :the perceptron and the sigmoid neuron Perceptrons 感知机的输入个数 ...

  6. 【scrapy】Item及Spider

    Items Item objects are simple containers used to collect the scraped data.They provide a dictionary- ...

  7. topcoder srm 551

    div1 250pt 题意:一个长度最多50的字符串,每次操作可以交换相邻的两个字符,问,经过最多MaxSwaps次交换之后,最多能让多少个相同的字符连起来 解法:对于每种字符,枚举一个“集结点”,让 ...

  8. C++设计模式之适配器模式(二)

    3.Socket网络通信的设计与实现------类适配器 除了对象适配器模式之外.适配器模式另一种形式.那就是类适配器模式,类适配器模式和对象适配器模式最大的差别在于适配器和适配者之间的关系不同,对象 ...

  9. Edmonds 开花算法

    Edmonds 开花算法 input: 图G,匹配M,未饱和点u idea: 查找从 u 開始的 M-交错路径.对每一个顶点记录父亲节点. 发现花朵.则收缩. 维护 S 和 T.S 表示沿着已经饱和的 ...

  10. rand和srand的用法(转载)

    首先我们要对rand&srand有个总体的看法:srand初始化随机种子,rand产生随机数,下面将详细说明. rand(产生随机数)表头文件: #include<stdlib.h> ...