(C++)面向对象程序设计Object-OrientedProgramminginC++课件_第1页
(C++)面向对象程序设计Object-OrientedProgramminginC++课件_第2页
(C++)面向对象程序设计Object-OrientedProgramminginC++课件_第3页
(C++)面向对象程序设计Object-OrientedProgramminginC++课件_第4页
(C++)面向对象程序设计Object-OrientedProgramminginC++课件_第5页
已阅读5页,还剩22页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、(C+)面向对象程序设计Object-Oriented Programming in C+Chapter 3 Keyboard Input and Screen Output(C+)面向对象程序设计Object-Oriented 引入:类和对象C+程序中怎样描述“李美”同学?引入:类和对象C+程序中怎样描述“李美”同学?引入:类和对象抽象共同特征:求平均成绩显示三科成绩及平均值学号姓名高数成绩英语成绩C+成绩属性行为引入:类和对象抽象共同特征:求平均成绩学号属性行为4属性行为数据函数object(方法)&|现实世界C+4属性行为数据函数object(方法)&|现实世界C+引入:类和对象抽象共同

2、特征:求平均成绩显示三科成绩及平均值学号姓名高数成绩英语成绩C+成绩属性行为class studentint id;char name20;float score3;float ave;float score_ave();void show();引入:类和对象抽象共同特征:求平均成绩学号属性行为classclass studentprivate:int id;char name20;float score3;float ave;public:student(int n,char *na,float s1,float s2,float s3);float score_ave();void sho

3、w();student:student(int n,char *na,float s1,float s2,float s3) id=n; strcpy(name,na); score0=s1, score1=s2, score2=s3;float student:score_ave() /求平均值 ave=(score0+score1+score2)/3; return ave;void student:show() coutNo: idtName: nameendl; cout高数: score0t英语: score1tC+: score2endl; cout平均成绩为ave=aveendl

4、;class studentvoid main() student st(001,“李美,100,100,100); st.score_ave(); st.show();(C+)面向对象程序设计Object-OrientedProgramminginC+课件引入:类和对象class studentint id;char name20;float score3;float ave;float score_ave();void show();学号姓名高数英语C+平均学号姓名高数英语C+平均001李美100100100100填充引入:类和对象class student学号姓名高数英语C+引入:类和对

5、象class studentint id;char name20;float score3;float ave;float score_ave();void show();student lm(001,“李美”,100,100,100,100)实例化主程序main() student lm(001,“李美,100,100,100); lm.score_ave(); lm.show();引入:类和对象class studentstudent l10 补充 计算机体系结构 10 补充 计算机体系结构 11113.1 Simple Keyboard InputData StreamData stre

6、am objects are used to perform basic input and output of data to and from various devices such as the keyboard and the screen.A stream is a data communication object connected to an input or output device.cout: standard output stream associated with SCREEN: The extraction operator is used to read da

7、ta from keyboad.3.1 Simple Keyboard InputData 3.1 Simple Keyboard InputExample:eads a number from the keyboard and stores it in the variable num 3.1 Simple Keyboard InputExamp流(stream)流(stream)表示信息从源到目的端的流动,负责建立数据生产者和消费者之间的联系, 数据按顺序从一个对象传送到另一对象。C+的输入输出流是指由若干字节组成的字节序列,这 些字节 中的数据按顺序从一个对象传送到另一对象。流流(str

8、eam)流(stream)表示信息从源到目的端的流 输出流Output stream:数据从内存传送到某个载体或设备中; 输入流 Input stream:数据从某个载体或设备传送到内存中;-内存-coutcin概念理解:输入输出(IO)流 输出流Output stream:数据从内存传送到某个载-内存-coutcin概念理解:输入输出(IO)流在C+中,输入输出流被定义为类。 C+ I/O 库中的类称为流类(stream class)。cin 和cout是iostream流类的全局对象,在 中进行了定义。所以,它们在main()开始之前已经初始化。-coutcin概念理解:输入输出(I7.2

9、.2 标准输入流 cinistream类的对象,它从标准输入设备(键盘)获取数据.程序中的变量通过流提取符“”从流中提取数据,“”实际上是istream类的一个成员函数,所以cin coutostream类的对象,它向标准输出设备(屏幕)输出数据数据通过流插入符“”插入到输出流中, “”实际上是ostream类的一个成员函数,所以cout 7.2.2 标准输入流 cin cout3.2 Manipulators(流操纵符)Manipulators are used to modify input and output data streams.endl, setw, setfill, fixe

10、d,setprecisionendl:skip to the start of a new line on the screensetw :set the width of a data field which is the number of columns that the data item occupies on the screen;setfill :change the “padding” character from a space to any other character.setw :设置数据域的宽度,即数据项在屏幕上所占的列数。只对下一个数据项起作用。所属头文件:ioma

11、nipcout endl endl endl can be used anywhere endlsetfill : 用于把占位符从空格改变为其他字符。对所有后继项起作用。所属头文件:iomanip3.2 Manipulators(流操纵符)Manipula3.2 ManipulatorsExample: How to use manipulators #include Line 4 is required for any manipulator, like setw, that has a value in parentheses.If the field width is set too s

12、mall to display a value, the width is automatically expanded so that all the digits in the value are displayed3.2 ManipulatorsExample: How t3.2 ManipulatorsExample: How to use setfill and setwsetfill change the “padding” character from a space to “*”setw applies only to the next data item in the out

13、put streamsetfill remains in effect for all subsequent data items sent to the output stream.3.2 ManipulatorsExample: How t3.2 Manipulators(流操纵符)setprecision :指定要显示的数据的位数。对所有后继项起作用。所属头文件:iomanipfixed放置在 setprecision之前: 指定小数点后的显示位数。对所有后继项起作用。所属头文件:iomanip3.2 Manipulators(流操纵符)fixed放置在3.2 Manipulatorss

14、etprecision: specify the number of digits of a number to display3.2 Manipulatorssetprecision: 3.3 Single-character input and outputCharacter Input and OutputWhitespace Characters(空白字符):generate an invisible blank or white space on the screen, such as Tab、Enter and the space bar.Inputnoskipws:no matt

15、er what it is read from keyboardAlternatively, the function get() associated with the input stream object cin can be usedOutputcout and its;the output stream object cout has a member function put() that can be used to display a character;noskips :使用它能够读入空白字符。所属头文件:iomanip或者用:cin.get()显示字符:cout.put()

16、3.3 Single-character input and3.3 Single-character input and outputExample:How to achieve single-char input&outputA function is a block of program code that carries out a specific task, which will be discussed in future.Read a single character from keyboard (Including whitespace characters)3.3 Singl

17、e-character input andProgramming pitfalls1. Do not mix up the insertion operator . The insertion operator is used to insert data into the output stream; the extraction operator is used to read data from the input stream.2. Some manipulators apply only to the next data field (e.g. setw); others (e.g. setprecision) stay in effect for all subsequent d

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论