C++教程05_抽象与分类.ppt

  1. 1、原创力文档(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。。
  2. 2、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载
  3. 3、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
查看更多
例16:设计学生类的属性 #include iostream using namespace std; ? class Student { private: string strName; //定义字符串,用于存放学生的姓名 unsigned long nIndex; //学生的索引值 int nScore; //学生的成绩 public: //公有成员 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.3 程序中类的设计——5.3.2 对象功能与行为的抽象 5.4.1对象的定义与初始化 类是一种抽象机制,一个类就是一个自定义类型。使用类需要定义类类型的实例,就是对象。类好像一个模具,而对象就是用这个模具铸造出的零件,我们使用的是一个个具体的零件而不是模具本身。 对象的定义与初始化 语法形式如下: 类名 对象名; 定义一个对象以后,可以通过“对象名.成员名”的方式访问它的公有成员。 对象初始化的方法需要通过构造函数定义。 * 5.4 类的对象 例5-17:设置并显示学生信息 #include iostream using namespace std; class Student { private: //私有有成员定义 string strName; //定义字符串,用于存放学生的姓名 unsigned long nIndex; //学生的索引值 int nScore; //学生的成绩 public: 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.1对象的定义与初始化 例5-17(续) void main() { Student student; student.SetName(“Michael”); student.SetIndex(0); student.SetScore(90); string strName = student.GetName(); cout ”The student\’ name is: ” strName.c_str()endl; cout ”The index of this student is: ”student.GetIndex()endl; cout ”The score of this student is: ”student.GetScore()endl; } * 5.4 类的对象——5.4.1对象的定义与初始化 运行结果: The student name is: Michael The index of this student is: 0 The score of this student is: 90 例5-17(续) 5.4 类的对象——5.4.1对象的定义与初始化 例5-18:对象的初始化 #include iostream using namespace std; class Student {

文档评论(0)

132****9295 + 关注
实名认证
内容提供者

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

1亿VIP精品文档

相关文档