C++教程 作者 郑莉 李宁 05_抽象与分类.pptVIP

C++教程 作者 郑莉 李宁 05_抽象与分类.ppt

  1. 1、原创力文档(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。。
  2. 2、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载
  3. 3、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
  4. 4、该文档为VIP文档,如果想要下载,成为VIP会员后,下载免费。
  5. 5、成为VIP后,下载本文档将扣除1次下载权益。下载后,不支持退款、换文档。如有疑问请联系我们
  6. 6、成为VIP后,您将拥有八大权益,权益包括:VIP文档下载权益、阅读免打扰、文档格式转换、高级专利检索、专属身份标志、高级客服、多端互通、版权登记。
  7. 7、VIP文档为合作方或网友上传,每下载1次, 网站将根据用户上传文档的质量评分、类型等,对文档贡献者给予高额补贴、流量扶持。如果你也想贡献VIP文档。上传文档
查看更多
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 5.6.2友元类 友元类的使用类似于友元函数,只是此时声明的是一个类,被声明为友元的类可以访问声明友元的类的所有私有数据成员。 * 5.6 友元 例5-29:友元类的定义和使用 #include iostream using namespace std; class B; class A{ private: double x; public: friend class B; A(double x) { this-x = x;} ~A() {} }; class B{ public: B() {} ~B() {} void print(const A a ) { cout a.x endl;} }; int main(){ A tempA(10.0); B tempB; tempB.print(tempA); return 0; } * 5.6 友元——5.6.2友元类 运行结果: 10 例5-29(续) 5.6 友元——5.6.2友元类 小结 本章重点介绍抽象与分类的思想。首先介绍了函数的定义是实现。其次介绍了类的概念以及如何定义类,包括成员数据和成员函数的概念。也介绍了对象的初始化与销毁过程,特别强调了构造函数和析构函数,以及特殊的构造函数拷贝构造函数。 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * #include iostream #include string using namespace std; class Student { private: //私有成员变量定义 string strName; //定义字符串,用于存放学生的姓名 unsigned long nIndex; //学生的索引值 int nScore; //学生的成绩 public: //公有成员函数定义 Student(string _name,unsigned long _index, int _score): strName(_name),nIndex(_index),nScore(_score) {} ~Student() {} string GetName() {return strName;} unsigned long GetIndex() {return nIndex;} int GetScore() {return nScore;} void SetName(string strNameNew) { strName = strNameNew;} void SetIndex(unsigned long nIndexNew) {nIndex = nIndexNew;} void SetScore (int nScoreNew) {nScore = nScoreNew;} }; * 5.4 类的对象——5.4.3对象数组 例5-21(续) void Query(bool flag, Student *sList){ int index; int num; char yOrN; bool isFound = false; cout请输入要查询的学生的学号:; cinindex; for(int i=0;i10;i++){ num=sList[i].GetIndex(); if(index == num){ cout 姓名: sList[i].GetName() endl; cout 学号: num endl; cout 成绩: sList[i].GetScore() endl; isFound = true; break; } } * 5.4 类的对象——5.4.3对象数组 例5-21(续) if(!isFound) cout 该学号信息不存在! endl; ? cout 是否继续查询? (y/n): ; cin yOrN; if(yOrN == y || yOrN == Y) flag = true; else flag = false; ? } ? voi

文档评论(0)

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

1亿VIP精品文档

相关文档