C++简单编程.docx

上传人:b****3 文档编号:826849 上传时间:2022-10-13 格式:DOCX 页数:55 大小:25.41KB
下载 相关 举报
C++简单编程.docx_第1页
第1页 / 共55页
C++简单编程.docx_第2页
第2页 / 共55页
C++简单编程.docx_第3页
第3页 / 共55页
C++简单编程.docx_第4页
第4页 / 共55页
C++简单编程.docx_第5页
第5页 / 共55页
点击查看更多>>
下载资源
资源描述

C++简单编程.docx

《C++简单编程.docx》由会员分享,可在线阅读,更多相关《C++简单编程.docx(55页珍藏版)》请在冰豆网上搜索。

C++简单编程.docx

C++简单编程

功能描述:

找出一个整型数组中的元素的最大值。

要求:

用类来实现,数据和函数都封装在类里面

*/

#include

usingnamespacestd;

#defineN10

classarray

{

public:

voidset_time();

voidmax();

voidshow_time();

private:

intinteger[N];

intmax1;

};

voidarray:

:

set_time()

{

inti;

cout<<"请输入10个整数:

"<

for(i=0;i<10;i++)

{

cin>>integer[i];

}

cout<<"请输出10个整数:

"<

for(i=0;i<10;i++)

{

cout<

}

}

voidarray:

:

max()

{

inti;

max1=integer[0];

for(i=1;i<10;i++)

{

if(integer[i]>max1)

{

max1=integer[i];

}

}

}

voidarray:

:

show_time()

{

cout<<"请输出最大的数值max1:

"<

}

intmain(void)

{

arraymax_integer;

max_integer.set_time();

max_integer.max();

max_integer.show_time();

return0;

}

/*

功能描述:

请完善该程序,在类中增加一个对数据成员赋初值的成员函数set_value,

调试并运行。

给出了类定义的头文件student.h,包含成员函数定义的源文件student.cpp

以及包含主函数的源文件main.cpp。

(1)类定义的头文件student.h中的内容:

(2)成员函数定义的源文件student.cpp中的内容:

*/

#include

usingnamespacestd;

classStudent

{public:

voiddisplay();

voidset_value();

private:

intnum;

charname[20];

charsex[10];

};

voidStudent:

:

set_value()

{

cout<<"请分别输入学号、姓名、性别:

"<

cin>>num>>name>>sex;

}

voidStudent:

:

display()

{cout<<"num:

"<

cout<<"name:

"<

cout<<"sex:

"<

}

intmain(void)

{

Studentstu1;

stu1.set_value();

stu1.display();

return0;

}

/*

功能描述:

声明一个时钟类,包含小时Hour、分钟Minutes、秒Second等3个数据成员,

有2个公有成员函数,分别是:

时间设置函数SetTime

(intNewH=0,intNewM=0,intNewS=0)

和时间显示函数ShowTime()。

在主函数main()中。

利用时间设置函数SetTime设置时间,

当调用时间显示函数ShowTime()时就显示设置的时间。

*/

#include

usingnamespacestd;

classclock

{

public:

clock(intNewH=0,intNewM=0,intNewS=0);

voidShowTime();

private:

intHour;

intMinutes;

intSecond;

};

clock:

:

clock(intNewH,intNewM,intNewS)

{

Hour=NewH;

Minutes=NewM;

Second=NewS;

}

voidclock:

:

ShowTime()

{

cout<<"现在的时间为:

"<

}

intmain(void)

{

clocktime_1(12,34,12);

time_1.ShowTime();

clocktime_2(12,34);

time_2.ShowTime();

clocktime_3(12);

time_3.ShowTime();

clocktime_4;

time_4.ShowTime();

return0;

}

/*

功能描述:

定义一个图书类Book,类中包括name(书名)、author(作者)和sale(销售量)

三个数据成员以及带三个参数的(所有参数都具有默认值)

构造函数、析构函数、设置信息的函数和显示信息的函数。

编写相应程序对所定义的类进行测试。

*/

#include

usingnamespacestd;

#include

classBook

{

public:

Book(stringna="C++",stringau="li",intsa=0):

name(na),author(au),sale(sa){}

voidshow_time();

~Book();

private:

stringname;

stringauthor;

intsale;

};

Book:

:

~Book()

{

cout<<"析构函数被调用!

"<

}

voidBook:

:

show_time()

{

cout<<"name:

"<

"<

"<

}

intmain(void)

{

BookBook_1;

Book_1.show_time();

BookBook_2("datastructure","YanWeiMin",29);

Book_2.show_time();

return0;

}

/*

功能描述:

需要求3个长方柱的体积,请编写一个基于对象的程序。

数据成员包括length(长)

witch(宽)、height(高)。

要求用成员函数实现以下功能:

1)由键盘分别输入3个长方柱的长宽高;

2)计算长方柱的体积;

3)输出3个长方柱的体积。

请编程序,上机调试并运行。

#include

usingnamespacestd;

classmath

{

public:

voidset_time();

voidvolume();

voidshow_time();

private:

floatlength;

floatwitch;

floatheight;

floatv;

};

voidmath:

:

set_time()

{

cout<<"请输入长方体的长、宽、高:

"<

cin>>length>>witch>>height;

}

voidmath:

:

volume()

{

v=length*witch*height;

}

voidmath:

:

show_time()

{

cout<<"输出长方体的体积:

"<

cout<

}

intmain(void)

{

mathvolume_1;

volume_1.set_time();

volume_1.volume();

volume_1.show_time();

mathvolume_2;

volume_2.set_time();

volume_2.volume();

volume_2.show_time();

mathvolume_3;

volume_3.set_time();

volume_3.volume();

volume_3.show_time();

return0;

}

/*

功能描述:

找出一个整型数组中的元素的最大值。

要求:

1、用类来实现,数据和函数都封装在类里面

2、写为一个多文件的程序:

(1)将类定义放在头文件arraymax.h中;

(2)将成员函数定义放在源文件arraymax.cpp中;

(3)主函数放在源文件file1.cpp中。

请写出完整的程序,调试并运行。

#include

usingnamespacestd;

#defineN10

classarraymax

{

public:

voidset_time();

voidmax();

voidshow_time();

private:

inti;

intinteger[N];

intmax1;

};

voidarraymax:

:

set_time()

{

cout<<"请输入10个整数:

"<

for(i=0;i<10;i++)

{

cin>>integer[i];

}

cout<<"请输出10个整数:

"<

for(i=0;i<10;i++)

{

cout<

}

}

voidarraymax:

:

max()

{

max1=integer[0];

for(i=1;i<10;i++)

{

if(integer[i]>max1)

{

max1=integer[i];

}

}

}

voidarraymax:

:

show_time()

{

cout<<"请输出最大的数值max1:

"<

}

intmain(void)

{

arraymaxmax_integer;

max_integer.set_time();

max_integer.max();

max_integer.show_time();

return0;

}

/*

功能描述:

定义一个日期类Date,包含年、月、日三个数据成员,

以及一个求第二天日期的成员函数和输出日期的成员函数。

*/

#include

usingnamespacestd;

classDate

{

public:

voidset_time();

voidsecond_time();

voidshow_time();

private:

intyear;

intmonth;

intday;

};

voidDate:

:

set_time()

{

cout<<"请分别输入年、月、日:

"<

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > IT计算机 > 电脑基础知识

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

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