网站大量收购独家精品文档,联系QQ:2885784924

[工学]面向对象程序设计语言C++第03章_类与对象.ppt

[工学]面向对象程序设计语言C++第03章_类与对象.ppt

  1. 1、本文档共37页,可阅读全部内容。
  2. 2、原创力文档(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。
  3. 3、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载
  4. 4、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
查看更多
[工学]面向对象程序设计语言C第03章_类与对象

class Circle { private: int x, y; //圆心的X、Y坐标 float fRadius; //半径 public: void SetXY(int a,int b); void SetRadius(float r); void Move(int newx,int newy); … //其它成员 }; 类的使用 void main() { Date today;yourbirth,oneday; today. InitDate(24,9,2008); yourbirth. InitDate(12,7,1990); Date * poneday; poneday=oneday; poneday-InitDate(26,9,2008); … } 将整数作为对象 class INT {int numb; public: void Set(int n){numb=n;} void Get(){return numb);} …}; void main() { INT oneINT;twoINT; oneINT.Set(1); twoINT.Set(10); coutoneINT.Get( ) twoINT.Get( ); } 问题:成员函数如何“知道”对哪个对象的数据进行操作? this class INT {int numb; public: void Set(int n ,INT * const this){this-numb=n;} void Get(INT * const this) {return this-numb);} …}; void main() { INT oneINT;twoINT; oneINT.Set(1,oneINT); twoINT.Set(10,twoINT); coutoneINT.Get(oneINT) twoINT.Get(twoINT); } 例仓库管理中,假定只关心货物的重量,可以使用static数据表示总重量。 #include iostream using namespace std; class Goods { private: float fWeight; static float fTotalWeight; public: void AddGoods(float weight) { fWeight = weight; fTotalWeight += weight; } void RemoveGoods() { fTotalWeight -= fWeight; } void ShowWeight() { cout fWeight endl; } void ShowTotalWeight() { cout fTotalWeight endl; //直接访问static数据 } }; float Goods::fTotalWeight = 0.0; //static 成员的定义及初始化 void main() { Goods goods1, goods2, goods3; goods1.AddGoods(100); goods2.AddGoods(70); goods3.AddGoods(150); goods2.ShowWeight(); goods1.ShowTotalWeight(); } 输出结果为: 70 320 第三章 类和对象 3.2 类的成员 3.2.3 静态成员 静态数据成员 在一个类中,若将一个数据说明前加上static,则该数据称为静态数据。 静态数据成员被该类的所有对象共享。无论建立多少个该类的对象,都只有一个静态数据的存储空间。 * 第三章 类和对象 3.2 类的成员 类aClass包括一个普通数据成员anInt和一个静态数据成员anSint。 假设定义了三个对象A、B和C。 * Object A int anint; Object B int anint; Object C int anint; static int sint; 第三章 类和对象 3.2 类的成员 静态数据成员的存储空间必须在类定义外进行分配,具体的语法如下: 类型名 类

文档评论(0)

hhuiws1482 + 关注
实名认证
内容提供者

该用户很懒,什么也没介绍

版权声明书
用户编号:5024214302000003

1亿VIP精品文档

相关文档