面向对象编程复习.docx

上传人:b****7 文档编号:8880446 上传时间:2023-02-02 格式:DOCX 页数:14 大小:19.94KB
下载 相关 举报
面向对象编程复习.docx_第1页
第1页 / 共14页
面向对象编程复习.docx_第2页
第2页 / 共14页
面向对象编程复习.docx_第3页
第3页 / 共14页
面向对象编程复习.docx_第4页
第4页 / 共14页
面向对象编程复习.docx_第5页
第5页 / 共14页
点击查看更多>>
下载资源
资源描述

面向对象编程复习.docx

《面向对象编程复习.docx》由会员分享,可在线阅读,更多相关《面向对象编程复习.docx(14页珍藏版)》请在冰豆网上搜索。

面向对象编程复习.docx

面向对象编程复习

1.声明一个复数类Complex,重载运算符“+”,“-”,使之能用于复数的加、减,运算符重载函数作为Complex类的成员函数。

请编程序实现。

2.分别声明Teacher(教师)类和Cadre(干部)类,采用多重继承方式由这两个类派生出新类Teacher_Cadre(教师兼干部)。

要求:

(1)在两个基类中都包含姓名、年龄、性别、地址、电话等数据成员。

(2)在Teacher类中还包含数据成员title(职称),在Cadre类中还包含数据成员post(职务)。

在Teacher_Cadre类中包含数据成员wages(工资)。

(3)对两个基类中的姓名、年龄、性别、地址、电话等数据成员用相同的名字,在引用这些数据成员时,指定作用域。

(4)在类体中声明成员函数,在类外定义成员函数。

在派生类Teacher_Cadre的成员函数show中调用Teacher类中的display函数,输出姓名、年龄、性别、职称、地址、电话,然后再用cout语句输出职务与工资。

3.已定义一个Shape抽象类,在此基础上派生出矩形Rectangle和圆形Circle类,二者都有GetPerim()函数计算对象的周长,并编写测试main()函数。

classShape

{

public:

Shape(){}

~Shape(){}

virtualfloatGetPerim()=0;

}

4.编写一个有关日期(年、月、日)和时间(时、分、秒)的程序。

该程序建立三个类,其中一个是日期的类Date,一个是时间的类Time,另一个是日期和时间类DateTime,它是前面两个类为基类的派生类。

要求通过DateTime的对象能输出指定日期和时间。

5.编程计算图形的面积。

程序可计算圆形、长方形、正方形的面积,运行时先提示用户选择图形的类型,然后,对圆形要求用户输入半径值,对长方形用户输入长和宽的值,对正方形要求用户输入边长的值,计算出面积的值后将其显示出来。

要求:

能够计算不同类型形状的的面积,并充分体现面向对象程序设计的类、对象、继承、多态特性。

(海伦公式求三角形面积:

a,b,c分别为三角形三边长,p=(a+b+c)/2,则三角形面面积为s=sqrt(p*(p-a)*(p-b)*(p-c)))

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

数据成员包括length(长)、width(宽)、height(高)。

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

(1)由键盘输入3个长方体的长、宽、高;

(2)计算长方体体积;

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

7.商店销售某一商品,商店每天公布统一的折扣(discount)。

同时允许销售人员在销售时灵活掌握售价(price),在此基础上,对一次购10件以上者还可以享受9.8折优惠。

现已知当天3个销货员销售情况为

销售员号(num)

销售件数(quantity)

销售单价(price)

101

5

23.5

102

12

24.56

103

100

21.5

请编程序,计算出当日此商品的总销售款sum以及每件商品的平均售价。

要求用静态数据成员和静态成员函数。

 

程序源码:

1.

#include

#include

usingnamespacestd;

classComplex

{

private:

doublereal,image;

public:

Complex(doublex=0,doubley=0):

real(x),image(y){}

Complexoperator+(constComplex&rhs)const;

Complexoperator-(constComplex&rhs)const;

Complexoperator*(constComplex&rhs)const;

Complexoperator/(constComplex&rhs)const;

};

ComplexComplex:

:

operator+(constComplex&rhs)const

