C++第四次实验.docx

上传人:b****5 文档编号:4715477 上传时间:2022-12-07 格式:DOCX 页数:25 大小:109.03KB
下载 相关 举报
C++第四次实验.docx_第1页
第1页 / 共25页
C++第四次实验.docx_第2页
第2页 / 共25页
C++第四次实验.docx_第3页
第3页 / 共25页
C++第四次实验.docx_第4页
第4页 / 共25页
C++第四次实验.docx_第5页
第5页 / 共25页
点击查看更多>>
下载资源
资源描述

C++第四次实验.docx

《C++第四次实验.docx》由会员分享,可在线阅读,更多相关《C++第四次实验.docx(25页珍藏版)》请在冰豆网上搜索。

C++第四次实验.docx

C++第四次实验

天津理工大学

计算机与通信工程学院

 

实验报告

2013至2014学年第二学期

 

课程名称

高级语言程序设计Ⅱ

实验(4)

实验名称

多态程序设计

实验时间

2014年4月28日第3节至第6节

学号姓名

专业

信息安全

主讲教师

唐召东

辅导教师

唐召东

软件环境

 

VC++6

硬件环境

 

PC机

实验目的

(1)掌握运算符重载的方法;

(2)掌握使用虚函数实现动态多态性;

实验内容(应包括实验题目、实验要求、实验任务等)

二、练习项目:

1.定义Point类,有坐标x,y两个成员变量,利用友元函数对Point类重载“++”运算符,实现对坐标值的改变。

具体要求如下:

(1)编写程序定义Point类,在类中定义整型的私有成员变量x,y;

(2)在类中定义两个友元函数,分别重载前置++和后置++;

(3)编写主函数测试。

注意函数有无返回值的区别,以及返回值是否带有&应用符号。

2.定义Point类,有坐标x,y两个成员变量,利用运算符重载对Point类重载“++”运算符,实现对坐标值的改变。

具体要求如下:

(1)编写程序定义Point类,在类中定义整型的私有成员变量x,y;

(2)定义成员函数Point&operator++();Pointoperator++(int);以实现对Point类重载“++”运算符,分别重载前置++和后置++;

(3)编写主函数测试。

3.定义一个分数类,通过重载运算符实现分数的四则运算、求负运算和赋值运算。

其中,要求加法“+”和减法“-”用友元函数实现重载,其他运算符用成员函数实现重载。

4.编写程序,定义抽象基类Container,由此派生出2个派生类球体类Sphere,圆柱体类Cylinder,分别用虚函数分别计算表面积和体积。

(1)球体的表面积为:

,球体的体积为

(2)圆柱表面积为:

2πR(h+R)圆柱体的体积πR2h。

定义相应的对象,编写主函数测试。

5.设计一个时钟类TIME,内含数据成员hour,minute,second表示时间,成员函数set()设置时间数据,show()显示时间数据。

重载运算符++和--(具有返回值),每执行一次++,second自增1,执行一次--,second自减1。

second和minute的值在0~59区间循环(满59后再自增则归0,minute加1;second为0时再自减则为59,minute减1)。

hour的值在0~23区间循环。

 

实验过程与实验结果(可包括实验实施的步骤、算法描述、流程、结论等)

1.源代码:

#include

usingnamespacestd;

classPoint

{

private:

doublex,y;

public:

Point(double_x=0,double_y=0)

{

x=_x;

y=_y;

}

~Point(){}

voidsetx(double_x)

{

x=_x;

}

voidsety(double_y)

{

y=_y;

}

doublegetx()

{

returnx;

}

doublegety()

{

returny;

}

friendvoidoperator<<(ostream&out,constPoint&p)

{

out<<"Point("<

}

friendvoidoperator++(Point&p,int);

friendvoidoperator++(Point&p);

};

voidoperator++(Point&p,int)

{

p.x++;

p.y++;

}

voidoperator++(Point&p)

{

++p.x;

++p.y;

}

intmain()

{

Pointa(1,1),b(3,4);

cout<

cout<

a++;

++b;

cout<

cout<

return0;

}

输出结果:

2.源代码:

#include

usingnamespacestd;

classPoint

{

private:

doublex,y;

public:

Point(double_x=0,double_y=0)

{

x=_x;

y=_y;

}

~Point(){}

voidsetx(double_x)

{

x=_x;

}

voidsety(double_y)

{

y=_y;

}

doublegetx()

{

returnx;

}

doublegety()

{

returny;

}

friendvoidoperator<<(ostream&out,constPoint&p)

{

out<<"Point("<

}

Pointoperator++(int)

{

x+=2;

y+=2;

return*this;

}

Point&operator++()

{

++x;

++y;

return*this;

}

};

intmain()

{

Pointa(1,1),b(3,4);

cout<

cout<

a++;

++b;

cout<

cout<

return0;

}

