【文档说明】(C++)面向对象程序设计Object-OrientedProgramminginC++课件.ppt,共(27)页,1.499 MB,由小橙橙上传
转载请保留链接:https://www.ichengzhen.cn/view-44698.html
以下为本文档部分文字说明:
1(C++)面向对象程序设计Object-OrientedProgramminginC++Chapter3KeyboardInputandScreenOutput2引入:类和对象C++程序中怎样描述“李美”同学?3引入:类和对象抽象共同特征:求平均成绩显示三科成绩及平均值学号姓
名高数成绩英语成绩C++成绩属性行为44属性行为数据函数object(方法)&&||5引入:类和对象抽象共同特征:求平均成绩显示三科成绩及平均值学号姓名高数成绩英语成绩C++成绩属性行为classstudent{intid;charname[20]
;floatscore[3];floatave;floatscore_ave();voidshow();};6classstudent{private:intid;charname[20];floatscore[3
];floatave;public:student(intn,char*na,floats1,floats2,floats3);floatscore_ave();voidshow();};student::student(intn,char*na,floats1
,floats2,floats3){id=n;strcpy(name,na);score[0]=s1,score[1]=s2,score[2]=s3;}floatstudent::score_ave()//求平均值{ave=(score[0]+score[1]+sco
re[2])/3;returnave;}voidstudent::show(){cout<<"No:"<<id<<"\t"<<"Name:"<<name<<endl;cout<<"高数:"<<score[0]<
<"\t"<<"英语:"<<score[1]<<"\t"<<"C++:"<<score[2]<<endl;cout<<"平均成绩为"<<"ave="<<ave<<endl;}7voidmain(){studentst(001,“李美",100,100,
100);st.score_ave();st.show();}8引入:类和对象classstudent{intid;charname[20];floatscore[3];floatave;floatscore_ave();voidshow();};学号姓名高数
英语C++平均学号姓名高数英语C++平均001李美100100100100填充9引入:类和对象classstudent{intid;charname[20];floatscore[3];floatave;floatscore_ave
();voidshow();};studentlm(001,“李美”,100,100,100,100)实例化主程序main(){studentlm(001,“李美",100,100,100);lm.score_ave
();lm.show();}1010补充计算机体系结构1111123.1SimpleKeyboardInputDataStreamDatastreamobjectsareusedtoperformbasicinputandoutputofdatatoandfromv
ariousdevicessuchasthekeyboardandthescreen.Astreamisadatacommunicationobjectconnectedtoaninputoroutputdevice.cout:standardoutputstreamassoc
iatedwithSCREEN<<:Theinsertionoperatorisusedtowritedatatocout.cin:standardoutputstreamassociatedwithKEYBOARD
>>:Theextractionoperatorisusedtoreaddatafromkeyboad.133.1SimpleKeyboardInputExample:eadsanumberfromthekeyboardandstoresiti
nthevariablenum14流(stream)流(stream)表示信息从源到目的端的流动,负责建立数据生产者和消费者之间的联系,数据按顺序从一个对象传送到另一对象。……C++的输入输出流是指由若干字节组成的字节序列,这些字节中的数据按顺序从一个对象传送到另一对象。流15输出流O
utputstream:数据从内存传送到某个载体或设备中;输入流Inputstream:数据从某个载体或设备传送到内存中;------------------------内存----------------------
--coutcin概念理解:输入输出(IO)流16------------------------内存------------------------coutcin概念理解:输入输出(IO)流在C++中,输入输出流被定义为类。C++I/O库中的类称为流类(streamcl
ass)。cin和cout是iostream流类的全局对象,在<iostream>中进行了定义。所以,它们在main()开始之前已经初始化。177.2.2标准输入流cinistream类的对象,它从标准输入
设备(键盘)获取数据.程序中的变量通过流提取符“>>”从流中提取数据,“>>”实际上是istream类的一个成员函数,所以cin>>coutostream类的对象,它向标准输出设备(屏幕)输出数据数据通过流插入符“>>”插入到输出流中,“<<
”实际上是ostream类的一个成员函数,所以cout<<183.2Manipulators(流操纵符)Manipulatorsareusedtomodifyinputandoutputdatastreams.endl,setw,setfill,fi
xed,setprecisionendl:skiptothestartofanewlineonthescreensetw:setthewidthofadatafieldwhichisthenumberofcolumnstha
tthedataitemoccupiesonthescreen;setfill:changethe“padding”characterfromaspacetoanyothercharacter.setw:设置数据域的宽度,即数据项在屏幕上所占的列数。只对下一个数据项起作用。所属头文件
:iomanipcout<<endl<<endl<<"endlcanbeusedanywhere"<<endlsetfill:用于把占位符从空格改变为其他字符。对所有后继项起作用。所属头文件:iomanip193.2ManipulatorsExample:Howtousemanipulat
ors#include<iomanip>Line4isrequiredforanymanipulator,likesetw,thathasavalueinparentheses.Ifthefieldwidthissettoosmalltodisplayaval
ue,thewidthisautomaticallyexpandedsothatallthedigitsinthevaluearedisplayed203.2ManipulatorsExample:Howtousesetfilland
setw•setfillchangethe“padding”characterfromaspaceto“*”•setwappliesonlytothenextdataitemintheoutputstream•setfillremainsineffectforallsubsequentdata
itemssenttotheoutputstream.213.2Manipulators(流操纵符)setprecision:指定要显示的数据的位数。对所有后继项起作用。所属头文件:iomanipfixed放置在se
tprecision之前:指定小数点后的显示位数。对所有后继项起作用。所属头文件:iomanip223.2Manipulatorssetprecision:specifythenumberofdigitsofan
umbertodisplay233.3Single-characterinputandoutputCharacterInputandOutputWhitespaceCharacters(空白字符):generateaninvisibleblankorwhite
spaceonthescreen,suchasTab、Enterandthespacebar.Inputnoskipws:nomatterwhatitisreadfromkeyboardAlternati
vely,thefunctionget()associatedwiththeinputstreamobjectcincanbeusedOutputcoutandits<<;theoutputstreamobjectcouthasamemberfunctionput()thatcanbe
usedtodisplayacharacter;noskips:使用它能够读入空白字符。所属头文件:iomanip或者用:cin.get()显示字符:cout.put()243.3Single-characterinputandoutputExample:Howtoa
chievesingle-charinput&outputAfunctionisablockofprogramcodethatcarriesoutaspecifictask,whichwillbediscussedinfuture.•Readasinglecharacter
fromkeyboard(Includingwhitespacecharacters)25Programmingpitfalls1.Donotmixuptheinsertionoperator<<andtheextractionope
rator>>.Theinsertionoperatorisusedtoinsertdataintotheoutputstream;theextractionoperatorisusedtoreaddata
fromtheinputstream.2.Somemanipulatorsapplyonlytothenextdatafield(e.g.setw);others(e.g.setprecision)stayin
effectforallsubsequentdatafields.3.Theline#include<iomanip>isrequiredforamanipulatorthathasavalueinparenthese
s,e.g.setw(4).Othermanipulators,likeendl,donotrequirethisline.26Quicksyntaxreference27Q&A