{

Complexc;

c.real=this->real+rhs.real;

c.image=this->image+rhs.image;

cout<<"相加的结果为:

"<=0?

"+":

"")<

returnc;

}

ComplexComplex:

:

operator-(constComplex&rhs)const

{

Complexc;

c.real=this->real-rhs.real;

c.image=this->image-rhs.image;

cout<<"相减的结果为:

"<=0?

"+":

"")<

returnc;

}

ComplexComplex:

:

operator*(constComplex&rhs)const

{

Complexc;

c.real=this->real*rhs.real-this->image*rhs.image;

c.image=this->real*rhs.image+this->image*rhs.real;

cout<<"相乘的结果为:

"<=0?

"+":

"")<

returnc;

}

ComplexComplex:

:

operator/(constComplex&rhs)const

{

doublem=rhs.real*rhs.real+rhs.image*rhs.image;

assert(m!

=0);

Complexc;

c.real=this->real*rhs.real+this->image*rhs.image;

c.image=this->image*rhs.real-this->real*rhs.image;

c.real/=m;

c.image/=m;

cout<<"相除的结果为:

"<=0?

"+":

"")<

returnc;

}

intmain()

{

Complexa(3,5),b(4,2);

(a+b);

(a-b);

(a*b);

(a/b);

}

 

2.

#include

#include

usingnamespacestd;

classTeacher

{

public:

Teacher(stringn,inta,chars,stringadd,longintte,stringti);

voiddisplay();

protected:

stringname;

intage;

charsex;

stringaddress;

longinttel;

stringtitle;//职称

};

classCadre

{

public:

Cadre(stringn,inta,chars,stringadd,longintt,stringp);

voiddisplay();

protected:

stringname;

intage;

charsex;

stringaddress;

longinttel;

stringpost;//职务

};

classTeacher_Cadre:

publicTeacher,publicCadre//声明多重继承的Teacher_Cadre类

{

public:

Teacher_Cadre(stringn,inta,chars,stringadd,longintt,stringti,stringp,doublew);

voidshow();

protected:

doublewages;

};

Teacher:

:

Teacher(stringn,inta,chars,stringadd,longintte,stringti)

{

name=n;

age=a;

sex=s;

address=add;

tel=te;

title=ti;

}

Cadre:

:

Cadre(stringn,inta,chars,stringadd,longintt,stringp)

{

name=n;

age=a;

sex=s;

address=add;

tel=t;

post=p;

}

voidTeacher:

:

display()

{

cout<<"name:

"<

cout<<"age:

"<

cout<<"sex:

"<

cout<<"address:

"<

cout<<"tel:

"<

cout<<"title:

"<

}

voidCadre:

:

display()

{

cout<<"name:

"<

cout<<"age:

"<

cout<<"sex:

"<

cout<<"address:

"<

cout<<"tel:

"<

cout<<"post:

"<

}

voidTeacher_Cadre:

:

show()//在派生类Teacher_Cadre的成员函数show中调用Teacher类中的display函数

{

Teacher:

:

display();

cout<<"post:

"<

:

post<

cout<<"wages:

"<

}

Teacher_Cadre:

:

Teacher_Cadre(stringn,inta,chars,stringadd,longintt,stringti,stringp,doublew):

Teacher(n,a,s,add,t,ti),Cadre(n,a,s,add,t,p)

{

wages=w;

}

intmain()

{

Teacher_Cadrect1("hk",24,'m',"xi'an",152999111,"主任","技术工",10000);

ct1.show();

cout<

return0;

}

3.

#include

//#include

//#include

usingnamespacestd;

classShape

{

public:

Shape(){}

~Shape(){}

virtualfloatGetPerim()=0;

};

classRectangle:

publicShape

{public:

Rectangle(floati,floatj):

L(i),W(j){}

~Rectangle(){}

floatGetPerim(){return2*(L+W);}

private:

floatL,W;

};

classCircle:

publicShape

{public:

Circle(floatr):

R(r){}

floatGetPerim(){return3.14*2*R;}

private:

floatR;

};

voidmain()