输出结果:

3.源代码:

#include

#include

classFenshu

{

private:

intzi;

intmu;

public:

Fenshu(int_zi=0,int_mu=1)

{

zi=_zi;

mu=_mu;

}

~Fenshu(){}

voidsetzi(int_zi=0)

{

zi=_zi;

}

voidsetmu(int_mu=1)

{

mu=_mu;

}

intgetzi()

{

returnzi;

}

intgetmu()

{

returnmu;

}

voidprint();

friendFenshuoperator+(constFenshu&f1,constFenshu&f2);

friendFenshuoperator-(constFenshu&f1,constFenshu&f2);

Fenshuoperator*(intn)

{

Fenshux;

x.zi=zi*n;

x.mu=mu;

returnx;

}

Fenshuoperator/(intn)

{

if(n==0)

{

cout<<"Nagetive!

";

return*this;

}

Fenshux;

x.zi=zi;

x.mu=mu*n;

returnx;

}

Fenshuoperator-()

{

Fenshux;

x.zi=-zi;

x.mu=mu;

returnx;

}

voidoperator=(constFenshu&f)

{

zi=f.zi;

mu=f.mu;

}

};

Fenshuoperator+(constFenshu&f1,constFenshu&f2)

{

Fenshuf;

f.zi=f1.zi*f2.mu+f2.zi*f1.mu;

f.mu=f1.mu*f2.mu;

returnf;

}

Fenshuoperator-(constFenshu&f1,constFenshu&f2)

{

Fenshuf;

f.zi=f1.zi*f2.mu-f2.zi*f1.mu;

f.mu=f1.mu*f2.mu;

returnf;

}

voidFenshu:

:

print()

{

if(zi==0)cout<<0<

else

{

int_zi=abs(zi>mu?

zi:

mu),_mu=abs(mu

mu:

zi);

intx;

x=_zi%_mu;

while(x!

=0)

{

_mu=_zi;

_zi=x;

x=_mu%_zi;

}

zi/=(_zi>_mu?

_mu:

_zi);

mu/=(_zi>_mu?

_mu:

_zi);

cout<

}

}

intmain()

{

Fenshux(2,4),y(4,4);

Fenshuz,a,b,c,d,e;

z=x+y;

a=y-x;

b=x-y;

c=-x;

d=y*(-1);

e=y/2;

z.print();

a.print();

b.print();

c.print();

d.print();

e.print();

return0;

}

输出结果:

4.源代码:

#include

usingnamespacestd;

constdoublePI=3.14;

classContainer

{

private:

doubler;

doubleh;

public:

~Container(){}

Container(double_r=0.0,double_h=0.0)

{

r=_r;

h=_h;

}

voidsetr(double_r=0.0)

{

r=_r;

}

doublegetr()

{

returnr;

}

voidseth(double_h=0.0)

{

h=_h;

}

doublegeth()

{

returnh;

}

virtualdoubles()=0;

virtualdoublev()=0;

};

classSphere:

publicContainer

{

public:

~Sphere(){}

Sphere(double_r=0.0):

Container(_r){}

doubles()

{

return4*PI*getr()*getr();

}

doublev()

{

return(double(4)/3)*PI*getr()*getr()*getr();

}

};

classCylinder:

publicContainer

{

public:

~Cylinder(){}

Cylinder(double_r=0.0,double_h=0.0):

Container(_r,_h){}

doubles()

{

return2*PI*getr()*(geth()+getr());

}

doublev()

{

return2*PI*getr()*geth();

}

};

intmain()

{

Container*p;

Spherea

(1);

Cylinderb(1,2);

p=&a;

cout<<"Spheres="<s()<<"v="<v()<

p=&b;

cout<<"Cylinders="<s()<<"v="<v()<

return0;

}

输出结果:

5.源代码:

#include

usingnamespacestd;

classTime

{

private:

inthour;

intminute;

intsecond;

public:

Time(int=0,int=0,int=0);

~Time(){}

voidsethour(int);

voidsetminute(int);

voidsetsecond(int);

intgethour();

intgetsecond();

intgetminute();

voidset(int=0,int=0,int=0);

voidshow();

Time&operator++(int);

Time&operator--(int);

};

Time:

:

Time(inth,intm,ints)

{

if(s>59)

{

s%=60;

m=m+s/60;

}

if(m>59)

{

m%=60;

h=h+m/60;

}

if(h>23)h%=24;

hour=h;

minute=m;

second=s;

}

voidTime:

:

sethour(inth)

{

if(h>23)h%=24;

hour=h;

}

voidTime:

:

setminute(intm)

{

if(minute>59)

{

minute%=60;

hour=hour+minute/60;

}

if(hour>23)hour%=24;

minute=m;

}

voidTime:

:

setsecond(ints)

