C++面向对象程序设计 第3章1例程讲解.docx
- 文档编号:5362566
- 上传时间:2022-12-15
- 格式:DOCX
- 页数:7
- 大小:14.72KB
C++面向对象程序设计 第3章1例程讲解.docx
《C++面向对象程序设计 第3章1例程讲解.docx》由会员分享,可在线阅读,更多相关《C++面向对象程序设计 第3章1例程讲解.docx(7页珍藏版)》请在冰豆网上搜索。
C++面向对象程序设计第3章1例程讲解
3-1
include
usingnamespacestd;
classTime
{public:
Time(){hour=0;minute=0;sec=0;}
voidset_time();
voidshow_time();
private:
inthour;
intminute;
intsec;
};
voidTime:
:
set_time()
{cin>>hour;cin>>minute;cin>>sec;}
voidTime:
:
show_time()
{cout< "< "< intmain() { Timet1;t1.show_time();Timet2;t2.show_time(); return0; } 3-1-1 #include usingnamespacestd; classTime {public: Time(){hour=0;minute=0;sec=0;} voidset_time(); voidshow_time(); private: inthour; intminute; intsec; }; intmain() { Timet1;t1.set_time();t1.show_time(); Timet2;t2.show_time(); return0; } voidTime: : set_time() {cin>>hour;cin>>minute;cin>>sec;} voidTime: : show_time() { cout< "< "< } 3-1-2 #include usingnamespacestd; classTime {public: Time(); voidshow_time(); private: inthour; intminute; intsec; }; Time: : Time() {hour=0;minute=0;sec=0;} intmain() { Timet1;t1.show_time(); Timet2;t2.show_time(); return0; } voidTime: : show_time() {cout< "< "< 3-2 #include usingnamespacestd; classBox {public: Box(int,int,int); intvolume(); private: intheight; intwidth; intlength; }; Box: : Box(inth,intw,intlen) {height=h;width=w;length=len;} intBox: : volume() {return(height*width*length);} intmain() { Boxbox1(12,25,30); cout<<"Thevolumeofbox1is"< Boxbox2(15,30,21); cout<<"Thevolumeofbox2is"< return0; } 3-3在例3.2的基础上,定义两个构造函数,其中一个无参数,一个有参数。 #include usingnamespacestd; classBox {public: Box(); Box(inth,intw,intlen): height(h),width(w),length(len){} intvolume(); private: intheight; intwidth; intlength; }; Box: : Box() {height=10; width=10; length=10; } intBox: : volume() {return(height*width*length); } intmain() { Boxbox1; cout<<"Thevolumeofbox1is"< Boxbox2(15,30,25); cout<<"Thevolumeofbox2is"< return0; } 3-4将例3.3程序中的构造函数改用含默认值的参数,长、宽、高的默认值均为10。 #include usingnamespacestd; classBox {public: Box(intw=10,inth=10,intlen=10); intvolume(); private: intheight; intwidth; intlength; }; Box: : Box(intw,inth,intlen) {height=h; width=w; length=len; } }intBox: : volume() {return(height*width*length); } intmain() { Boxbox1; cout<<"Thevolumeofbox1is"< Boxbox2(15); cout<<"Thevolumeofbox2is"< Boxbox3(15,30); cout<<"Thevolumeofbox3is"< Boxbox4(15,30,20); cout<<"Thevolumeofbox4is"< return0; } 3-4-2 #include usingnamespacestd; classBox {public: Box(inth=10,intw=10,intlen=10); intvolume(); private: intheight; intwidth; intlength; }; Box: : Box(inth,intw,intlen): height(h),width(w),length(len) {} intBox: : volume() {return(height*width*length); } intmain() { Boxbox1; cout<<"Thevolumeofbox1is"< Boxbox2(15); cout<<"Thevolumeofbox2is"< Boxbox3(15,30); cout<<"Thevolumeofbox3is"< Boxbox4(15,30,20); cout<<"Thevolumeofbox4is"< return0; }
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- C+面向对象程序设计 第3章1例程讲解 C+ 面向 对象 程序设计 例程 讲解