2025年面向对象编程实践指南核心概念与代码实现解析_第1页
2025年面向对象编程实践指南核心概念与代码实现解析_第2页
2025年面向对象编程实践指南核心概念与代码实现解析_第3页
2025年面向对象编程实践指南核心概念与代码实现解析_第4页
2025年面向对象编程实践指南核心概念与代码实现解析_第5页
已阅读5页,还剩24页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

面向对象程序设计试验汇报

学生所在学院:信息科学与工程学院

学生所在班级:

学生姓名:

指导教师:

试验—C++基础

1.1试验目的

1.理解并熟悉开发环境,学会调试程序:

2.熟悉C++中简朴的原则输入输出函数的使用措施;

3.理解const修饰符的作用并学会应用:

4.理解内联函数的优缺陷并学会其使用场所:

5.理解并学会函数重载:

6.理解并纯熟掌握使用new和delele来分派内存:

7.理解并纯熟掌握引用的使用措施。

1.2试验内容

1.2.1程序阅读

1.理解下面的程序并运行,然后回答问题。

#include<iostream.h>

in(max_dcf(intx,inty)

(

return(x>y?x:y);

)

intmax_def(inty,intz)

(

inttemp=0;

retum(temp=(x>y?x:y))>z?temp:z;

)

doubleniax_def(doublex,doubley)

(

retum(x>y?x:y);

)

intmain()

(

intxl=O;

intx2=0;

doubledi=0.0;

doubled2=0.0;

xl=max_def(5,6);

x2=max_def(2,3,4);

d1=max_dcf(2.1,5.6);

d2=max_def(123,3.4,7.8);----------------------------------------------①

cout«"xl=*'«xl«cndl;

cout<<"x2="<<x2<<endl;

cout«"d1="«dl«cndl;

cout«"d2=,,«d2«endl;................................................②

return1;

问题一:上述程序的输出成果是什么?

答:输出成果为

回’F:\AJI的文件夹\zO\Debug\Cppl.exe'

K1=6

x2=4

dl-5.6

42=12

Pressanykeytocontinue

问题二:①处调用的是哪个函数?

答:①处调用的是doublemax_def(doublexdoubley)函数

问题三:②处的输出成果为何是d2=12,而不是d2=12.3?

答:由于调用的intmax_def(inty,intz)函数返回值为整型。

2.理解下面的程序并运行,然后回答问题。

#include<iostream.h>

intmain()

int*pl=newint:----------------------------------------------(T)

int*p2=newint(O);----------------------------------------------②

char*p3=newchar[10];--------------------------------------------③

return1;

问题一:①、②、③处动态申请内存分别代表什么意思?

答:①处:定义一种整型指针动态分派一种整型空间将首地址赋给pl;

②处:定义一种整型指针动态分派一种整型空间,并赋值为0:

③处:定义一种字符型指针动态分派具有10个数组元素的字符数组空间,将首地址赋

给p3。

问题二:该程序存在什么不合理的地方?

答:程序结束时没有释放分派的存储空间。

3.理解下面的程序并运行,然后回答问题。

#include<iostream.h>

voidsa,intb)

inttemp=a;

a=b;

b=temp;

)

voids*a,int*b)

{

inttcinp=*a;

*b=tcmp;

intmain()

inti=5;

in(j=10;

cout«"Befores"«i«".j="«j«endl;

s);

cout«"Afterthefirsts"«i«",j=',«j«endl;

s);

cout«"Aftertheseconds',«i«",j=,"«j«endl;

return1;

)

问题一:输出成果是什么?

问题二:①处函数调用不能实现两个数的互换,而②可以,原因是什么?

答:①处调用的函数形参为整型变量,是值传递,形参的变化不影响实参:

②处调用的函数形参为指针,是址传递,形参的变化会导致实参的变化。

问题三:②处调用的是哪个函数?

答:调用的函数是voids*a,int*b)«

试验二类和对象一一类的构建

2.1试验目的

1•类的定义;

2.类对象的使用:

