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

第7章 对象和类;7.1 过程性编程和面向对象编程;例如,用计算机统计一个篮球队中各球员的技术数据 面向过程的程序员: 要输入球员的姓名,投篮次数,命中率等重要数据,其中一些数据如命中率由计算机来计算 程序能够显示这些数据 让main函数调用一个函数来实现输入,再调用一个函数来实现计算,调用第三个函数显示结果。下一场比赛呢?可以再用一个函数更新统计数据。Main函数中需要放一个菜单,以让用户选择输入,计算,显示结果或更新数据 表示数据:用数组或结构 总之,首先考虑要遵循的步骤,然后考虑如何表示数据;面向对象的程序员如下考虑: 我要跟踪的是球员,要有一个对象表示选手的姓名和各种统计数据 需要一些方法:首先是把基本信息加入到这些单元中去----初始化;需要一些计算方法,如命中率,程序应自动执行这些方法,无需用户干预;更新和显示信息的方法 所以,用户和数据交互的方式有三种:初始化,更新和报告----用户接口 总之,使用面向对象的方法时首先从用户的角度考虑对象----描述对象所需的数据以及描述用户与数据交互所需的操作。;提倡多文件的程序结构;头文件中常包含的内容: 函数原型 符号常量(用#define或const定义) 结构声明 类生明 模板声明 内联函数(普通函数定义不可) OOP通常的做法是一个头文件中放置类定义,另一个文件(源文件)放置类中方法的定义 一个坐标转换的例子:;// coordin.h -- structure templates and function prototypes // structure templates #ifndef COORDIN_H_ #define COORDIN_H_ struct polar { double distance; // distance from origin double angle; // direction from origin }; struct rect { double x; // horizontal distance from origin double y; // vertical distance from origin }; // prototypes polar rect_to_polar(rect xypos); void show_polar(polar dapos); #endif;// file1.cpp -- example of a three-file program #include iostream #include coordin.h // structure templates, function prototypes using namespace std; int main() { rect rplace; polar pplace; cout Enter the x and y values: ; while (cin rplace.x rplace.y) // slick use of cin { pplace = rect_to_polar(rplace); show_polar(pplace); cout Next two numbers (q to quit): ; } cout Bye!\n; return 0; };// file2.cpp -- contains functions called in file1.cpp #include iostream #include cmath #include coordin.h // structure templates, function prototypes // convert rectangular to polar coordinates polar rect_to_polar(rect xypos){ using namespace std; polar answer; answer.distance = sqrt( xypos.x * xypos.x + xypos.y * xypos.y); answer.angle = std::atan2(xypos.y, xypos.x); return answer; // returns a polar structure } // show polar coordinates, converting angle to degrees

文档评论(0)

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

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

1亿VIP精品文档

相关文档