C++面向对象程序设计 第3章1例程讲解.docx

上传人:b****6 文档编号:5362566 上传时间:2022-12-15 格式:DOCX 页数:7 大小:14.72KB
下载 相关 举报
C++面向对象程序设计 第3章1例程讲解.docx_第1页
第1页 / 共7页
C++面向对象程序设计 第3章1例程讲解.docx_第2页
第2页 / 共7页
C++面向对象程序设计 第3章1例程讲解.docx_第3页
第3页 / 共7页
C++面向对象程序设计 第3章1例程讲解.docx_第4页
第4页 / 共7页
C++面向对象程序设计 第3章1例程讲解.docx_第5页
第5页 / 共7页
点击查看更多>>
下载资源
资源描述

C++面向对象程序设计 第3章1例程讲解.docx

《C++面向对象程序设计 第3章1例程讲解.docx》由会员分享,可在线阅读,更多相关《C++面向对象程序设计 第3章1例程讲解.docx(7页珍藏版)》请在冰豆网上搜索。

C++面向对象程序设计 第3章1例程讲解.docx

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模板 > 商务科技

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1