《C++面向对象程序设计》实验报告.docx

上传人:b****7 文档编号:9787699 上传时间:2023-02-06 格式:DOCX 页数:20 大小:82.73KB
下载 相关 举报
《C++面向对象程序设计》实验报告.docx_第1页
第1页 / 共20页
《C++面向对象程序设计》实验报告.docx_第2页
第2页 / 共20页
《C++面向对象程序设计》实验报告.docx_第3页
第3页 / 共20页
《C++面向对象程序设计》实验报告.docx_第4页
第4页 / 共20页
《C++面向对象程序设计》实验报告.docx_第5页
第5页 / 共20页
点击查看更多>>
下载资源
资源描述

《C++面向对象程序设计》实验报告.docx

《《C++面向对象程序设计》实验报告.docx》由会员分享,可在线阅读,更多相关《《C++面向对象程序设计》实验报告.docx(20页珍藏版)》请在冰豆网上搜索。

《C++面向对象程序设计》实验报告.docx

《C++面向对象程序设计》实验报告

《C++面向对象程序设计》实验报告

实验序号:

2      实验项目名称:

类和对象

学  号

姓  名

专业

实验地点

指导教师

实验时间

一、实验目的及要求

(1)理解类和对象的概念;

(2)了解C++在非面向对象方面对C功能的扩充与增强。

(3)初步掌握使用类和对象编制C++程序。

(4)掌握对象数组、对象指针和string类的使用方法。

(5)掌握使用对象、对象指针和对象引用作为函数参数的方法。

(6)掌握类对象作为成员的使用方法。

(7)掌握静态数据成员和静态成员函数的使用方法。

(8)理解友元的概念和掌握友元的使用方法。

二、实验设备(环境)及要求

MicorsoftVisualC++6.0

三、实验内容与步骤(题目、算法和结果描述)

1、输入下列程序。

#include

usingnamespacestd;

classCoordinate

{

public:

Coordinate(intx1,inty1)

{x=x1;

y=y1;}

Coordinate(Coordinate&p);

~Coordinate()

{cout<<"Destructoriscalled\n";}

intgetx()

{returnx;}

intgety()

{returny;}

private:

intx,y;

};

Coordinate:

:

Coordinate(Coordinate&p)

{

x=p.x;

y=p.y;

cout<<"Copy-initializationConstructouriscalled\n";

}

intmain()

{

Coordinatep1(3,4);

Coordinatep2(p1);

Coordinatep3=p2;

cout<<"p3=("<

return0;

}

(1)写出程序的运行结果。

【运行结果截图】:

(2)将Coordinator类中带有两个参数的构造函数进行修改,在函数体内增添下列语句:

cout<<”constructoriscalled.\n”;

【运行结果截图】:

【运行结果分析】:

带有两个参数的构造函数在主函数中只被调用了一次,其他的都是调用使用对象引用作为函数参数的构造函数。

(3)按下列要求进行调试:

在主函数体内,添加下列语句:

Coordinatorp4;

Coordinatorp5

(2);

调试程序时会出现什么错误?

为什么?

如何对已有的构造函数进行适当修改?

【运行结果截图】:

【解释】:

把带有两个参数的构造函数改为带有默认参数的构造函数,那么后添加的两条语句编译的时候就不会出错了。

(4)经过以上第

(2)步和第(3)步的修改后,结合运行结果分析:

创建不同的对象时会调用不同的构造函数。

【运行结果分析】:

p1是调用有两个参数的构造函数,p2和p3都是p1的拷贝,但他们调用的是使用对象引用作为函数参数的构造函数,p4和p5都是调用两个参数的构造函数,p4由于没有给出实参,所以使用默认参数,即x=1,y1;而p5给出一个实参,所以x=2,y=1.

2、设计一个4*4魔方程序,让魔方的各行值的和等于各列值的和,并且等于两对角线的和,例如以下魔方,各行各列及两对角线值的和都是64.

313525

9211915

17131123

727291

【提示】:

求4*4的魔方的一般步骤如下:

(1)设置初始魔方的起始值和相邻元素之间的差值。

例如上述魔方的初始魔方的起始值(first)和相邻元素之间的差值(step)分别为:

first=1;step=2;

(2)设置初始魔方元素的值,例如上述魔方的初始魔方为:

1357

9111315

17192123

25272931

(3)生成最终魔方。

方法如下:

1)求最大元素值与最小元素值的和sum,该实例的sum是:

