c vs c++ in strcut and class
总习惯用c的用法,现在学习C++,老爱拿来比较。声明我用的是g++4.2.1 SUSE Linux。看例子吧
- #include <iostream>
- #include <cstring>
- #include <string>
- using namespace std;
- enum zoo_obj_kind{
- null = 0,
- #define null null
- no = 0,
- #define no no
- animal = 2,
- #define animal animal
- plant = 4,
- #define plant plant
- others = 8
- #define others others
- };
- struct zoo_obj{
- zoo_obj_kind zo_kind;
- char name [40];
- };
- class zoo_obj_1{
- zoo_obj_kind zo_kind;
- char name [40];
- };
- int main(void){
- cout << "struct :" << sizeof(struct zoo_obj) << endl;
- cout << "clsas :" << sizeof( zoo_obj_1) << endl;
- }
- struct size:44
- clsas size:44
- #include <iostream>
- #include <cstring>
- #include <string>
- using namespace std;
- enum zoo_obj_kind{
- null = 0,
- #define null null
- no = 0,
- #define no no
- animal = 2,
- #define animal animal
- plant = 4,
- #define plant plant
- others = 8
- #define others others
- };
- struct zoo_obj{
- zoo_obj_kind zo_kind;
- char name [40];
- void (*say)(struct zoo_obj *);
- };
- void say(struct zoo_obj *obj){
- if(!obj) {
- printf("null\n");
- return ;
- }
- printf("name:%s\n",obj->name);
- }
- class zoo_obj_1{
- zoo_obj_kind zo_kind;
- char name [40];
- void say(zoo_obj_1 &obj){
- cout << "name:" << name << endl;
- }
- };
- int main(void){
- cout << "struct :" << sizeof(struct zoo_obj) << endl;
- cout << "clsas :" << sizeof( zoo_obj_1) << endl;
- }
- struct size:48
- clsas size:44
- #include <iostream>
- #include <cstring>
- #include <string>
- using namespace std;
- enum zoo_obj_kind{
- null = 0,
- #define null null
- no = 0,
- #define no no
- animal = 2,
- #define animal animal
- plant = 4,
- #define plant plant
- others = 8
- #define others others
- };
- struct zoo_obj{
- zoo_obj_kind zo_kind;
- char name [40];
- void (*say)(struct zoo_obj &);
- };
- void say(struct zoo_obj &obj){
- printf("name:%s\n",obj.name);
- }
- class zoo_obj_1{
- public:
- zoo_obj_kind zo_kind;
- char name [40];
- void say(){cout << "name:" << name << endl;}
- void say(zoo_obj_1 &obj){cout << "name:" << obj.name << endl;}
- };
- typedef struct zoo_obj s_zoo_obj;
- typedef zoo_obj_1 c_zoo_obj;
- int main(void){
- s_zoo_obj s_obj = {animal,"dog",say};
- zoo_obj_1 c_obj = {animal,"cat"};
- cout << "struct size:" << sizeof(struct zoo_obj) << endl;
- cout << "clsas size:" << sizeof( zoo_obj_1) << endl;
- s_obj.say(s_obj);
- c_obj.say(c_obj);
- }
- struct size:48
- clsas size:44
- name:dog
- name:cat
- #include <iostream>
- #include <cstring>
- #include <string>
- using namespace std;
- enum zoo_obj_kind{
- null = 0,
- #define null null
- no = 0,
- #define no no
- animal = 2,
- #define animal animal
- plant = 4,
- #define plant plant
- others = 8
- #define others others
- };
- struct zoo_obj{
- zoo_obj_kind zo_kind;
- char name [40];
- void (*say)(struct zoo_obj *);
- };
- void say(struct zoo_obj *obj){
- !obj
- ? printf("null\n")
- : printf("name:%s\n",obj->name);
- }
- class zoo_obj_1{
- public:
- zoo_obj_kind zo_kind;
- char name [40];
- void say(){cout << "name:" << name << endl;}
- void say(zoo_obj_1 *obj){
- !obj
- ? cout << "null\n"
- : cout << "name:" << obj->name << endl;
- }
- };
- typedef struct zoo_obj s_zoo_obj;
- typedef zoo_obj_1 c_zoo_obj;
- int main(void){
- s_zoo_obj s_obj = {animal,"dog",say};
- zoo_obj_1 c_obj = {animal,"cat"};
- cout << "struct size:" << sizeof(struct zoo_obj) << endl;
- cout << "clsas size:" << sizeof( zoo_obj_1) << endl;
- s_obj.say(&s_obj);
- c_obj.say(&c_obj);
- s_obj.say(NULL);
- c_obj.say(NULL);
- }
- struct size:48
- clsas size:44
- name:dog
- name:cat
c vs c++ in strcut and class的更多相关文章
- golang基础--strcut结构体
结构体struct类似python语言中的类class,结构体类的元素可以是一个变量,或者函数或者其它的类型,好比python的属性和方法. // struct结构体,类似python语言中的clas ...
- go strcut 封装
package model import "fmt" type person struct { Name string age int //其它包不能直接访问.. sal floa ...
- ASP.NET MVC5+EF6+EasyUI 后台管理系统(41)-组织架构
系列目录 本节开始我们要实现工作流,此工作流可以和之前的所有章节脱离关系,也可以紧密合并. 我们当初设计的项目解决方案就是可伸缩可以拆离,可共享的项目解决方案.所以我们同时要添加App.Flow文件夹 ...
- Linux C++中的时间函数(转)
http://net.pku.edu.cn/~yhf/linux_c/function/03.html asctime(将时间和日期以字符串格式表示) 相关函数 time,ctime,gmtime ...
- 33个超级有用必须要收藏的PHP代码样例
作为一个正常的程序员,会好几种语言是十分正常的,相信大部分程序员也都会编写几句PHP程序,如果是WEB程序员,PHP一定是必备的,即使你没用开发过大型软件项目,也一定多少了解它的语法. 在PHP的流行 ...
- [汇编与C语言关系]3. 变量的存储布局
以下面C程序为例: #include <stdio.h> ; ; ; int c; int main(void) { ; char b[] = "Hello World" ...
- C库函数使用与总结之时间函数
1. localtime(取得当地目前时间和日期) [头文件]#include <time.h> [函数原型]struct tm *localtime(const time_t * tim ...
- 一个asp采集程序
<% if request.QueryString="" then url="http://www.hbcz.gov.cn:7001/XZQHQueryWAR/xx ...
- 接触Matlab5年一个总结(Matlab要掌握的一些要点 )
阅读目录 前言 Matlab的开发环境与简单介绍 Matlab的常见命令 Matlab的灵魂-矩阵操作 Matlab的.m或.fig的编程与技巧 从大二开始接触到matlab,讲真,这是一个我觉得很良 ...
随机推荐
- sprintf,你知道多少?
选自<CSDN 社区电子杂志——C/C++杂志>http://emag.csdn.net 2005 年1 月 总第1 期 - 93 -本文作者:steedhorse(晨星)printf 可 ...
- android 4.0后不允许屏蔽掉home键
屏蔽home键的方法 // 屏蔽掉Home键 public void onAttachedToWindow() { this.getWindow().setType(WindowManager.Lay ...
- 大数据时代的 9 大Key-Value存储数据库
在过去的十年中,计算世界已经改变.现在不仅在大公司,甚至一些小公司也积累了TB量级的数据.各种规模的组织开始有了处理大数据的需求,而目前关系型数据库在可缩放方面几乎已经达到极限. 一个解决方案是使用键 ...
- Java__jar包的简单操作
工作需要研究了下jar,这里就举个简单的例子,以helloworld为例吧 1.启动eclipse编写代码: 次步不做赘述 package cn.test.jar; public class Hell ...
- 基于visual Studio2013解决面试题之1408桶排序
题目
- Caused by: android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity
Caused by: android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity ...
- HTML文档类型DTD与浏览器怪异模式
虽然在兼容IE6时候经常会注意到两个模式的区别,但是系统的理解起来,还没有认真总结过.看了一些网上的资料.结合自己的理解汇总了一下,放在这里备忘并分享给大家. 浏览器从服务端获取网页后会根据文档的DO ...
- hdu4712 Hamming Distance
Hamming Distance Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others) To ...
- C++汉字转拼音(转)
#include<iostream> #include<string> using namespace std; string findLetter(int nCode); s ...
- 关于new enhancement的一些知识
关于new enhancement sap源程序里也给我们留了很多. 以下例句point .section.spot说明这些知识点. 1.不管是point还是section 都是基于spot的,spo ...