3.类组员变量的定义和使用;

4.类组员函数的定义和使用:

5.理解类的作用域:

6.理解类的申明;

7.理解类中组员的访问属性:

2.2试验内容

2.2.1程序阅读

1.理解下面的程序并运行,然后回答背面的问题。

#include<iostream.h>

classCDaie

(

public:

voidSet(intdjnty)

(

month=ni;

day=d;

year=y;

)

intIsLeapYear()

(

return(year%4==0&&year%100!=0)||(year%400==0);

)

voidPrint()

(

cout«month«"/"«day«"/"«ycar«cndl;

)

private:

intmonth;

intday;

intyear;

voidmain()

CDatea;

a.Set(10,15,);

a.Print();

)

问题一:以上程序为何不能通过编译?怎样改正?

答:由于Se【函数被设为私有main函数中无法调用,应在set函数前加public。

问题二:类中的组员函数和组员变量分别属于哪种访问权限?

答:数据组员为私有,Print为公有:sei原为私有,改正后为公有。

问题三:①处语句的意思是什么?

答:将类定义一种对象a。

2.理解下面的程序并运行,然后回答问题。

.#include<iostream.h>

ininionih:〃全局变量

intday;

intyear;

voidSet(intm,intd,inty)

(

::month=iTi;

::day=d;

::year=y;

}

classCDate

(

public:

voidSet(intni,inld,inty)

(

::Set(rn,d,y);

)

voidPrint()

(

cout«inonth«7"«day«'7"«ycar«cndl;

)

private:

intmonth;

intday;

intyear;

voidmain()

CDaiea;

a.Sct(10,15,);

a.Print();

问题一:①处是在给组员变量赋值还是全局变量赋值,假如去掉域作用符::,程序与否还能

对的运行?

答:全局变量,不能。

问题二:②处调用的哪个函数,假如去掉域作用符::,程序与否还能对的运行?

答:调用全局set函数;不能。

问题三:程序的输出成果是?

答:

试验三类和对象一一构造函数与析构函数

3.1试验目的

1.理解this指针的作用和使用方法;

2.掌握构造函数的定义和作用;

3.掌握构造函数的使用:

4.掌握拷贝构造函数的定义和使用:

5.掌握构造函数的重载:

6.掌握析构函数的定义和使用c

3.2试验内容

3.2.1程序阅读

I.理解下面的程序并运行,然后回答问题。

#include<iostream.h>

classCPoint

public:

voidSet(inlx,inty);

voidPrint();

private:

intx;

inty;

);

voidCPoint::Sct(intx,inty)

(

x=x;

y=y;

}