{

Shape*sp;

sp=newCircle(10);

cout<GetPerim()<

sp=newRectangle(6,4);

cout<GetPerim()<

}

4.

#include

//#include

//#include

usingnamespacestd;

classTime

{

public:

Time(inth,intm,ints):

hour(h),minute(m),sec(s){}

voiddisplay(){cout<

"<

"<

protected:

inthour;

intminute;

intsec;

};

classDate

{

public:

Date(inty,intmo,intd):

year(y),month(mo),day(d){}

voidshow(){cout<

protected:

intyear;

intmonth;

intday;

};

classTimedate:

publicDate,publicTime

{

public:

Timedate(inty,intmo,intd,inth,intm,ints):

Date(y,mo,d),Time(h,m,s){}

voiddisplayall(){show();display();}

};

intmain()

{

Dated(2009,6,8);

d.show();

Timet(19,47,58);

t.display();

TimedateT(2009,6,8,19,37,50);

T.displayall();

return0;

}

5.(此题程序仅供参考)

#include

#include//因为用到了sqrt()函数,sqrt()为开根号

//#include

//#include

usingnamespacestd;

intmain()

{

cout<<"1计算圆面积"<

<<"2计算矩形面积"<

<<"3计算三角形面积"<

<<"4退出"<

<<"请选择相应程序代码:

";

intx;//x对应编号

cin>>x;

doublearea;

while(x!

=4)//当x!

=4时循环,即当x=4时退出循环

{

if(x==1)

{

doubler;

cout<<"半径:

";

cin>>r;

area=3.14*r*r;

cout<<"面积为"<

}

elseif(x==2)

{

doublea,b;

cout<<"长和宽:

";

cin>>a>>b;

area=a*b;

cout<<"面积为"<

}

elseif(x==3)

{

doublea,b,c,d;

cout<<"三边长:

";

cin>>a>>b>>c;

d=0.5*(a+b+c);

area=sqrt(d*(d-a)*(d-b)*(d-c));

cout<<"面积为"<

}

else

cout<<"输入有误,请重新输入!

"<

cout<<"程序代码:

";

cin>>x;

}

return0;

}

6.

#include

//#include//因为用到了sqrt()函数,sqrt()为开根号

//#include

//#include

usingnamespacestd;

classcuboid

{

public:

voidset_cuboid();

voidshow_cuboid();

private:

doublelength;

doublewidth;

doubleheight;

doublevolume;

};

voidcuboid:

:

set_cuboid()

{

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

"<

cin>>length>>width>>height;

}

voidcuboid:

:

show_cuboid()

{

volume=length*width*height;

cout<<"长方形体积为:

"<

}

intmain()

{

cuboidcuboida,cuboidb,cuboidc;

cuboida.set_cuboid();

cuboida.show_cuboid();

cuboidb.set_cuboid();

cuboidb.show_cuboid();

cuboidc.set_cuboid();

cuboidc.show_cuboid();

return0;

}

7.

#include

////#include//因为用到了sqrt()函数,sqrt()为开根号

////#include

////#include

usingnamespacestd;

classsalesman{

public:

salesman(){

quantity=0;

price=0;}

staticfloataverage();

staticvoiddisplay();

voidtotal();voidset();

private:

staticfloatdiscount;

staticfloatsum;

staticintn;

intquantity;

floatprice;};

voidsalesman:

:

set(){

cout<<"销售件数"<

cin>>quantity;

cout<<"销售单价"<

cin>>price;

}

voidsalesman:

:

total(){

if(quantity>=10)

price=price*0.98;

sum+=quantity*price*discount;

n+=quantity;}

floatsalesman:

:

average(){

return(sum/n);

}

floatsalesman:

:

discount=0.9;

intsalesman:

:

n=0;

floatsalesman:

:

sum=0;

voidsalesman:

:

display(){

cout<<"总销售款为"<

cout<<"平均售价为"<

:

average()<

}

intmain(){

salesmansal[3];

sal[0].set();

sal[0].total();

sal[1].set();

sal[1].total();

sal[2].set();

sal[2].total();

salesman:

:

display();

return0;

}

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

当前位置:首页 > 高等教育 > 农学

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

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