C++实验二类与对象附答案.docx

上传人:b****8 文档编号:9811827 上传时间:2023-02-06 格式:DOCX 页数:33 大小:72.73KB
下载 相关 举报
C++实验二类与对象附答案.docx_第1页
第1页 / 共33页
C++实验二类与对象附答案.docx_第2页
第2页 / 共33页
C++实验二类与对象附答案.docx_第3页
第3页 / 共33页
C++实验二类与对象附答案.docx_第4页
第4页 / 共33页
C++实验二类与对象附答案.docx_第5页
第5页 / 共33页
点击查看更多>>
下载资源
资源描述

C++实验二类与对象附答案.docx

《C++实验二类与对象附答案.docx》由会员分享,可在线阅读,更多相关《C++实验二类与对象附答案.docx(33页珍藏版)》请在冰豆网上搜索。

C++实验二类与对象附答案.docx

C++实验二类与对象附答案

实验二类与对象

实验目的和要求

1.掌握类、类的数据成员、类的成员函数的定义方式。

2.理解类成员的访问控制方式。

3.掌握对象的定义和操作对象的方法。

4.理解构造函数和析构函数的定义与执行过程。

5.掌握重载构造函数的方法。

6.了解拷贝构造函数的定义方法。

实验内容

1.下面程序中有错,在不删除和增加代码行的情况下,改正错误语句,使其正确运行。

#include

classAa

{

public:

Aa(inti=0)

{

a=i;

cout<<"Constructor"<

}

~Aa()

{

cout<<"Destructor"<

}

voidprint()

{

cout<

}

private:

inta;

};

intmain()

{

Aaal

(1),a2

(2);

al.print();

cout<

return0;

}

2.检查下面的程序,找出其中的错误,并改正。

然后上机调试,使程序能正常运行。

(1)

#include

classDate

{

voidset_date();

voidshow_date();

intyear;

intmonth;

intday;

};

Dated;

intmain()

{

set_date();show_date();

}

voidset_date()

{cin>>d.year;cin>>d.month;cin>>d.day;

}

voidshow_date()

{cout<

}

(2)

#include

classA

{

public:

voidA(inti=0)

{

m=i;

}

voidshow()

{cout<

}

void~A(){}

private:

intm;

};

intmain()

Aa(5);a.m+=10;a.show();return0;

}

(3)#includeclassX

{private:

inta=0;int&b;voidsetA(inti){

a=i;

}

X(inti)

{

a=i;

}public:

intX()

{

a=b=0;

}

X(inti,intj)

{

a=i;

b=j;

}

voidsetC(intk)

{c=c+k;

}

};

voidmain()

{

Xx1;

Xx2

(2);

Xx3(1,2);x1.setA(3);

}

3.调试下列程序。

#include

classTPoint

