《面向对象程序设计》编程题复习及其答案Word文档格式.docx
《《面向对象程序设计》编程题复习及其答案Word文档格式.docx》由会员分享,可在线阅读,更多相关《《面向对象程序设计》编程题复习及其答案Word文档格式.docx(41页珍藏版)》请在冰点文库上搜索。
在题8基础上新增加一个复制构造函数,并主函数新增复制构造对象。
11、(静态成员)
学号num,姓名name,成绩score,累加总分sum,累计人数count。
成员函数包括:
构造函数统计人数count;
(1)非静态成员total函数,求sum。
(2)静态成员average函数,求平均成绩。
(3)主函数定义包含3个学生的数组,求三个学生的平均成绩。
12、(友元函数)
定义一个Time类,私有数据成员包括:
时hour、分minute、秒second
成员函数:
构造函数Time()
定义一个友元函数display,使之访问私有成员输出时分秒。
13、(运算符重载)
(1)定义一个复数类,实现对“+”,>
>
<
<
的重载,使之可实现以下主函数。
intmain()
{
Complexa(3,4),b(1,2),c,d,e,f;
c=a+b;
d=a+5;
e=3+b;
cin>
f;
cout<
f<
endl;
return0;
}
(2)定义一个Time类,实现对“++”的前缀、后缀重载。
P134
i++:
单目运算符天猫优惠券,我就要券
14、(单继承及派生类构造函数)
(1)定义一个person类,数据成员包括:
姓名name,年龄age,性别sex
构造函数及display函数
(2)定义一个派生类student,公有继承与person类,新增成员:
专业major,入学日期enterdate(Date类自定义)。
(3)主函数定义student对象,各类构造函数的调用顺序。
15、(多继承)
在14题基础上由person类再派生出teacher类,新增成员:
职称title,工资wage
16、(虚继承)
由student类和teacher类共同派生出graduate类,新增成员:
年级grade,将person类定义成虚基类,实现用虚继承解决二义性。
17、(抽象类及多态性)
定义一个抽象基类Shape,由它派生出5个派生类,Circle(圆形)、Square(正方形)、Rectangle(矩形)、Trapezoid(梯形)、Triangel(三角形)。
用虚函数分别计算几何图形面积,并求他们的和。
要求定义一个基类指针,使它指向每一个派生类对象,体现其多态性。
1、#include<
iostream>
usingnamespacestd;
voidmax()
inta,b;
"
请输入两个数:
a>
b;
if(a>
b)
cout<
b<
"
a<
else
max();
return0;
2、#include<
voidmax(inta,intb)
voidmax(inta,intb,intc)
b&
&
a>
c)
elseif(b>
c<
max(2,5);
max(7,5,3);
3、#include<
voidmax(inta,intb,intc=0)
4、#include<
voidswap1(inta,intb)
inttemp;
temp=a;
a=b;
b=temp;
voidswap2(int*a,int*b)
temp=*a;
*a=*b;
*b=temp;
voidswap3(int&
a,int&
intx,y;
x=5;
y=7;
swap1(x,y);
x="
x<
y="
y<
swap2(&
x,&
y);
swap3(x,y);
5、#include<
#include<
string>
classstudent
private:
intnum;
stringname;
intage;
charsex;
public:
voidset()
{
请输入:
学号,姓名,年龄,性别(form)"
num>
name>
age>
sex;
}
voiddisplay()
学号:
num<
姓名:
name<
年龄:
age<
性别:
sex<
};
students;
s.set();
s.display();
6、#include<
/*voidset()
*/
student(intnum,stringname,inta,ints)
调用构造函数num="
this->
num=num;
name=name;
age=a;
sex=s;
}天猫优惠券,我就要券
~student()
析构函数学号:
students(1001,"
张三"
25,'
f'
),s1(1002,"
李四"
24,'
m'
);
//s.set();
s1.display();
7、#include<
student()
0<
num=0;
name="
;
age=18;
sex='
),s2;
s2.display();
8、#include<
天猫优惠券,我就要券
student(intnum=0,stringname="
inta=18,ints='
)
天猫优惠券,我就要券学号:
9、#include<
students[3]={student(1001,"
),student(1002,"
)};
for(inti=0;
i<
3;
i++)
天猫优惠券,我就要券
s[i].display();
student*p;
p=newstudent(1003,"
王五"
23,'
p->
display();
deletep;
10、#include<
student(student&
s)
num=s.num;
name=s.name;
age=s.age;
sex=s.sex;
students1(1001,"
students2(s1);
11、
#include<
floatscore;
staticfloatsum;
staticintcount;
floats=80)
score=s;
count++;
成绩:
score<
voidtotal();
staticfloataverage();
floatstudent:
:
sum=0.0;
intstudent:
count=0;
voidstudent:
total()
sum+=score;
average()
returnsum/count;
87),student(1002,"
),student(1003,"
90)};
s[i].total();
平均成绩:
student:
average()<
12、
iostream.h>
//#include<
//usingnamespacestd;
classTime
inthour;
intminute;
intsecond;
Time(inth=12,intm=0,ints=0)
hour=h;
minute=m;
second=s;
friendvoiddisplay(Time&
voiddisplay(Time&
t)
t.hour<
时"
t.minute<
分"
t.second<
秒"
Timet,t1(13,12,22);
display(t);
display(t1);
13、
(1)
//方法1:
多次重载+
classComplex
floatreal;
floatimage;
Complex(floatreal,floatimage)
this->
real=real;
image=image;
Complex()
real=0;
image=0;
friendComplexoperator+(Complex&
a,Complex&
b);
a,floatb);
friendComplexoperator+(floata,Complex&
friendostream&
operator<
(ostream&
output,Complexa);
friendistream&
operator>
(istream&
input,Complexa);
Complexoperator+(Complex&
returnComplex(a.real+b.real,a.image+b.image);
a,floatb)
returnComplex(a.real+b,a.image);
Complexoperator+(floata,Complex&
returnComplex(b.real+a,b.image);
ostream&
output,Complexa)
if(a.image>
0)
output<
a.real<
+"
a.image<
i"
returnoutput;
istream&
input,Complexa)
input>
a.real>
a.image;
returninput;
//方法2:
使用转换构造函数实现
Complex(floatreal,float