利用C语言理解面向对象的本质
来源:互联网 发布:mac游戏不能全屏 编辑:程序博客网 时间:2024/06/05 20:46
/*时间:2014年11月4日09:33:15C++成员函数的原理:1:当一些函数与某一个结构体(类)的成员变量运算关系非常密切,C++把这样的一些函数划分为这个类的内部,叫做成员函数;2:成员函数的内部必须有个本结构体(类)的指针变量(this),这个指针变量指向哪个结构体(类)的对象,这个函数将操作哪个结构体内的成员。3:最终达到的效果就是一个类不但有成员变量(属性)而且还有一些相关的行为(功能),这个效果就是面向对象的开发;*/#include <stdio.h>const float PI = 3.141592653f;struct Circle{float radius;//半径};float Circumference(struct Circle* pthis)//圆周 周长{return 2 * PI * pthis->radius;}float Area(struct Circle* pthis)//圆的面积{return PI * pthis->radius * pthis->radius;}void Input(struct Circle *pthis){printf("请输入游泳池的半径:");scanf("%f",&pthis->radius);}struct Rectangle{float fWidth;//宽float fHeight;//高};float Rectangle_sum(struct Rectangle* pthis){return 2*pthis->fHeight + 2*pthis->fWidth; //矩形的周长}float Rectangle_area(struct Rectangle* pthis){return pthis->fHeight * pthis->fWidth;}void Rectangle_Input(struct Rectangle* pthis){printf("请输入矩形的高与宽:\n");scanf("%f %f", &pthis->fHeight,&pthis->fWidth);}void main(){struct Circle Pool;struct Circle PoolRim;struct Rectangle rect;struct Rectangle rect2;Input(&Pool);Input(&PoolRim);//PoolRim.radius += 3;printf("栅栏长度是:%0.2f\n", Circumference(&PoolRim));printf("内部通道的面积是:%0.2f\n",Area(&PoolRim) - Area(&Pool));Rectangle_Input(&rect);Rectangle_Input(&rect2);printf("栅栏长度是:%0.2f\n",Rectangle_sum(&rect));printf("内部通道的面积是:%0.2f\n", (Rectangle_area(&rect2) - Rectangle_area(&rect)));}
代码中的 函数都带有一个 struct 类型的参数指针;而我们在C++当中,会把这个函数放到了类的内部,但是它会有一个this指针;
0 0
- 利用C语言理解面向对象的本质
- 面向对象的本质
- 面向对象的本质
- C语言的面向对象
- 面向对象的c语言
- 面向对象的本质是什么?
- 面向对象的本质是什么?
- 面向对象的本质是什么?
- 什么是面向对象的本质?
- 小议面向对象的本质
- Java面向对象的本质
- 面向对象语言的理解---小笔记
- C语言中利用宏进行面向对象(OOP)的操作
- luajit利用ffi结合C语言实现面向对象的封装库
- C语言面向对象
- C语言面向对象
- C语言中面向对象的尝试
- C语言面向对象的实现
- Windows使用VNC viewer访问Ubuntu 14.04远程桌面的简单方法
- 未来一年想去的地方
- 为什么通常牛顿法比梯度下降法能更快的收敛
- IntelliJ IDEA13 如何创建TestModule(用于robotium自动测试)
- ios开发之深入浅出 (6) — ARC之对象转型
- 利用C语言理解面向对象的本质
- eclipse c++ 引用其它工程类 undefined reference to....
- F3不能用位带操作GPIO之原由
- 在python中排序元组
- 分布式数据库数据从属与客户端与服务器的数据同步
- npm spm bower三个包管理器,哪个比较好用
- JAVA card 应用开发(二) 在项目增加APPLET
- shell 判断传入的变量是否为空 if [ X$1 = X ]
- Hive 参数