C++面向对象程序设计 第3章1例程讲解Word文档格式.docx
《C++面向对象程序设计 第3章1例程讲解Word文档格式.docx》由会员分享,可在线阅读,更多相关《C++面向对象程序设计 第3章1例程讲解Word文档格式.docx(7页珍藏版)》请在冰豆网上搜索。
![C++面向对象程序设计 第3章1例程讲解Word文档格式.docx](https://file1.bdocx.com/fileroot1/2022-12/15/94ee99db-c8f4-4503-84b5-6061228a1965/94ee99db-c8f4-4503-84b5-6061228a19651.gif)
}
voidset_time();
voidshow_time();
private:
inthour;
intminute;
intsec;
};
voidTime:
:
set_time()
{cin>
>
hour;
cin>
minute;
sec;
}
show_time()
{cout<
<
hour<
"
minute<
sec<
endl;
intmain()
{
Timet1;
t1.show_time();
Timet2;
t2.show_time();
return0;
3-1-1
#include<
minute=0;
sec=0;
intmain()
t1.set_time();
set_time()
show_time()
cout<
3-1-2
Time();
Time:
Time()
{hour=0;
3-2
classBox
Box(int,int,int);
intvolume();
intheight;
intwidth;
intlength;
Box:
Box(inth,intw,intlen)
{height=h;
width=w;
length=len;
intBox:
volume()
{return(height*width*length);
Boxbox1(12,25,30);
Thevolumeofbox1is"
box1.volume()<
Boxbox2(15,30,21);
Thevolumeofbox2is"
box2.volume()<
3-3在例3.2的基础上,定义两个构造函数,其中一个无参数,一个有参数。
Box();
Box(inth,intw,intlen):
height(h),width(w),length(len){}
Box()
{height=10;
width=10;
length=10;
Boxbox1;
Boxbox2(15,30,25);
3-4将例3.3程序中的构造函数改用含默认值的参数,长、宽、高的默认值均为10。
Box(intw=10,inth=10,intlen=10);
Box(intw,inth,intlen)
}intBox:
Boxbox2(15);
Boxbox3(15,30);
Thevolumeofbox3is"
box3.volume()<
Boxbox4(15,30,20);
Thevolumeofbox4is"
box4.volume()<
}
3-4-2
Box(inth=10,intw=10,intlen=10);
Box(inth,intw,intlen):
height(h),width(w),length(len)
{}