资源描述
,Title Holder,27,单击此处编辑母版文本样式,第二级,第三级,第四级,第五级,*,C+,程序设计,主讲人:,苏小红教授,sxh,哈尔滨工业大学,计算机科学与技术学院,Title Holder,单击此处编辑母版文本样式,第二级,第三级,第四级,第五级,Title Holder,17,单击此处编辑母版文本样式,第二级,第三级,第四级,第五级,*,C+,程序设计,主讲人:,苏小红教授,sxh,哈尔滨工业大学,计算机科学与技术学院,Title Holder,单击此处编辑母版文本样式,第二级,第三级,第四级,第五级,(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+,引入:类和对象,抽象,共同特征:,求平均成绩,显示三科成绩及平均值,学号,姓名,高数成绩,英语成绩,C+,成绩,属性,行为,class student,int id;,char name20;,float score3;,float ave;,float score_ave();,void show();,;,引入:类和对象抽象共同特征:求平均成绩学号属性行为class,class student,private:,int id;,char name20;,float score3;,float ave;,public:,student(int n,char *na,float s1,float s2,float s3);,float score_ave();,void show();,;,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;,class student,void main(),student st(001,“,李美,100,100,100);,st.score_ave();,st.show();,(C+)面向对象程序设计Object-OrientedProgramminginC+课件,引入:类和对象,class student,int id;,char name20;,float score3;,float ave;,float score_ave();,void show();,;,学号,姓名,高数,英语,C+,平均,学号,姓名,高数,英语,C+,平均,001,李美,100,100,100,100,填充,引入:类和对象class student学号姓名高数英语C+,引入:类和对象,class student,int 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 l,10,补充,计算机体系结构,10 补充 计算机体系结构,11,11,3.1 Simple Keyboard Input,Data Stream,Data stream 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 data from keyboad.,3.1 Simple Keyboard InputData,3.1 Simple Keyboard Input,Example:eads a number from the keyboard and stores it in the variable num,3.1 Simple Keyboard InputExamp,流(,stream),流,(stream),表示信息从源到目的端的流动,负责建立数据生产者和消费者之间的联系, 数据按顺序从一个对象传送到另一对象。,C+,的输入输出流是指由若干字节组成的,字节序列,,这,些字节,中的数据按顺序从一个对象传送到另一对象。,流,流(stream)流(stream)表示信息从源到目的端的流,输出流,Output stream,:,数据从内存传送到某个载体或设备中;,输入流,Input stream,:,数据从某个载体或设备传送到内存中,;,-,-,内存,-,-,cout,cin,概念理解:输入输出,(IO),流,输出流Output stream:数据从内存传送到某个载,-,-,内存,-,-,cout,cin,概念理解:输入输出,(IO),流,在,C+,中,输入输出流被定义为类。,C+ I/O,库中的类称为流类(,stream class),。,cin,和,cout,是,iostream,流类的全局对象,在,中进行了定义。所以,它们在,main(),开始之前已经初始化。,-coutcin概念理解:输入输出(I,7.2.2,标准输入流,cin,istream,类的对象,它从标准输入设备(键盘)获取数据,.,程序中的变量通过流提取符“”从流中提取数据,“”实际上是,istream,类的一个成员函数,所以,cin,cout,ostream,类的对象,它向标准输出设备(屏幕)输出数据,数据通过流插入符“”插入到输出流中,“,”实际上是,ostream,类的一个成员函数,所以,cout,7.2.2 标准输入流 cin cout,3.2 Manipulators(,流操纵符,),Manipulators are used to modify input and output data streams,.,endl, setw, setfill, fixed,,,setprecision,endl,:skip to the start of a new line on the screen,setw,: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,:设置数据域的宽度,即数据项在屏幕上所占的列数。,只对下一个数据项起作用。,所属头文件:,iomanip,cout endl endl endl can be used anywhere endl,setfill,:,用于把占位符从空格改变为其他字符。,对所有后继项起作用。,所属头文件:,iomanip,3.2 Manipulators(流操纵符)Manipula,3.2 Manipulators,Example: 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 small to display a value, the width is,automatically expanded so that all the digits in the value are displayed,3.2 ManipulatorsExample: How t,3.2 Manipulators,Example: How to use setfill and setw,setfill,change the “padding” character from a space to,“*”,setw,applies only to the next data item in the output stream,setfill remains in effect for all subsequent data items sent to the output stream.,3.2 ManipulatorsExample: How t,3.2 Manipulators(,流操纵符,),setprecision,:指定要显示的数据的位数。,对所有后继项起作用。,所属头文件:,iomanip,fixed,放置在,setprecision,之前:,指定小数点后的显示位数。,对所有后继项起作用。,所属头文件:,iomanip,3.2 Manipulators(流操纵符)fixed放置在,3.2 Manipulators,setprecision: specify the number of digits of a number to display,3.2 Manipulatorssetprecision:,3.3,Single-character input and output,Character Input and Output,W,hitespace,C,haracters,(空白字符,),:generate,an invisible blank or white space on the screen, such as Tab,、,Enter,and the space bar.,Input,noskipws:,no matter what it is read from keyboard,Alternatively, the function get() associated with the input stream object cin can be used,Output,cout and its. 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 data fields.,3. The line,#include ,is required for a manipulator that has a value in parentheses, e.g. setw( 4 ). Other manipulators, like endl, do not require this line.,Programming pitfalls1. Do not,Quick syntax reference,Quick syntax reference,Q & A,Thank You!,Q & AThank You!,
展开阅读全文