C++内部结构--经典例子
来源:互联网 发布:银行卡芯片 算法rsa 编辑:程序博客网 时间:2024/06/05 14:13
例子1:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct object {
char name[16];
};
struct person {
char name[16];
void sleep() { printf("%s is person, he is sleeping/n", this->name); }
void study() { printf("%s is person, he is studying/n", this->name); }
};
struct dog {
char name[16];
void sleep() { printf("%s is dog, he is sleeping/n", this->name); }
void bark() { printf("%s is dog, he is barking/n", this->name); }
};
#define bless(object, type) ((type*) object)
int main()
{
struct object * o = (struct object *) malloc(sizeof(struct object));
strcpy(o->name, "tom");
// 先把"tom"变为人
bless(o, person)->sleep();
bless(o, person)->study();
// 再把"tom"变为狗
bless(o, dog)->sleep();
bless(o, dog)->bark();
// 最后,再把"tom"变回人
bless(o, person)->sleep();
bless(o, person)->study();
return 0;
}
// 程序运行时输出:
// tom is person, he is sleeping
// tom is person, he is studying
// tom is dog, he is sleeping
// tom is dog, he is barking
// tom is person, he is sleeping
// tom is person, he is studying
---------------------------------------------------------------------------------------------------------
例子2:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
class object {
public:
char ccc[20];
char bbb[20];
};
class person {
private:
char name[16];
public:
void sleep() { printf("%s is person, he is sleeping/n", this->name); }
void study() { printf("%s is person, he is studying/n", this->name); }
};
class dog {
private:
char name[16];
public:
void sleep() { printf("%s is dog, he is sleeping/n", this->name); }
void bark() { printf("%s is dog, he is barking/n", this->name); }
};
#define bless(object, type) ((type*) object)
int main()
{
object * o = (object *)malloc(sizeof(object));
strcpy(o->ccc, "tom");
// 先把"tom"变为人
bless(o, person)->sleep();
bless(o, person)->study();
// 再把"tom"变为狗
bless(o, dog)->sleep();
bless(o, dog)->bark();
// 最后,再把"tom"变回人
bless(o, person)->sleep();
bless(o, person)->study();
return 0;
}
// 程序运行时输出:
// tom is person, he is sleeping
// tom is person, he is studying
// tom is dog, he is sleeping
// tom is dog, he is barking
// tom is person, he is sleeping
// tom is person, he is studying
- C++内部结构--经典例子
- c经典例子______01
- C++----------------------------c++继承经典例子
- [c++]继承的经典例子
- C语言 经典小例子
- C语言操作mysql经典例子
- C/C++连接MySQL数据库“经典”例子!
- c语言-一些学习c的经典例子
- 一道经典的Java面试题,抛析内部结构
- C语言中,计算程序执行时间的经典例子
- C语言argc和argv经典小例子如何运行
- c语言_链表实例讲解(两个经典例子)
- 经典Robocode例子代码
- ASP经典代码例子
- asp经典例子
- ASP编程经典例子
- overriding经典例子
- Spring入门经典例子
- android 编译命令
- SVN 的标准目录结构:trunk, branches 和 tags
- 请大家试用我开发的动态域名解析客户端软件
- Android之view重绘
- 使用cmd中的tree命令生成目录树
- C++内部结构--经典例子
- 开发板常用设置命令
- 今天系统展示
- TAO使用指南 -- IDL编译器
- 如何建立以点开头或以点结尾的文件夹
- 把ASP.Net页面导入Excel中,方便打印(页面内容用客户端Excel打开)
- Java垃圾回收简介
- android 知识积累
- 使用对象模型操作文档库-DocumentLibrary