1+31=32

2)用32减去初始魔方所有对角线上元素的值,然后将结果放在原来的位置,这样就可以求得最终魔方。

本题的魔法类magic的参考框架如下:

classmagic

{public:

voidgetdata();

voidgetfirstmagic();

voidgeneratemagic();

voidprintmagic();

private:

intm[4][4];

intstep;

intfirst;

intsum;

}

【运行结果截图】:

3、设计一个用来表示直角坐标系的Location类,在主程序中创建类Location的两个对象A和B,要求A的坐标点在第3象限,B的坐标点在第2象限,分别采用成员函数和友元函数计算给定两个坐标点之间的距离,要求按如下格式输出结果:

A(x1,y1),B(x2,y2),

Distance1=d1

Distance2=d2

其中:

x1、y1、x2、y2为指定坐标值,d1和d2为两个坐标点之间的距离。

【提示】:

类Location的参考框架如下:

classLocation

{public:

Location(double,double);//构造函数

doublegetx();//成员函数,取x坐标值

doublegety();//成员函数,取y坐标值

doubledistancee(Location&);//成员函数,求给定两点之间的距离

frienddoubledistancee(Location&,Location&);//友元函数,求给定两点之间的距离

private:

doublex,y;

}

【运行结果截图】:

4、声明一个Student类,在该类中包括一个暑假成员score(分数)、两个静态数据成员total_score(总分)和count(学生人数);还包括一个成员函数account()用于设置分数、累计学生的成绩之和、累计学生人数,一个静态成员函数sum()用于返回学生的成绩之和,另一个静态成员函数average()用于求全部成绩的平均值。

在main函数中,输入某班同学的成绩,并调用上述函数求出全班同学的成绩之和和平均分。

【Student类的框架】

classStudent

{

public:

voidaccount();

staticfloatsum();

staticfloataverage();

private:

floatscore;

staticfloattotal_score;

staticintcount;

};

【运行结果截图】:

5、使用C++的string类,将5个字符串按逆转后的顺序显示出来。

例如,逆转前5个字符的字符串是:

GermanyJapanAmericaBritainFrance

按逆转后的顺序输出字符串是:

FranceBritainAmericaJapanGermany

【运行结果截图】:

6、定义一个圆类(Circle),属性为半径(radius)和圆周长、面积,操作为输入半径并计算周长、面积,输出半径、周长和面积。

要求定义构造函数(以半径为参数,缺省值为0,

圆周长和面积在构造函数中生成)和复制构造函数。

【运行结果截图】:

7、教材P134习题[3.33]和[3.34]

【运行结果截图】:

四、分析与讨论(记录实验过程中出现的主要问题和心得体会)

 

五、教师评语

签名:

日期:

成绩

附:

程序源代码

1、#include

usingnamespacestd;

classCoordinate

{

public:

Coordinate(intx1=1,inty1=1)

{x=x1;

y=y1;

cout<<"constructoriscalled.\n";}

Coordinate(Coordinate&p);

~Coordinate()

{cout<<"Destructoriscalled\n";}

intgetx()

{returnx;}

intgety()

{returny;}

private:

intx,y;

};

Coordinate:

:

Coordinate(Coordinate&p)

{

x=p.x;

y=p.y;

cout<<"Copy-initializationConstructouriscalled\n";

}

intmain()

{

Coordinatep1(3,4);

Coordinatep2(p1);

Coordinatep3=p2;

cout<<"p3=("<

Coordinatep4;

Coordinatep5

(2);

return0;

}

2、#include

usingnamespacestd;

classmagic

{

public:

voidgetdata();

voidgetfirstmagic();

voidgeneratemagic();

voidprintmagic();

private:

intm[4][4];

intstep;

intfirst;

intsum;

};

voidmagic:

:

getdata()

{

cout<<"pleaseinputfirstandstep:

";

cin>>first>>step;

}

voidmagic:

:

getfirstmagic()

{

inti,j,t;

t=first;

for(i=0;i<4;i++)

for(j=0;j<4;j++)

{

m[i][j]=first;

first+=step;

}

sum=t+m[3][3];

}

voidmagic:

:

generatemagic()

{

inti,j;

for(i=0;i<4;i++)

m[i][i]=sum-m[i][i];

for(i=0,j=3;i<4,j>=0;i++,j--)

m[i][j]=sum-m[i][j];

}

voidmagic:

:

printmagic()

