好文档 - 专业文书写作范文服务资料分享网站

c++编程题库题库.doc

天下 分享 时间: 加入收藏 我要投稿 点赞

1.1

编写一个基于对象的程序,要求: (1)定义一个时间类Time,类内有私有数据成员hour(小时)、minute(分钟)、sec(秒),公有成员函数set_time()、show_time()。

(2)set_time()函数和show_time()函数在类内定义。set_time()作用是从键盘输入时间、分钟、秒的值,show_time()的作用是在屏幕上显示时间、分钟、秒的值。 (3)在main()函数定义Time类的对象t1,并调用set_time()函数给时间赋值,调用show_time()函数输出时间的值。 #include using namespace std; class Time {public:

void set_time() {cin>>hour; cin>>minute; cin>>sec; }

void show_time()

{cout<

private: int hour; int minute; int sec; };

int main() {

Time t1;

t1.set_time(); t1.show_time(); return 0; } 1.2

编写一个基于对象的程序,求长方体的体积,要求: (1)定义一个长方体类Box,类内有私有数据成员lengh(长)、width(宽)、height(高),公有成员函数get_value()、volume()。

(2)get_value()函数和volume()函数在类外定义。get_value()作用是从键盘输入长、宽、高的值,volume()的作用是计算长方体的体积并在屏幕上显示。 (3)在main()函数定义Box类的对象box1,并调用get_value()函数给长、宽、高赋值,调用volume()函数输出长方体体积。 #include using namespace std; class Box {public:

void get_value(); void volume(); private:

float lengh; float width; float height; };

void Box::get_value()

{ cout<<\ cin>>lengh;

cin>>width; cin>>height; }

void Box::volume() { cout<<\of box1 is \

int main() {Box box1;

box1.get_value(); box1.volume(); return 0; }

1.3.

编写一个基于对象的程序,求一个有十个数据的整型数组中元素的最大值,要求: (1)定义一个类Array_max,类内有私有数据成员array[10]、max分别存储十个整数、最大值,公有成员函数set_value()、max_volume()。

(2)set_value()函数和max_volume()函数在类外定义。get_value()作用是从键盘输入数组十个元素的值,max_volume()的作用是求出并显示数组元素的最大值。

(3)在main()函数定义Array_max类的对象arrmax,并调用set_value()函数给数组赋值,调用max_volume()函数求出并显示数组元素的最大值。 #include using namespace std; class Array_max {public:

void set_value();

void max_value(); private:

int array[10]; int max; };

void Array_max::set_value() { int i;

for (i=0;i<10;i++) cin>>array[i]; }

void Array_max::max_value() {int i;

max=array[0];

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

if(array[i]>max) max=array[i]; cout<<\ }

int main()

{Array_max arrmax; arrmax.set_value(); arrmax.max_value(); return 0; } 1.4

编写一个程序,用成员函数重载运算符“+”,使之能用于两个复数相加。

#include

using namespace std; class Complex {public:

Complex(){real=0;imag=0;}

Complex(double r,double i){real=r;imag=i;} Complex operator + (Complex &c2); void display(); private:

double real; double imag; };

Complex Complex::operator + (Complex &c2) {Complex c;

c.real=real+c2.real; c.imag=imag+c2.imag; return c;}

void Complex::display()

{cout<<\

int main()

{Complex c1(3,4),c2(5,-10),c3; c3=c1+c2;

cout<<\ cout<<\ cout<<\ return 0; } 1.5

编写一个程序,用友元函数重载运算符“+”,使之能用于两个

c++编程题库题库.doc

1.1编写一个基于对象的程序,要求:(1)定义一个时间类Time,类内有私有数据成员hour(小时)、minute(分钟)、sec(秒),公有成员函数set_time()、show_time()。(2)set_time()函数和show_time()函数在类内定义。set_time()作用是从键盘输入时间、分钟、秒的值,show_time()
推荐度:
点击下载文档文档为doc格式
2gkmz0u7l73uh255c6he20sz532alg00cdv
领取福利

微信扫码领取福利

微信扫码分享