您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 企业财务 > 解析C++例题代码第2章例题1
第2章例题【例2-1】//time.h#includeiostreamusingnamespacestd;classTime{public:voidSetTime(intNewH,intNewM,intNewS);voidShowTime();private:intHour,Minute,Second;};//time.cpp#includetime.hvoidTime::SetTime(intNewH,intNewM,intNewS){Hour=NewH;Minute=NewM;Second=NewS;}voidTime::ShowTime(){coutHour:Minute:Secondendl;}//example2_1.cpp#includetime.hintmain(){Timetime1;//声明对象time1time1.ShowTime();//通过对象time1访问公有成员函数Timetime2;//声明对象time2time2.ShowTime();//通过对象time2访问公有成员函数return0;}//example2_1.cpp#includetime.hintmain(){Timetime1;//声明对象time1time1.SetTime(0,0,0);//通过对象time1访问公有成员函数time1.ShowTime();//通过对象time1访问公有成员函数Timetime2;//声明对象time2time2.SetTime(8,30,5);//通过对象time2访问公有成员函数time2.ShowTime();//通过对象time2访问公有成员函数return0;}【例2-2】//example2_2.cpp#includeiostreamusingnamespacestd;classStudent//学生类的声明{public://公有成员函数,外部接口voidinput(intpid,char*pname,floats);voidmodify(floats);voiddisplay();private:intid;charname[10];floatscore;};voidStudent::input(intpid,char*pname,floats)//成员函数的实现{id=pid;strcpy(name,pname);score=s;}voidStudent::modify(floats){score=s;}voidStudent::display(){coutid:idendl;//虽在类外,成员函数仍可访问私有成员coutname:nameendl;coutscore:scoreendl;}intmain(){Students1;//定义Student类的对象s1s1.input(8410101,ZhangHua,95);//通过s1访问公有成员函数s1.display();s1.modify(90);s1.display();return0;}【例2-3】//example2_3.cpp#includeiostreamusingnamespacestd;structAccount{charid[16];//帐号charname[50];//户名charpwd[6];//密码doublebalance;//余额};intmain(){structAccounta;//C++中,可以省略structa.balance=1000000000;coutbalance=a.balanceendl;return0;}【例2-4】//example2_4.cpp#includeiostreamusingnamespacestd;classAccount{charid[16];//帐号charname[50];//户名charpwd[6];//密码doublebalance;//余额};//以括号及分号结束intmain(){Accounta;a.balance=1000000000;}【例2-5】//example2_5.cpp#includeiostreamusingnamespacestd;classTime//时间类的定义{public://外部接口,公有成员函数inlinevoidSetTime(intNewH,intNewM,intNewS);//可省略关键字inlineinlinevoidShowTime();//可省略关键字inlineprivate://私有数据成员intHour,Minute,Second;};//时间类成员函数的具体实现inlinevoidTime::SetTime(intNewH,intNewM,intNewS)//内联函数的显式定义{Hour=NewH;Minute=NewM;Second=NewS;}inlinevoidTime::ShowTime()//内联函数的显式定义{coutHour:Minute:Secondendl;}【例2-6】//example2_6.cpp#includeiostreamusingnamespacestd;intmax(intx,inty);//内联函数的声明intmain(){inta,b;coutInputtwodata:;cinab;coutThemaxis:max(a,b)endl;return0;}inlineintmax(intx,inty)//内联函数的实现{returnxy?x:y;}【例2-7】//example2_7.cpp#includeiostreamusingnamespacestd;classBox{public:voidSet(int,int,int);//函数的声明,省略形参intvolume();private:intheight;intwidth;intlength;};voidBox::Set(inth,intw,intlen)//函数的实现{height=h;width=w;length=len;}intBox::volume(){return(height*width*length);//求体积}intmain(){Boxbox1;//声明对象box1box1.Set(12,25,30);//类外访问成员函数coutThevolumeofbox1isbox1.volume()endl;//类外访问成员函数Boxbox2;//声明对象box2box2.Set(15,30,21);//类外访问成员函数coutThevolumeofbox2isbox2.volume()endl;//类外访问成员函数return0;}【例2-8】//example2_8.cpp#includeiostream#defineSIZE10//符号常量usingnamespacestd;classCStack{public:voidinit(){position=0;}//内联成员函数charpush(charch);//成员函数的声明charpop();private:charstk[SIZE];//默认为私有成员intposition;};charCStack::push(charch)//成员函数的实现{if(position==SIZE){coutendlstackisfull.endl;return0;}stk[position++]=ch;returnch;};charCStack::pop(){if(position==0){coutendlstackisnull.endl;return0;}returnstk[--position];};intmain(){CStacks;//定义对象ss.init();//对象s调用公有成员函数charch;coutPleaseinputcharacters:;cinch;while(ch!='!'&&s.push(ch))//s.push(ch)是对象s调用公有成员函数cinch;coutdatainstack:;while(ch=s.pop())//s.pop()是对象s调用公有成员函数coutch;return0;}【例2-9】//example2_9.cpp#includeiostreamusingnamespacestd;classTime{public:Time();//无参构造函数的声明voidSetTime(intNewH,intNewM,intNewS);voidShowTime();private:intHour,Minute,Second;};Time::Time()//无参构造函数的实现{Hour=0;Minute=0;Second=0;}voidTime::SetTime(intNewH,intNewM,intNewS){Hour=NewH;Minute=NewM;Second=NewS;}voidTime::ShowTime(){coutHour:Minute:Secondendl;}intmain(){Timetime1;//声明对象时没有初始值,自动调用无参构造函数time1.ShowTime();//通过对象time1访问公有成员函数Timetime2;time2.ShowTime();//声明对象时没有初始值,自动调用无参构造函数time2.SetTime(8,30,5);//通过对象调用成员函数重新设值time2.ShowTime();return0;}【例2-10】//example2_10.cpp#includeiostreamusingnamespacestd;classTime{public:Time(intNewH,intNewM,intNewS);//带参构造函数的声明voidShowTime();private:intHour,Minute,Second;};Time::Time(intNewH,intNewM,intNewS)//带参构造函数的实现{Hour=NewH;Minute=NewM;Second=NewS;cout构造函数被调用!endl;}voidTime::ShowTime(){coutHour:Minute:Secondendl;}intmain(){Timetime1(10,24,2);time1.ShowTime();Timetime2(17,9,50);time2.ShowTime();return0;}【例2-11】//example2_11.cpp#includeiostreamusingnamespacestd;classStudent//学生类的声明{public://公有成员函数,外部接口Student(intpid,char*pname,floats);voidmodify(floats);voiddisplay();private:intid;//私有数据成员,外部不可见charname[10];floatscore;};Student::Student(intpid,char*pname,floats)//成员函数的实现{id=pid;strcpy(name,pname);score=s;}voidStudent::modify(floats){score=s;}voidStudent::display(){coutid:idendl;//虽在类外,成员函数仍可访问私有成员coutname:nameendl;coutscore:scoreendl;}intmain()//主函数{Students(8410101,ZhangHu
本文标题:解析C++例题代码第2章例题1
链接地址:https://www.777doc.com/doc-3696428 .html