{private:

intX,Y;

public:

TPoint(intx,inty)

{

X=x;

Y=y;

cout<<"Constructoriscalled"<

}

TPoint(TPoint&p);

~TPoint()

{cout<<"Destructoriscalled"<

intgetx()

{returnX;}

intgety()

{returnY;}

};

TPoint:

:

TPoint(TPoint&p)

{

X=p.X;

Y=p.Y;cout<<"Copy-initializationConstructoriscalled"<

}

voidmain()

{

TPointp1(4,9);

TPointp2(p1);

TPointp3=p2;cout<<"p3=("<

}

(1)写出程序的输出结果,并解释输出结果。

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

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

TPointp4,p5

(2);

调试程序会出现什么现象?

为什么?

如何解决?

(提示:

对已有的构造函数进行适当修改)结合运行结果分析如何使用不同的构造函数创建不同的对象。

(3)在主函数内使用new创建不同参数动态两个动态对象,输出其坐标,并用delete删除之。

运行程序,分析运行结果。

(4)在程序中定义个全局对象,在主函数最前添加语句:

cout<<"Entermain"<

运行程序,分析运行结果。

4•完善程序,并写出运行结果

根据程序要求,完善程序后输入源程序,编译连接,并写出运行结果。

如果某个自然数除了1和它本身外还有其他因子,则这个自然数就是合数(非素数)。

试定义一个类NUM,从3开始向上试探找出n个连续的自然数,且它们都是合数。

当找到第一组连续的n个合数后,即停止查找。

具体要求如下:

(1)私有成员

intn:

存放满足条件的连续自然数的个数。

int*p:

根据n的值申请一个动态数组用来存放求出的满足条件的n个自然数。

(2)公有成员函数

NUM(intn1):

构造函数,用n1初始化n,根据n的值申请动态数组空间,使p指向该

动态数组空间。

intyes(intx):

判断x是否为合数。

如果是,返回1,否则返回0。

voidfun():

从3开始向上试探找出n个连续的自然数,且它们都是合数,并依次存入动

态数组。

voidprint():

输出满足条件的n个合数。

~NUM():

析构函数,释放动态数组所占用的存储空间。

(3)在主函数中定义一个NUM类的对象num,求出10个连续的合数。

然后通过对象num调用成员函数求出10个连续的合数,并输出计算结果。

#include

classNUM

{〃定义数据类NUM

private:

intn;int*p;

public:

NUM(intn1)

{

n=n1;

p=;//动态分配存放连续合数的存储空间

}

intyes(intx)

{

for(inti=2;i<=x/2;i++)if(x%i==0)

return1;

return0;

}

voidfun(void)

{

intj;

for(inti=3;1;i++)

{

j=0;

while(&&j

{

P[j]=i;

j++;

i++;

}

if(j==n)

break;

}

}

voidprint(void)

{

cout<<"找到的"<

"<

for(inti=0;i

cout<

cout<

}

〜NUM()

{

;//释放在构造函数中申请的存储空间

}

};

voidmain()

{

cout<<"请输入要求的连续合数的个数(例如10):

";

intn;

cin»n;

NUMnum(n);

;〃寻找连续的合数,并存入动态申请的存储空间中;〃输出寻找到的合数

}

5.请定义一个矩形类(Rectangle),私有数据成员为矩形的长度(len)和宽度(wid),缺省构造函数置len和wid为0,有参构造函数置len和wid为对应形参的值,另外还包括求矩形周

长、求矩形面积、取矩形长度和宽度、修改矩形长度和宽度为对应形参的值、输出矩形尺寸等公有成员函数。

要求输出矩形尺寸的格式为“length:

长度,width:

宽度”。

编写主函数

对定义的类进行测试。

6.声明一个时间类,时间类中有3个私有数据成员(Hour,Minute,Second)和两个公有成

员函数(SetTime和PrintTime)。

SetTime根据传递的3个参数为对象设置时间;PrintTime负

责将对象表示的时间显示输出,输出格式为“Hour:

Minute:

Second”。

(1)在主函数中,建立一个时间类的对象,设置时间为9点20分30秒并显示该时间。

(2)使用构造函数代替上面的SetTime成员函数,并在主函数中使用构造函数设置时间为10点40分50秒,并显示该时间。

(3)重载时间类的构造函数(不带参数)使小时、分、秒均为0。

(4)在时间类的析构函数中输出"Goodbye!

7.下面是一个整型链表类intList的声明,请给出该类所有数据成员的类外定义,并在主函

数中测试该类。

classintList

{

protected:

structNode

{

intdata;

Node*next;

};

Node*L;

public:

intList();//构造函数

~intList();//析构函数

boolInsert(inti,intelem);

boolRemove(inti,int&elem);

〃删除链表的第i个位置的元素,删除成功返回true,失败返回false

intFind(intelem);

〃在链表中查找值为elem的元素,找到返回该元素在链表中的位置,否则返回0

intLength();//返回链表长度(元素个数)

voidPrintList();//输出链表};

参考答案(非权威,仅仅是我自己的理解,如有错误,欢迎批评指正!

第一题:

#include

classAa

{

public:

Aa(inti=0)

{

a=i;

cout<<"Constructor"<

~Aa()

{

cout<<"Destructor"<

}

voidprint()

{

cout<

}

private:

};

inta;

intmain(){

Aaal

(1),a2

(2);al.print();a2.print();return0;

}

ConstructoF1

Constructor2

1

2

Destructor2

[Destractor1(Pi*essanjykeytocantInue

第二题

(1)

#includeclassDate

{

public:

voidset_date();voidshow_date();

private:

};

intyear;intmonth;

intday;

Dated;intmain(){

d.set_date();d.show_date();return0;

voidDate:

:

set_date()

{_

cin>>year;cin»month;cin>>day;

}voidDate:

:

show_date()

{_

coutvvyearvv'/'vvmonth<<'/'<

}

(2)

#include

classA

{

public:

A(inti=0){

}

m=i;

voidshow()

{

coutvvmvvendl;

}

~A(){}

friendvoidadd(A&);

private:

intm;

};

voidadd(A&a)

{

a.m+=10;

}intmain(){

Aa(5);add(a);a.show();return0;

}

(3)

#include

classX

{

private:

inta,b,c;

public:

};

X(inti)

{

a=i;

}

X()

{

a=b=0;

}

X(inti,intj)

{

a=i;

b=j;

}

voidsetC(intk)

{

c=c+k;

}

voidsetA(inti)

{

a=i;

}

voidmain()

{

Xx1;

Xx2

(2);

Xx3(1,2);

x1.setA(3);

第三题

voidmain()

{

TPointp1(4,9);〃调用构造函数初始化pl

TPointp2(p1);//显示调用拷贝构造函数初始化p2

TPointp3=p2;〃对象之间的赋值,由于之前没有定义p3,因此用另一个同

类的对象给其赋值时,会调用拷贝构造函数。

coutvv"p3=("vvp3.getx()vv","vvp3.gety()vv")"v

}

■'[>\C+-\^^ZZ\Debdg\2.exe"

Constructoi*called

Copy-initializati

BJestructopiscalled

Destructoriscalled

DesCvwctoFiscalled

Pressanykeytocontinue

答:

调试程序会程序错误,原因是对于无输入的对象和只要一个输入值的对象没有相应的构造函数,要解决也挺简单,就是在说明部分加上相应的构造函数就可以了。

修改后的程序如下:

#includeclassTPoint

{private:

intX,Y;public:

TPoint()

{

X=Y=0;cout<<"AConstructoriscalled"<

}

TPoint(inti)

{

X=i;

Y=0;cout<<"BConstructoriscalled"<

}

TPoint(intx,inty)

{

X=x;

Y=y;

cout<<"Constructoriscalled"<

}

TPoint(TPoint&p);

~TPoint()

{cout<<"Destructoriscalled"<

{returnX;}

intgety()

{returnY;}

};

TPoint:

:

TPoint(TPoint&p)

{

X=p.X;

Y=p.Y;cout<<"Copy-initializationConstructoriscalled"<

}

voidmain()

{

TPointp1(4,9);cout<<"p1=("<

TPointp2(p1);cout<<"p2=("<

TPointp3=p2;

coutvv"p3=("vvp3.getx()vv","vvp3.gety()vv")"v

(2);

coutvv"p4=("vvp4.getx()vv","vvp4.gety()vv")"v

coutvv"p5=("vvp5.getx()vv","vvp5.gety()vv")"v

}

直3'D:

\C-b-肢验二、g\2.exe"I1=1丨回

Constpuctoriscalledpl=<4,9>

Copy-initializationConstructopiscalledp2=<4,9>

Copy-initializationConstructopiscalledp3=<4,9>

AConsti'Lictopiscalled

BConstPLictoi*iscalledp4=<0,0>

t)5=C2,B>

Destruetoriscalled

Destructoriscalled

Destruetoriscalled

Destructoriscalled

Destruetoriscalled

PressanykeytocontinLie

#include

classTPoint

{

private:

intX,Y;

public:

TPoint(intx,inty)

{

X=x;

Y=y;

cout«"Constructoriscalled"<

}

TPoint(TPoint&p);

~TPoint()

{

cout<<"Destructoriscalled"<

}

intgetx()

{returnX;}

intgety()

{returnY;}

};

TPoint:

:

TPoint(TPoint&p)

{

X=p.X;

Y=p.Y;cout«"Copy-initializationConstructoriscalled"«endl;

}voidmain()

{

TPoint*p仁newTPoint(4,9);coutvv"p1=("vvp1->getx()vv","vvp1->gety()vv")"v

#includeclassTPoint

{

private:

intX,Y;

public:

TPoint(intx,inty)

{

X=x;

Y=y;

cout<<"Constructoriscalled"<

}

TPoint(TPoint&p);

~TPoint()

{

cout<<"Destructoriscalled"<

}

intgetx(){returnX;}

intgety(){returnY;}

voidShowPoint()

{

cout<<"p1=("<

}

};

TPoint:

:

TPoint(TPoint&p)

{

X=p.X;

Y=p.Y;

cout<<"Copy-initializationConstructoriscalled"<

}

voidmain()

{

cout<<"Entermain"<

TPoint*p1=newTPoint(4,9);

p1->ShowPoint();

deletep1;

S]'O\C++\^i^ZZ\Debjg\2.exe"〔三1旦僵工盘・

Enternain

ConstructorIscalled

01-<4.9>

DestmctopiscalledPressanykeytocontinue

第四题:

#includeclassNUM

{〃定义数据类NUM

private:

intn;int*p;

public:

NUM(intn1)

{

n=n1;

p=newint[n];〃动态分配存放连续合数的存储

空间

}

intyes(intx)

{

for(inti=2;i<=x/2;i++)

if(x%i==0)return1;

return0;

}

voidfun(void)

{

intj;

for(inti=3;1;i++)

j=0;

while(yes(i)&&j

{

p[j]=i;

j++;

i++;

}

if(j==n)break;

voidprint(void)

{

coutvv"找到的"<<*<"个连续合数为:

"<

for(inti=0;i

cout<

cout<

}

~NUM()

{

if(p)delete[]p;//释放在构造函数中申请的存储空

}

};

voidmain()

{

coutvv"请输入要求的连续合数的个数(例如10):

";

intn;

cin>>n;

NUMnum(n);

num.fun();//寻找连续的合数,并存入动态申请的存储空间中

num.print();//输出寻找到的合数

籍鑑瑋鬆勰的个数*例如価片花

114115116117118119120121122123

PressanykeytocontinLie

第五题:

#include

classRectangle

{

private:

intlen,wid;

public:

Rectangle()

{

len=wid=0;

cout<<"FirstConstructoriscalled"<

}

Rectangle(inti,intw)

{

len=i;

wid=w;

cout«"SecondConstructoriscalled"<

voidSet_Rectangle()

{

cin>>len;cin>>wid;

~Rectangle()

{cout<<"Destructoriscalled"<

};

voidmain()

{

inttemp;

Rectanglerect,Default(8,9);

cout<

cout<<"默认长度:

"<

"<

cout<<"矩形的周长为:

"<

cout<<"矩形的面积为:

"<

coutvv"是否需要修改参数(1:

修改0:

退出):

";

cin>>temp;

while(temp==1)

coutvv"请输入矩形的长和宽:

"<

coutvv"长度:

"vvr

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

当前位置:首页 > 求职职场 > 简历

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

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