- 1、本文档共68页,可阅读全部内容。
- 2、有哪些信誉好的足球投注网站(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。
- 3、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载。
- 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
您可能关注的文档
- 7.3统计.ppt
- 7.3_“东方明珠”——香港和澳门.ppt
- 7.5晨会.ppt
- 7.4 任意项级数.ppt
- 大连理工大学入学准备生活实用讲解.ppt
- 天一联考语文答案讲解.ppt
- 天商市场营销题库及答案讲解.doc
- 大道之行也课件讲解.ppt
- 天山雪耳生长史讲解.ppt
- 天娱传媒与超级女声的案例讲解.pptx
- 中国统计年鉴 051001.pdf
- 中国统计年鉴 4-10.各种价格指数.pdf
- 开发者培训认证体系20150707.pdf
- 纸浆和造纸碳排放量计算工具(英文)Pulp and Paper tool-version-1_4_0.xls
- 导线产品标准JASO D609 -2012 (Ampacity measurement spec).pdf
- Schlemmer_Catalog建筑工程图集.pdf
- 导线产品标准LV 214 (Connector Test Specification).pdf
- 中国统计年鉴 B0102e.pdf
- 保洁试题库含答案.docx
- 口腔正畸学模拟练习题(附答案).docx
文档评论(0)