11.数据共享和成员特性重点.ppt

  1. 1、本文档共44页,可阅读全部内容。
  2. 2、原创力文档(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。
  3. 3、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载
  4. 4、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
查看更多
11.4.2 this指针的实质 this指针的指向 当类实例化时,this指针指向对象自己;而在类的声明时,指向类本身。 当成员函数的形参名与该类的成员变量名同名时,必须用this指针来显式区分。 例 : class CPoint { public: CPoint( int x = 0, int y = 0 ) { this- x = x; this-y = y; } void Offset ( int x = 0, int y = 0 ) { (*this). x = x; (*this).y = y; } void Print() const { coutPoint(xPos, yPos) endl; } private: int x, y; }; 11.4.2 this指针的实质 this指针的指向 当类实例化时,this指针指向对象自己;而在类的声明时,指向类本身。 当成员函数的形参名与该类的成员变量名同名时,必须用this指针来显式区分。 在静态成员函数中是不能使用this指针的。 this指针的类型 在普通成员函数中, 在类常成员函数中, 类名 * const this = 类名; const类名 * const this = 类名; 第11章 数据共享和成员特性 11.1 静态成员 11.2 友元 11.3 成员其他特性 11.4 this指针 11.5 综合应用实例 11.5 综合应用实例 1.??CPerson和CScore类的设计 定义的CPerson和CScore类代码如下: class CPerson { friend class CScore; // 定义友元类 private: char strName[12]; // 姓名 char strStuNO[9]; // 学号 } class CScore { public: CScore( ){} // 显式默认构造函数 CScore( CScore one ) // 显式默认拷贝构造函数 { strcpy(stu.strName, one.stu.strName ); strcpy(stu.strStuNO, one.stu.strStuNO ); fScore[0] = one.fScore[0]; fScore[1] = one.fScore[1]; fScore[2] = one.fScore[2]; } void SetData( char *strname, char *strno, float s1, float s2, float s3) { strcpy(stu.strName, strname ); strcpy(stu.strStuNO, strno ); fScore[0] = s1; fScore[1] = s2; fScore[2] = s3; } void Output(void) { cout姓名:stu.strNameendl; cout学号:stu.strStuNOendl; cout三门课成绩:fScore[0], fScore[1], fScore[2]endl; } void Input(void) { cout姓名:; cinstu.strName; cout学号:; cinstu.strStuNO; cout三门成绩:; cinfScore[0]fScore[1]fScore[2]; } private: CPerson stu; float fScore[3]; // 三门课程成绩 }; 2.??CStack类的设计 CStack类的声明代码如下: class CStack { public: static void Create(int nSize = 10); // 创建函数 static void Release(); // 释放函数 public: // 操作 static void Push(CScore a); // 压入 static CScore Pop(void); // 弹出 private: static CScore *sp; static CScore *buffer; static int m_nSize; }; [例Ex_Stack11] 设计栈类静态成员来存取学生成绩数据。 #include iostream #include cstdlib #include cstring using na

文档评论(0)

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

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

1亿VIP精品文档

相关文档