实验五实验报告.docx

上传人:b****3 文档编号:3448907 上传时间:2022-11-23 格式:DOCX 页数:12 大小:81.23KB
下载 相关 举报
实验五实验报告.docx_第1页
第1页 / 共12页
实验五实验报告.docx_第2页
第2页 / 共12页
实验五实验报告.docx_第3页
第3页 / 共12页
实验五实验报告.docx_第4页
第4页 / 共12页
实验五实验报告.docx_第5页
第5页 / 共12页
点击查看更多>>
下载资源
资源描述

实验五实验报告.docx

《实验五实验报告.docx》由会员分享,可在线阅读,更多相关《实验五实验报告.docx(12页珍藏版)》请在冰豆网上搜索。

实验五实验报告.docx

实验五实验报告

实验五类和对象

(二)

一、实验目的:

1.进一步理解类和对象的概念;

2.进一步理解类的成员的访问控制的含义,公有和私有成员的区别;

3.掌握构造函数和析构函数的含义与作用、定义方式和实现;

4.能够根据给定的要求定义类并实现类的成员函数;

5.了解C++面向对象程序设计的基本思想、基本方法和基本步骤;

6.掌握MSVisualC++6.0调试C++程序的基本方法、基本步骤。

二、实验内容:

1、输入下列程序,按要求进行实验,并记录实验的结果。

#include

usingnamespacestd;

classCoordinate

{

public:

Coordinate(intx1,inty1){x=x1;y=y1;}

Coordinate(Coordinate&p);

~Coordinate(){cout<<"Destructoriscalled."<

intgetx(){returnx;}

intgety(){returny;}

private:

intx,y;

};

Coordinate:

:

Coordinate(Coordinate&p){

x=p.x;y=p.y;

cout<<"Copyinitianizationconstructoriscalled."<

}

intmain(){

Coordinatep1(3,4);

Coordinatep2(p1);

Coordinatep3=p2;

cout<<"p3=("<

return0;

}

(1)记录程序的运行结果

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

cout<<”Constructoriscalled.”<

重新记录程序的运行结果,并解释输出结果。

(3)按下列要求进行测试。

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

Coordinatep4;

Coordinatep5

(2);

测试程序会出现什么错误?

为什么?

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

(4)结合第

(2)和第(3)步的修改和运行结果:

分析创建不同的对象时如何调用适当构造函数?

2、根据注释语句的提示实现类Date的成员函数,并将完整的程序编译、连接成功以保证程序能够正确运行。

#include

classDate{

public:

voidprintDate();//显示日期

voidsetDay(int);//设置日的值

voidsetMonth(int);//设置月的值

voidsetYear(int);//设置年的值

private:

intday,month,year;

};

intmain(){

DatetestDay;

testDay.setDay(24);

testDay.setMonth(10);

testDay.setYear(2012);

testDay.printDate();

return0;

};

3、下面是一个计算器类的定义,请完成该类的成员函数的实现,并设计一个主函数使用该类和相关成员函数以测试该类设计的正确性。

#include

usingnamespacestd;

classcoutner

{

public:

counter(intnumber);//构造函数

voidincrement();//给value原值加2

voiddecrement();//给value原值减2

voidsetvalue(int);//设置计数器值

intgetvalue();//取得计数器值

voidprint();//显示计数

private:

intvalue;//数据成员

};

intmain(){

...//定义counter类的几个对象并调用有关成员函数

return0;

}

4、定义一个类ClsName,要求该类设计如下:

(1)该类有两个整型数据成员x和y;

(2)为该类重载三个不同的构造函数:

分别为无参数、带一个参数和带两个参数的构造函数,要求在构造函数中输出必要的信息以示区别;

(3)设计2个成员函数用来读取数据成员x和y;

(4)设计2个成员函数用来设置数据成员x和y;

(5)设计1个成员函数用来在屏幕上打印输出数据成员x和y;

(6)在main()函数中用三个不同的构造函数创建3个对象,并使用所有的成员函数对这些对象进行必要的操作。

三、实验设计

1、

(2)程序:

#include

usingnamespacestd;

classCoordinate

{

public:

Coordinate(intx1,inty1){x=x1;

y=y1;

cout<<"Constructoriscalled."<

Coordinate(Coordinate&p);

~Coordinate(){cout<<"Destructoriscalled."<

intgetx(){returnx;}

intgety(){returny;}

private:

intx,y;

};

Coordinate:

:

Coordinate(Coordinate&p){

x=p.x;y=p.y;

cout<<"Copyinitianizationconstructoriscalled."<

}

intmain(){

Coordinatep1(3,4);

Coordinatep2(p1);

Coordinatep3=p2;

cout<<"p3=("<

return0;

}

2.

#include

classDate{

public:

voidprintDate()//显示日期

{

cout<

cout<

cout<

}

voidsetDay(int)//设置日的值

{

intd;

d=day=24;

}

voidsetMonth(int)//设置月的值

{

intm;

m=month=10;

}

voidsetYear(int)//设置年的值

{

inty;

y=year=2012;

}

private:

intday,month,year;

};

intmain(){

DatetestDay;

testDay.setDay(24);

testDay.setMonth(10);

testDay.setYear(2012);

testDay.printDate();

return0;

};

3.

#include

usingnamespacestd;

classCoutner

{

public:

coutner(intnumber)//构造函数

{value=number;}

voidincrement(intin)//给value原值加2

{value=in+2;}

voiddecrement(intde)//给value原值减2

{value=de-2;}

voidsetvalue(inta)//设置计数器值

{value=a;}

intgetvalue()//取得计数器值

{returnvalue;}

voidprint()//显示计数

{

cout<

}

private:

intvalue;//数据成员

};

intmain()

{

Coutnertestcoutner;//定义coutner类的几个对象并调用有关成员函数

testcoutner.setvalue(4);

testcoutner.print();

testcoutner.getvalue();

testcoutner.print();

testcoutner.increment(4);

testcoutner.print();

testcoutner.decrement(4);

testcoutner.print();

return0;

}

4.

#include

usingnamespacestd;

classClsname

{

public:

voidsetx(inta)

{

x=a;

}

voidsety(intb)

{

y=b;

}

intgetx()

{returnx;}

intgety()

{returny;}

Clsname();

Clsname(inta);

Clsname(inta,intb);

voidshowClsname();

private:

intx;

inty;

};

Clsname:

:

Clsname()

{

x=1;

y=1;

}

Clsname:

:

Clsname(inta)

{

x=a;

}

Clsname:

:

Clsname(inta,intb)

{

x=a;

y=b;

}

inlinevoidClsname:

:

showClsname()

{cout<

}

voidmain()

{

inta,b,i,j;

cout<<"pleaseenterthenumberaandb:

"<

cin>>a>>b;

Clsnameop1(1,1);

op1.setx

(1);

op1.sety

(1);

i=op1.getx();

j=op1.gety();

op1.showClsname();

cout<<"x="<

Clsnameop2(a,1);

op2.setx(a);

op2.sety

(1);

i=op2.getx();

j=op2.gety();

op2.showClsname();

cout<<"x="<

Clsnameop3(a,b);

op3.setx(a);

op3.sety(b);

i=op3.getx();

j=op3.gety();

op3.showClsname();

cout<<"x="<

}

4、实验结果

1、

(1)

(2)

(3)出错处

修改:

Coordinatep4=p3;

Coordinatep5(p2);

2.

 

3.

4.

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

当前位置:首页 > 农林牧渔 > 水产渔业

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

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