voidCPoinc:Prin(()

(

cout«"x="«x«",y="«y«endl;

)

voidniain()

(

CPointpt;

pt.Set(10,20);

pt.PrintO:

)

问题一:以上程序编译能通过吗?假如不能,原因是什么?

答:能通过编译。

问题二:以上程序的运行构造与否对的,假如不对的,分析为何,怎样改正?

答:运行构造不对的,由于Sei函数中,x=x,参数x覆盖数据组员,使得数据组员并没有

被赋值。

2.理解下面的程序并运行,然后回答背面的问题。

includc<iostrcam.h>

classCPcrson

(

public:

voidPrint();

private:

CPerson();

private:

intage;

char*name;

);

CPerson::CPerson()

(

)

voidCPcrson::Print()

(

cout«"naine="«nainc«",agc="«agc«cndl;

)

voidmain()

(

CPcrsonps(23,“张三”);

ps.Prin(();

)

问题一:以上程序存在三个错误,在不变化主函数内容的前提下,试改正该程序。

答:第一处错误是:在程序头应添加#访9讪0<曳由唔.11>文献,第二处错误是:构造函数没有

参数,应添加为(inti,charj),第三处错误是:构造函数没有函数体。改正后的程序为:

#include<iostream.h>

#include<string.h>

classCPerson

(

public:

voidPrint();

CPerson(inli,char*j);

public:

intage;

char*name;

);

CPcrson::CPcrson(inti.char*j)

agc=i;

name寸

voidCPerson::Pnnt()

cout«"name="«name«",age="«age«endl;

)

voidmain()

(

CPersonps(23,“张三”);

ps.Print();

试验四类和对象一对象传递与静态组员

4.1试验目的

1.静态组员(静态数据组员、静态组员函数)的作用与使用时的注意事项

2.学友元(友元函数、友元类)的作用和使用;

3.理解常类型。

4.2试验内容

4.2.1程序阅读

I.理解下面的程序并运行,然后回答背面的问题。

#include<iostream.h>

#include<string.h>

classCStudent

(

public:

CStudent(char*n,inta);

~CStudent();

staticvoidSetAge(inlage);

private:

char*naine;

intage;

staticintnTotalObj;

};

intCStudent::nTotalObj=0;

CStudent::CStudent(char*n,inta)

:age(a)

ininLcn=slrIen(n);

name=newchar[nLen+1];

strcpy(name,n);

name[nLen]=>\O';

nTotalObj++;

)

CStudcnt::~CStudcnt()

(

dclctcIJname;

nTotalObj-;

)

voidCStudent::SetAge(in(age)

(

ihis-^age=age:

)

voidmain()

(

CStudentstu1(“张三",25);

CStudentstr2("李四",26);

cout«"CStudent::nTotalO?j="«CStudent::nTotalObj«endl;

)

问题一:以上程序编译能通过吗,为何?

答:不能,CStudent::nTotalObj为私有变量,类引不能访问。

问题二:组员变量nTotalObj起什么作用,它是怎样实现的?

答:记录该类所创立对象的个数,通过申明为静态。

问题三:假如不改编主函数和类Cstudent中的组员变量的属性,应当怎样改正该程序?

答:可以将staticintnTotalObj申明为公有。

2.理解下面的程序并运行,然后回答背面的问题。

#include<iostream.h>

#include<string.h>

classCStudcnt

(

public:

CStudcnt(char*n,inta);

-CStudentO;

private:

char*name;

intage;

};

CSiudent::CStudent(char*n,inta)

:age(a)

(

intnLen=strlen(n);

namc=ncwchar[nLcn+1];

strcpy(namc,n);

namc[nLcn)-\O';

}

CStudcnt::-CS(udcnt()

(

delete||name;

}

classCTeacher

(

public:

CTeaclier(char*tn,intta);

〜CTeacher。;

voidSetStuAge(inta);

private:

char*name:

intage;

CSludents(u;

);

CTeacher::CTeacher(char*tn,intta)

:age(ta)

(

intnLen=strlen(tn);

natnc=ncwchar[nLcn+1];

strcpy(namc.tn);

namc[nLcnJ='\O';

CTcachcr::«CTcachcr()

delete[]name;

voidCTeacher::SetStuAge(inta)

(

stu.age=a;

)

voidniain()

(

CStudentslul(“张三”,25);

CStudentstr2("李四北26);

)

问题一:以上程序有两个错误,指出并改正。

答:第一处错误是Cstudcntage为私有,Ctcachcr无法访问。

第二处错误是Ctcachcr中Cstudcnt未初始化。

改正:®public:intage@Cteacher::Cteacher(char5ettn,intta)::age(ta),stu(tn,ta)

3.理解下面的程序并运行,然后回答背面的问题。

#include<iostream.h>

classDate

(

private:

constintyear;

constintmonth;

constintday;

public:

Date(inty,intm,intd);

voidshowdate();

);

Date::Date(inty,intmjntcl)

(

year=y;

rnonth=m;

day=d;

)

voidDatc::showdatc()

cout«year«'7"«nionth«'7"<<day«cndl;

voidmain()

constDaleobj(,10,30);

obj.showdate();

)

问题一:以上程序有两个错误,试指出来,并改正之?

答:第一处错误是:构造函数应当用组员初始化列表对常数据组员初始化:第二处错误是:

没有用常组员函数访问常对象数据组员。改正后的程序为:

#includc<iostrcam>

usingnamespacestd;

classDate

(

private:

constintyear;

constintmonth:

constin(day;

public:

Date(inty,intm,intd);

voidshowdate()const;

);

Date::Date(inty,intm,intd):year(y),month(m),day(d)

voidDate::showdate()const

(

cout«year«'7"«month«,7',«day«endl;

)

voidmain()

(

Dateobj(,10,30);

obj.showdate();

)

试验五派生与继承一单基派生

5.1试验目的

1.理解继承的概念:

2.理解共有派生、私有派生和保护派生:

3.理解单基派生类中构造函数和析构函数的执行次序。

5.2试验内容

5.2.1程序阅读

1.理解下面的程序并运行,然后回答背面的问题。

#include<iostream.h>

classCBasc

(

public:

CBasc(inta)

:a(a)

(

)

protected:

voidprint()

(

cout<<"a=',«a«endi;

)

private:

inta;

);

classCDerive:publicCBase

(

public:

voidpiint()

(

CBase::print();

cout«"b="«b«endl;

)

private:

intb;

)

voidmain()

(

CDcrivcd;

d.print();

CBaseb;

b.print();

)

问题一:以上程序有两个错误,试指出来,并改正之。

答:类CBase中的组员数据应当为公有访问属性,第二个错误是构造函数有问题。改正后

的程序有两种:

#inckidc<iostrcam>

usingnamespacestd;

classCBase

(

public:

CBasc(inta):a(a)

(

}

Voidprint()

(

cout<<"a=',«a«endi;

)

public:

inta;

);

classCDerive:publicCBase

(

public:

CDerive(inta,inih):CBase(a),b(b)

(

)

voidprint()

(

CBase::prinl();

cout«"b="«b«endl;

)

private:

intb;

};

voidmain()

CDerived(6,4);

d.print();

CBaseb(5);

b.print();

#includc<iostrcain>

usingnamespacestd;

classCBase

(

public:

voidprint()

cout<<"a="«a«endi;

public:

inta;

);

classCDerive:publicCBase

(

public:

voidprinl()

CBase::print();

cout«"b="«b«endl;

private:

intb;

);

voidmain()

CDerived;

d.print();

CBaseb;

b.prinl();

)

2.理解下面的程序并运行,然后回答背而的问题。

#inckidc"iostrcam.h"

classCBase

(

public:

CBasc(inta)

:a(a)

(

cout«"basestructure"«endl;

)

-CBase()

(

cout«"basedestructure"«endl;

)

voidprint()

(

cout«"a="«a«endi;

)

protected:

inta;

);

classCDerive:publicCBase

(

public:

CDerive(intajntbjntc)

:CBase(a),b(b),c(c)

(

cout«"dcrviccstrucuirc"«cndl;

)

〜CDcrive。

cout«"derivedestruc(ure"«endl;

voidprint()

CBase::print();

cout«"b.a="«b.a<<endl;

cout«"c="«c«endl;

)

private:

CBascb:

intc;

};

voidmain()

(

CDerived(l,2,3);

d.printO;

}

问题一:以上程序的输出成果是什么,为何?

答:输出成果是

ranC:\Windows\system32\cmd.exe

basestructure

basestructure

deruicestructure

a=l

b.a=2

c=3

deriuedestructure

basedestructure

basedestructure

请按任意键继续..・

原因是基类和派生类中构造函数和析构函数的执行次序。

问题二:①处语句执行完后,d.b.a的值为多少?

答:值为1。

试验六派生与继承一多基派生

6J试验目的

1.理解多基派生的定义;

2.基派生中的构造函数与析检函数的调用次序;

3.理解多基派生中虚基类的作用。

6.2试验内容

6.2.1程序阅读

1.理解下面的程序并运行,然后回答背面的问题。

classCBasel

{

public:

CBasel(inta)

:a(a)

(

cout«"base1structure...M«endl;

}

~CBasel()

(

cout«"baseldestructure..."«endl:

)

voidprinl()

(

cout<<"a="«a«endl;

)

protected:

inta;

);

classCBasc2

(

public:

CBasc2(intb)

:b(b)

cout«"base2structure..."«endl;

~CBase2()

(

cout«"base2destructure..."«endl;

)

voidprint()

(

cout<<"b="«b«cndl;

)

protected:

intb;

);

classCDerive:publicCBasel.publicCBase2

(

public:

CDeriveO

(

cout«"derivestructure..."«endl;

}

~CDerive()

(

cout«"derivedestructure..."«endl:

)

voidprinl()

(

CBasel::print();

CBase2::print();

bl.print();

b2.print();

cout«"c="«c«endl;

)

private:

CBaselbl;

CBasc2b2;

intc;

voidmain()

CDerived;

d.print();

)

问题一:改正以上程序中的错误,并分析输出成果。

答:CBasel与CBase2没有合适的构造函数,改正措施在CBasel,CBase2设置缺省值为0

改正:将CDerive构造函数改为:CDerive(inta,intbjntc):

CBascl(a),CBasc2(b).bl(a),b2(b),c(c)

(

cout«"dcrivcstructure..."«cndl;

)

主函数中CDerived;改为CDerived(1,2,3);

输出成果是:

试验七多态性一函数与运算符的重载

7.1试验目的

1.理解静态联编和动态联编的概念;

2.掌握组员函数方式运算符重载;

3.掌握友元函数方式运算符重载;

4.掌握++、-、=运算符的重载。

7.2试验内容

1.理解下面的程序并运行,然后回答背面的问题。

#include'riostream.h"

ciassCComplcx

(

public:

CComplexO

(

real=O:

imag=0;

)

CComplex(intx,inty)

(

real=x;

•mag-y;

)

intreal;

iniimag;

CComplexoperator+(CComplexobj1)

(

CComplexobj2(real+0电1.real.imag+obj1.imag);

returnobj2;

)

);

voidmain()

(

CComplexobj1(100.30);

CComplcxobj2(20,30);

CComplexobj;

obj=objl+obj2;

cout«obj.real«endl;

cout«obj.imag«endI;

问题一:①处的运算符重载,为何该函数的返回值要设计成Ccomplex类型?

答:①处运算符重载,要返回两个值real和imag,因此函数返回值类型设计为CComplex

类型。

问题二:②处的运算符重载函数调用就相称于"obj=operaior+(objl,obj2):”,请问CComplex

类中的运算符重载函数为何只有一种参数?

答:由于调用重载运算符的对象自身相称于一种参数。

课后习题

第二章

一试验目的

1.掌握引用的使用。

2.掌握调用函数的措施。

二.试验内容

2.19写出下列程序的运行成果。

#include<iostream>

usingnamespacestd;

voidf(int&m,intn)

(

inttemp:

temp=m;

m=n;

n=temp;

)

Intmain()

(

inta=5,b=10;

f(a,b)

cout<<a«,M,«b<<cndl;

return0;

三.试验成果

四.心得体会

1.引用可以作为函数形参,和指针变量作为函数参数效果同样,不过引用作为函数参数更清

晰。

2.

第三章

一.试验目的

1.掌握静态数据组员的使用。

2.掌握静态组员函数的使用。

二.试验内容

3.26写出下列程序的运行成果。

#include<iostream>

usingnamespacestd;

ClassM{

intA;

staticintB;

public:

M(inta)

(

A=a;

B+=a;

cout〈v"Construcling\n”<vendl;

)

Staticvoidfl(Mm);

~M()

{cout«''Dcstructing\n”《cndl;

)

);

VoidM::fl(Mm)

Cout«,,A=,,«m.A«cndl;

Cout<<"B=''<<b<<endl;

intM::B=O;

intmain()

{

MP(5),Q(10);

M::fl(P);

M::fl(Q);

Return0;

)

三.试验成果

'.■

密C:\Windows\system32\cmdexe

Constructing

Construct

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论