{

if(s>59)

{

minute=minute+s/60;

s%=60;

}

if(minute>59)

{

hour=hour+minute/60;

minute%=60;

}

if(hour>23)hour%=24;

second=s;

}

intTime:

:

gethour()

{

returnhour;

}

intTime:

:

getsecond()

{

returnsecond;

}

intTime:

:

getminute()

{

returnminute;

}

voidTime:

:

set(inth,intm,ints)

{

if(s>59)

{

m=m+s/60;

s%=60;

}

if(m>59)

{

h=h+m/60;

m%=60;

}

if(h>23)h%=24;

hour=h;

minute=m;

second=s;

}

voidTime:

:

show()

{

cout<

"<

"<

}

Time&Time:

:

operator++(int)

{

second++;

if(second>59)

{

minute=minute+second/60;

second%=60;

}

if(minute>59)

{

hour=hour+minute/60;

minute%=60;

}

if(hour>23)hour%=24;

return*this;

}

Time&Time:

:

operator--(int)

{

second--;

if(second<0)

{

minute--;

second=59;

}

if(minute<0)

{

minute=59;

hour--;

}

if(hour<0)hour=0;

return*this;

}

intmain()

{

Timet;

intx,y,z;

chartemp;

cout<<"现在初始化计数器(HourMinuteSecond):

";

cin>>x;cin>>y;cin>>z;

t.set(x,y,z);

cout<<"现在时间是:

";

t.show();

do

{

cout<<"输入*重新设置计数器;"<

cout<<"输入+计数器递加,输入-计数器递减;"<

cout<<"输入字母o计数器清零;"<

cout<<"输入q退出计数器。

"<

cout<<"请输入>";

cin>>temp;

switch(temp)

{

case'*':

cout<<"现在初始化计数器(HourMinuteSecond):

";

cin>>x;cin>>y;cin>>z;

t.set(x,y,z);

cout<<"现在时间是:

";

t.show();

break;

case'+':

t++;

cout<<"现在时间是:

";

t.show();

break;

case'-':

t--;

cout<<"现在时间是:

";

t.show();

break;

case'o':

t.set(0,0,0);

cout<<"现在时间是:

";

t.show();

break;

case'q':

break;

default:

cout<<"请输入正确的数据!

"<

break;

}

}while(temp!

='q');

return0;

}

输出结果:

{

if(sub->getname()==sub_m->getname())

*sub=*sub_m;

sub_m++;

}

sub_m-=num_m;

sub++;

}

sub-=shumu;

}

voidCstudent:

:

insert_s()//录入分数

{

inttemp;

cout<<"Pleaseinputthescores.\n";

for(inti=0;i

{

cout<getname()<<':

'<

cin>>temp;

sub->setscore(temp);

sub++;

}

sub-=shumu;

}

voidCstudent:

:

dill()

{

intsum=0;doublec=0.0,s=0.0;

for(inti=0;i

{

if(sub->getscore()>=95&&sub->getscore()<=100)sub->setpoint(4.5);

elseif(sub->getscore()>=90&&sub->getscore()<=95)sub->setpoint(4.0);

elseif(sub->getscore()>=85&&sub->getscore()<=90)sub->setpoint(3.5);

elseif(sub->getscore()>=80&&sub->getscore()<=85)sub->setpoint(3.0);

elseif(sub->getscore()>=75&&sub->getscore()<=80)sub->setpoint(2.5);

elseif(sub->getscore()>=70&&sub->getscore()<=75)sub->setpoint(2.0);

elseif(sub->getscore()>=65&&sub->getscore()<=70)sub->setpoint(1.5);

elseif(sub->getscore()>=60&&sub->getscore()<=65)sub->setpoint(1.0);

else

{

sub->setpoint(0.0);

sub->setflag(true);

}

sum=sum+sub->getscore();

c=c+sub->getxuefen()*sub->getpoint();

s=s+sub->getxuefen();

sub++;

}

sub-=shumu;

ave_g=double(sum)/shumu;

ave_p=c/s;

}

voidCstudent:

:

print_h()

{

cout<<"IDNamesexclassdateaverageofscoreaverageofpoint\n";

cout<<"--------------------------------------------------------------------------------\n";

}

voidCstudent:

:

print()

{

cout<

cout<<"Thescoreofeachsubject:

\n";

cout<<"Namescorepoint"<

cout<<"----------------------------\n";

for(inti=0;i

{

cout<getname()<<""<getscore()<<""<getpoint()<

sub++;

}

sub-=shumu;

cout<<"Theretestsubjectis:

\n";

intkey=0;

for(inti=0;i

{

if(sub->getflag()==true)

{

key=1;

cout<getname()<

}

sub++;

}

sub-=shumu;

if(key==0)cout<<"none\n";

}

intmain()

{

Cstudent

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

当前位置:首页 > 经管营销 > 公共行政管理

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

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