c++编程题题库.docx
《c++编程题题库.docx》由会员分享,可在线阅读,更多相关《c++编程题题库.docx(18页珍藏版)》请在冰豆网上搜索。
c++编程题题库
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
usingnamespacestd;
classTime
{public:
voidset_time()
{cin>>hour;
cin>>minute;
cin>>sec;
}
voidshow_time()
{cout<"<"<private:
inthour;
intminute;
intsec;
};
intmain()
{
Timet1;
t1.set_time();
t1.show_time();
return0;
}
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
usingnamespacestd;
classBox
{public:
voidget_value();
voidvolume();
private:
floatlengh;
floatwidth;
floatheight;
};
voidBox:
:
get_value()
{cout<<"pleaseinputlengh,width,height:
";
cin>>lengh;
cin>>width;
cin>>height;
}
voidBox:
:
volume()
{cout<<"volmueofbox1is"<intmain()
{Boxbox1;
box1.get_value();
box1.volume();
return0;
}
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
usingnamespacestd;
classArray_max
{public:
voidset_value();
voidmax_value();
private:
intarray[10];
intmax;
};
voidArray_max:
:
set_value()
{inti;
for(i=0;i<10;i++)
cin>>array[i];
}
voidArray_max:
:
max_value()
{inti;
max=array[0];
for(i=1;i<10;i++)
if(array[i]>max)max=array[i];
cout<<"max="<}
intmain()
{Array_maxarrmax;
arrmax.set_value();
arrmax.max_value();
return0;
}
1.4
编写一个程序,用成员函数重载运算符“+”,使之能用于两个复数相加。
#include
usingnamespacestd;
classComplex
{public:
Complex(){real=0;imag=0;}
Complex(doubler,doublei){real=r;imag=i;}
Complexoperator+(Complex&c2);
voiddisplay();
private:
doublereal;
doubleimag;
};
ComplexComplex:
:
operator+(Complex&c2)
{Complexc;
c.real=real+c2.real;
c.imag=imag+c2.imag;
returnc;}
voidComplex:
:
display()
{cout<<"("<intmain()
{Complexc1(3,4),c2(5,-10),c3;
c3=c1+c2;
cout<<"c1=";c1.display();
cout<<"c2=";c2.display();
cout<<"c1+c2=";c3.display();
return0;
}
1.5
编写一个程序,用友元函数重载运算符“+”,使之能用于两个复数相加。
#include
classComplex
{public:
Complex(){real=0;imag=0;}
Complex(doubler){real=r;imag=0;}
Complex(doubler,doublei){real=r;imag=i;}
friendComplexoperator+(Complex&c1,Complex&c2);
voiddisplay();
private:
doublereal;
doubleimag;
};
Complexoperator+(Complex&c1,Complex&c2)
{returnComplex(c1.real+c2.real,c1.imag+c2.imag);}
voidComplex:
:
display()
{cout<<"("<intmain()
{Complexc1(3,4),c2(5,-10),c3;
c3=c1+c2;
cout<<"c1=";c1.display();
cout<<"c2=";c2.display();
cout<<"c1+c2=";c3.display();
return0;
}
1.6
编写一个基于对象的程序,求圆球的体积,要求:
(1)定义一个圆球类Circle,类有私有数据成员radius(半径),公有成员函数get_value()、volume()。
(2)get_value()函数和volume()函数在类外定义。
get_value()作用是从键盘输入半径的值,volume()的作用是计算圆球的体积并在屏幕上显示。
(圆球体积计算公式为:
v=4/3πr3)
(3)在main()函数定义Circle类的对象circle1,并调用get_value()函数给球半径赋值,调用volume()函数输出圆球的体积。
#include
usingnamespacestd;
classCircle
{public:
voidget_value();
voidvolume();
private:
floatradius;
};
voidCircle:
:
get_value()
{cout<<"pleaseinputradius:
";
cin>>radius;
}
voidCircle:
:
volume()
{cout<<"volmueofcircle1is"<<4.0/3*3.14159*radius*radius*radius<intmain()
{Circlecircle1;
circle1.get_value();
circle1.volume();
return0;
}
1.7
编写一个基于对象的程序,要求:
(1)定义一个日期类Date,类有私有数据成员year(年)、month(月)、day(日),公有成员函数set_date()、show_date()。
(2)set_date()函数和show_date()函数在类外定义。
set_date()作用是从键盘输入年、月、日的值,show_date()的作用是在屏幕上显示年、月、日的值。
(3)在main()函数定义Date类的对象d1,并调用set_date()函数给日期赋值,调用show_date()函数输出日期的值。
#include
usingnamespacestd;
classDate
{public:
voidset_date();
voidshow_date();
private:
intyear;
intmonth;
intday;
};
voidDate:
:
set_date()
{cin>>year;
cin>>month;
cin>>day;
}
voidDate:
:
show_date()
{cout<
intmain()
{
Dated1;
d1.set_date();
d1.show_date();
return0;
}
2.1
编写一个面向对象的程序,要求:
(1)定义一个基类Student,类有私有数据成员num(学号)、name()、sex(性别),公有成员函数get_value()、display(),get_value()作用是从键盘给num、name、sex赋值,display()的作用是显示num、name、sex的值。
(2)定义一个派生类Student1,Student1公有继承自Student类。
Student1类新增私有数据成员age(年龄)、addr(地址),新增公有成员函数get_value_1()、display_1()。
get_value_1()的作用是实现从键盘给num、name、sex、age、addr赋值,display_1()的作用是显示num、name、sex、age、addr的值。
(3)在main()函数定义Student1类的对象stud1,并调用get_value_1()函数给对象赋值,调用display_1()函数显示学生的所有信息。
#include
usingnamespacestd;
classStudent
{public:
voidget_value()
{cin>>num>>name>>sex;}
voiddisplay()
{cout<<"num:
"<cout<<"name:
"<cout<<"sex:
"<private:
intnum;
charname[10];
charsex;
};
classStudent1:
publicStudent
{public:
voidget_value_1()
{get_value();
cin>>age>>addr;}
voiddisplay_1()
{display();
cout<<"age:
"<cout<<"address:
"<private:
intage;
charaddr[30];
};
intmain()
{Student1stud1;
stud1.get_value_1();
stud1.display_1();
return0;
}
2.2
编写一个面向对象的程序,要求:
(1)定义一个基类Student,类有私有数据成员num(学号)、name()、sex(性别),公有成员函数get_value()、display(),get_value()作用是从键盘给num、name、sex赋值,display()的作用是显示num、name、sex的值。
(2)定义一个派生类Student1,Student1私有继承自Student类。
Student1类新增私有数据成员age(年龄)、addr(地址),新增公有成员函数get_value_1()、display_1()。
get_value_1()的作用是实现从键盘给num、name、sex、age、addr赋值,display_1()的作用是显示num、name、sex、age、addr的值。
(3)在main()函数定义Student1类的对象stud1,并调用get_value_1()函数给对象赋值,调用display_1()函数显示学生的所有信息。
#include
usingnamespacestd;
classStudent
{public:
voidget_value()
{cin>>num>>name>>sex;}
voiddisplay()
{cout<<"num:
"<cout<<"name:
"<cout<<"sex:
"<private:
intnum;
charname[10];
charsex;
};
classStudent1:
privateStudent
{public:
voidget_value_1()
{get_value();
cin>>age>>addr;}
voiddisplay_1()
{display();
cout<<"age:
"<cout<<"address:
"<private:
intage;
charaddr[30];
};
intmain()
{Student1stud1;
stud1.get_value_1();
stud1.display_1();
return0;
}
2.3
编写一个面向对象的程序,要求:
(1)定义一个基类Student,类有私有数据成员num(学号)、name()、sex(性别),公有成员函数get_value()、display(),get_value()作用是从键盘给num、name、sex赋值,display()的作用是显示num、name、sex的值。
(2)定义一个派生类Student1,Student1保护继承自Student类。
Student1类新增私有数据成员age(年龄)、addr(地址),新增公有成员函数get_value_1()、display_1()。
get_value_1()的作用是实现从键盘给num、name、sex、age、addr赋值,display_1()的作用是显示num、name、sex、age、addr的值。
(3)在main()函数定义Student1类的对象stud1,并调用get_value_1()函数给对象赋值,调用display_1()函数显示学生的所有信息。
#include
usingnamespacestd;
classStudent
{public:
voidget_value()
{cin>>num>>name>>sex;}
voiddisplay()
{cout<<"num:
"<cout<<"name:
"<cout<<"sex:
"<private:
intnum;
charname[10];
charsex;
};
classStudent1:
protectedStudent
{public:
voidget_value_1()
{get_value();
cin>>age>>addr;}
voiddisplay_1()
{display();
cout<<"age:
"<cout<<"address:
"<private:
intage;
charaddr[30];
};
intmain()
{Student1stud1;
stud1.get_value_1();
stud1.display_1();
return0;
}
2.4
编写一个面向对象的程序,要求:
(1)定义一个基类Student,类有保护数据成员num(学号)、name()、sex(性别),公有成员包括构造函数、show()函数。
构造函数带3个参数用于定义对象时赋初值,show()函数作用是显示学生信息,即num、name、sex的值。
(2)定义一个派生类Student1,Student1公有继承自Student类。
Student1类新增私有数据成员age(年龄)、addr(地址),新增公有成员包括构造函数、show()函数。
构造函数带5个参数用于定义对象时赋初值,show()函数作用是显示学生信息,即num、name、sex、age、addr的值。
(3)在main()函数定义Student1类的对象stud1并赋初值,调用show()函数显示该学生的所有信息。
#include
#include
usingnamespacestd;
classStudent
{public:
Student(intn,stringnam,chars)
{num=n;
name=nam;
sex=s;}
voidshow()
{cout<<"num:
"<cout<<"name:
"<cout<<"sex:
"<}
protected:
intnum;
stringname;
charsex;
};
classStudent1:
publicStudent
{public:
Student1(intn,stringnam,chars,inta,charad[]):
Student(n,nam,s)
{age=a;
addr=ad;
}
voidshow()
{Student:
:
show();
cout<<"age:
"<cout<<"address:
"<}
private:
intage;
stringaddr;
};
intmain()
{Student1stud1(10010,"Wang-li",'f',19,"115BeijingRoad,Shanghai");
stud1.show();
return0;
}
2.5
编写一个面向对象的程序,要求:
(1)定义一个基类Student,类有保护数据成员num(学号)、name(),公有成员包括构造函数、show()函数。
构造函数带2个参数用于定义对象时赋初值,show()函数作用是显示学生信息,即num、name的值。
(2)定义一个派生类Student1,Student1公有继承自Student类。
Student1类新增私有数据成员age(年龄)、addr(地址)以及子对象monitor(班长,Student类型),新增公有成员包括构造函数、show()函数。
构造函数带6个参数用于定义对象时赋初值,show()函数作用是显示学生的所有信息,即本人的num、name、age、addr以及班长的num、name。
(3)在main()函数定义Student1类的对象stud1并赋初值,调用show()函数显示该学生的所有信息。
#include
#include
usingnamespacestd;
classStudent
{public:
Student(intn,stringnam)
{num=n;
name=nam;
}
voidshow()
{cout<<"num:
"<cout<<"name:
"<}
protected:
intnum;
stringname;
};
classStudent1:
publicStudent
{public:
Student1(intn,stringnam,intn1,stringnam1,inta,stringad)
:
Student(n,nam),monitor(n1,nam1)
{age=a;
addr=ad;
}
voidshow()
{cout<<"Thisstudentis:
"<Student:
:
show();
cout<<"age:
"<cout<<"address:
"<cout<<"Classmonitoris:
"<monitor.show();
}
private:
Studentm