{

inti,j;

for(i=0;i<4;i++)

{

for(j=0;j<4;j++)

{

cout<

}

cout<

}

}

intmain()

{

magicA;

A.getdata();

A.getfirstmagic();

A.generatemagic();

A.printmagic();

return0;

}

3、#include

#include

classLocation

{

public:

Location(double,double);

doublegetx();

doublegety();

doubledistance(Location&);

frienddoubledistance(Location&,Location&);

private:

doublex,y;

};

Location:

:

Location(doubler,doublei)

{

x=r;

y=i;

}

doubleLocation:

:

getx()

{

returnx;

}

doubleLocation:

:

gety()

{

returny;

}

doubleLocation:

:

distance(Location&t)

{

doubledx=x-t.x;

doubledy=y-t.y;

returnsqrt(dx*dx+dy*dy);

}

doubledistance(Location&a,Location&b)

{

doubledx=a.x-b.x;

doubledy=a.y-b.y;

return(sqrt(dx*dx+dy*dy));

}

intmain()

{

LocationA(-1,-1);

LocationB(-2,2);

cout<<"A("<

doubled=A.distance(B);

cout<<"Distance1="<

cout<<"Distance2="<

return0;

}

4、#include

usingnamespacestd;

classStudent

{

public:

voidaccount(float);

staticfloatsum();

staticfloataverage();

private:

floatscore;

staticfloattotal_score;

staticintcount;

};

voidStudent:

:

account(floatscore1)

{

score=score1;

total_score+=score;

count++;

}

floatStudent:

:

sum()

{

floatk=total_score;

returnk;

}

floatStudent:

:

average()

{

floatt=total_score/(count-1);

returnt;

}

floatStudent:

:

total_score=0.0;

intStudent:

:

count=0;

intmain()

{

floatscore1;

intn=0;

Studenta;

cout<<"-----请输入学生成绩-----"<

cout<<"----若要退出输入按00----"<

do{

cin>>score1;

a.account(score1);

if(score1==00)

break;

n++;

}while

(1);

cout<<"theclasssizeis:

"<

cout<<"thetotalscoreis:

"<

cout<<"theaveragescoreis:

"<

return0;

}

5、#include

#include

usingnamespacestd;

intmain()

{

inti;

stringch[5];

for(i=0;i<5;i++)

cin>>ch[i];

for(i=4;i>=0;i--)

cout<

cout<

return0;

}

6、#include

usingnamespacestd;

classCircle

{

private:

doubler,area,circumference;

public:

Circle(double);

Circle(constCircle&);

doubleR();

doubleArea();

doubleCircumference();

};

Circle:

:

Circle(doublet)

{

r=t;

}

Circle:

:

Circle(constCircle&p)

{

r=2*p.r;

}

doubleCircle:

:

R()

{

returnr;

}

doubleCircle:

:

Area()

{

return3.14159265*r*r;

}

doubleCircle:

:

Circumference()

{

return2*3.14159265*r;

}

intmain()

{

Circlep1(1.0);

cout<<"thecircle'sRis:

"<

cout<<"thecircle'sAreais:

"<

cout<<"thecircel'scircumferenceis:

"<

Circlep2(p1);

cout<<"thecircle'sRis:

"<

cout<<"thecircle'sAreais:

"<

cout<<"thecircel'scircumferenceis:

"<

Circlep3(3.05);

cout<<"thecircle'sRis:

"<

cout<<"thecircle'sAreais:

"<

cout<<"thecircel'scircumferenceis:

"<

return0;

}

7、

3.33

#include

usingnamespacestd;

classbook

{

private:

intqu;

intprice;

public:

book(int);

intgetx();

intride();

};

book:

:

book(inti)

{

qu=i;

price=10*qu;

}

intbook:

:

ride()

{

returnprice*qu;

}

intmain()

{

booka[5]={1,2,3,4,5};

for(inti=0;i<5;i++)

cout<

cout<

return0;

}

3.34

#include

usingnamespacestd;

classbook

{

private:

intqu;

intprice;

public:

book(int);

intgetx();

intride();

};

book:

:

book(inti)

{

qu=i;

price=10*qu;

}

intbook:

:

ride()

{

returnprice*qu;

}

intmain()

{

booka[5]={1,2,3,4,5};

book*p;

p=&a[4];

for(inti=4;i>=0;i--)

{

cout<ride()<<'';

p--;

}

cout<

return0;

}

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

当前位置:首页 > 自然科学 > 生物学

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

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