面向对象程序的设计实验.docx

上传人:b****2 文档编号:13979134 上传时间:2023-06-19 格式:DOCX 页数:33 大小:45.61KB
下载 相关 举报
面向对象程序的设计实验.docx_第1页
第1页 / 共33页
面向对象程序的设计实验.docx_第2页
第2页 / 共33页
面向对象程序的设计实验.docx_第3页
第3页 / 共33页
面向对象程序的设计实验.docx_第4页
第4页 / 共33页
面向对象程序的设计实验.docx_第5页
第5页 / 共33页
面向对象程序的设计实验.docx_第6页
第6页 / 共33页
面向对象程序的设计实验.docx_第7页
第7页 / 共33页
面向对象程序的设计实验.docx_第8页
第8页 / 共33页
面向对象程序的设计实验.docx_第9页
第9页 / 共33页
面向对象程序的设计实验.docx_第10页
第10页 / 共33页
面向对象程序的设计实验.docx_第11页
第11页 / 共33页
面向对象程序的设计实验.docx_第12页
第12页 / 共33页
面向对象程序的设计实验.docx_第13页
第13页 / 共33页
面向对象程序的设计实验.docx_第14页
第14页 / 共33页
面向对象程序的设计实验.docx_第15页
第15页 / 共33页
面向对象程序的设计实验.docx_第16页
第16页 / 共33页
面向对象程序的设计实验.docx_第17页
第17页 / 共33页
面向对象程序的设计实验.docx_第18页
第18页 / 共33页
面向对象程序的设计实验.docx_第19页
第19页 / 共33页
面向对象程序的设计实验.docx_第20页
第20页 / 共33页
亲,该文档总共33页,到这儿已超出免费预览范围,如果喜欢就下载吧!
下载资源
资源描述

面向对象程序的设计实验.docx

《面向对象程序的设计实验.docx》由会员分享,可在线阅读,更多相关《面向对象程序的设计实验.docx(33页珍藏版)》请在冰点文库上搜索。

面向对象程序的设计实验.docx

面向对象程序的设计实验

实验一C++基础

1.1实验目的

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

2.熟悉C++中简单的标准输入输出函数的使用方法;

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

4.理解内联函数的优缺点并学会其使用场合;

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

6.理解并熟练掌握使用new和delete来分配内存;

7.理解并熟练掌握引用的使用方法。

1.2实验内容

1.2.1程序阅读

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

#include

intmax_def(intx,inty)

{

return(x>y?

x:

y);

}

intmax_def(intx,inty,intz)

{

inttemp=0;

return(temp=(x>y?

x:

y))>z?

temp:

z;

}

doublemax_def(doublex,doubley)

{

return(x>y?

x:

y);

}

intmain()

{

intx1=0;

intx2=0;

doubled1=0.0;

doubled2=0.0;

x1=max_def(5,6);

x2=max_def(2,3,4);

d1=max_def(2.1,5.6);

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

cout<<"x1="<

cout<<"x2="<

cout<<"d1="<

cout<<"d2="<

return1;

}

问题一:

上述程序的输出结果是什么?

问题二:

用的是哪个函数?

答:

调用的函数是

intmax_def(intx,inty,intz)

{

inttemp=0;

return(temp=(x>y?

x:

y))>z?

temp:

z;

}

问题三:

②处的输出结果为什么是d2=12,而不是d2=12.3?

答:

因为①处调用的是整型函数,d2在此函数中被转换为整型,小数点后面被删除。

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

#include

intmain()

{

int*p1=newint;-----------------------------------------------------①

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

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

return1;

}

问题一:

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

答:

①new动态分配存放一个整数的内存空间,并将其首地址赋给指针变量p1;②new动态分配存放一个整数的内存空间,并对其初始化赋值为0,并将其首地址赋给指针变量p2;③new动态分配存放10个字符型数组元素的内存空间,并将其首地址赋给指针变量p3。

问题二:

该程序存在什么不合理的地方?

答:

程序结束时没有将分配的空间释放,应该使用delete函数释放内存。

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

#include

voidswap(inta,intb)

{

inttemp=a;

a=b;

b=temp;

}

voidswap(int*a,int*b)

{

inttemp=*a;

*a=*b;

*b=temp;

}

intmain()

{

inti=5;

intj=10;

cout<<"Beforeswap:

i="<

swap(i,j);-----------------------------------------------------①

cout<<"Afterthefirstswap:

i="<

swap(&i,&j);-----------------------------------------------------②

cout<<"Afterthesecondswap:

i="<

return1;

}

问题一:

输出结果是什么?

问题二:

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

答:

①处调用的函数只是交换了局部变量a和b,并没有改变i和j的值;②处调用的函数使用了引用形参,i和j的值随着此处调用的函数中a和b的对换而对换。

问题三:

②处调用的是哪个函数?

答:

调用的函数是

voidswap(inta,intb)

{

inttemp=a;

a=b;

b=temp;

}

1.2.2程序设计

1.定义两个重名函数,分别求出两点间平面距离和空间距离。

#include

#include

usingnamespacestd;

intdistance(intx1,inty1,intx2,inty2)

{

doubledis;

dis=sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));

cout<

returndis;

}

intdistance(intx1,inty1,intx2,inty2,intz1,intz2)

{

doubledis;

dis=sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)+(z1-z2)*(z1-z2));

cout<

returndis;

}

voidmain()

{

inta;

inti,j,k,l,q,w,e,r,t,y;

cout<<"请输入平面两点坐标:

"<

cin>>i>>j>>k>>l;

a=distance(i,j,k,l);

cout<<"请输入空间两点坐标"<

cin>>q>>w>>e>>r>>t>>y;

a=distance(q,w,e,r,t,y);

}

2.设计一个函数:

exch(),当调用exch(a,b,c)时,将a赋值给b,b赋值给c,c赋值给a,要求采用引用的方式来实现。

#include

#include

usingnamespacestd;

voidexch(int&m,int&n,int&p)

{

inttemp=p;

p=n;

n=m;

m=temp;

}

intmain()

{

inta=1,b=2,c=3;

cout<<"a="<

exch(a,b,c);

cout<<"a="<

return0;

}

1.3思考题

1.自己设计一个程序,测试指向常量的指针,常指针,指向常量的常指针之间的区别。

#include

usingnamespacestd;

voidmain()

{

inta=10;

intconst*p=&a;

cout<

cout<<*p<

intb=20;

}

我们可以改变指针变量p所指向的内容,而不能改变p的地址空间,如 添加上p=&b;我们就会发现编译错误!

指向常量的指针const——int*p,特点是指针所保存的地址可以改变,然而指针所指向的值却不可以改变。

同理,当添加*p=b时,会发生编译错误!

指向常量的常指针

constintconst*p特点是指针所保存的地址不可变,指针所指向的数值也不可变。

2.编写一个函数,实现两个字符串变量的交换。

#include

usingnamespacestd;

voidExchg2(char*m,char*n)

{

chartmp=*m;

*m=*n;

*n=tmp;

}

voidmain()

{

chara='q';

charb='p';

cout<<"a="<

Exchg2(&a,&b);

cout<<"a="<

}

实验三类和对象—构造函数与析构函数

3.1实验目的

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

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

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

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

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

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

3.2实验内容

3.2.1程序阅读

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

#include

classCPoint

{

public:

voidSet(intx,inty);

voidPrint();

private:

intx;

inty;

};

voidCPoint:

:

Set(intx,inty)

{

x=x;

y=y;

}

voidCPoint:

:

Print()

{

cout<<"x="<

}

voidmain()

{

CPointpt;

pt.Set(10,20);

pt.Print();

}

问题一:

以上程序编译能通过吗?

如果不能,原因是什么?

能通过编译。

问题二:

以上程序的运行结构是否正确,如果不正确,分析为什么,如何改正?

结果不正确,因为Set函数中的形参与类中的相同产生错误,改为voidCPoint:

Set(intm,intn)。

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

#include

classCPerson

{

public:

voidPrint();

private:

CPerson();

private:

intage;

char*name;

};

CPerson:

:

CPerson()

{

}

voidCPerson:

:

Print()

{

cout<<"name="<

}

voidmain()

{

CPersonps(23,"张三");

ps.Print();

}

问题一:

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

#include

#include

usingnamespacestd;

classCPerson

{

public:

voidPrint();

CPerson(intm,stringn)

{

age=m;

name=n;

}

private:

intage;

stringname;

};

voidCPerson:

:

Print()

{

cout<<"name="<

}

voidmain()

{

CPersonps(23,"张三");

ps.Print();

}

3.2.2程序设计

1.设计实现一个CPoint类,满足以下要求:

a.该类包含两个整型成员变量x(横坐标)和y(纵坐标),以及一个输出函数Print()用来输出横坐标和纵坐标,要求不可以在类的外部直接访问成员变量;

b.可以采用没有参数的构造函数初始化对象,此时的成员变量采用默认值0;

c.可以采用直接输入参数的方式来初始化该类的成员变量;

#include

#include

usingnamespacestd;

classCPoint{

public:

voidprint();

CPoint(){x=0;y=0;}

point(intx1,inty1);

intGetX(){returnx;}

intGetY(){returny;}

private:

intx;

inty;

};

voidCPoint:

:

print()

{

cout<

}

CPoint:

:

point(intx1,inty1)

{

x=x1;

y=y1;

}

voidmain()

{CPointp;

CPoint();

p.print();

p.point(1,2);

p.print();

p.GetX();

p.GetX();

}

3.3思考题

1.设计一个CStudent(学生)类,并使CStudent类具有以下特点:

a.有学生姓名、学号、程序设计、信号处理、数据结构三门课程的成绩;

b.全部信息由键盘输入;

c.通过成员函数统计学生平均成绩,当课程数量增加时,成员函数无须修改仍可以求取平均成绩;

d.输出学生的基本信息、各科成绩与平均成绩;

e.学生对象用链表存储;

f.统计不及格学生人数。

#include

#include

#include

#defineN3

#defineM3

classCStudent

{

public:

voidsetstudent(char*name,char*sn,floatscore[N]);

voidshowstudent();

private:

charSname[10];

charSno[8];

floatScore[3];

floatAvg;

floatSum;

intcount;

};

voidCStudent:

:

setstudent(char*name,char*sn,floatscore[N])

{

inti;

floatSum=0.0;

intcount=0;

strcpy(Sname,name);

strcpy(Sno,sn);

for(i=0;i

{

Score[i]=score[i];

count++;

}

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

{

Sum=Sum+Score[i];

}

Avg=Sum/count;

}

voidCStudent:

:

showstudent()

{

inti;

cout<

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

cout<

cout<

}

voidmain()

{

inti,j,k=0;

charname[10],no[8];

floatscore[N];

for(j=1;j<=M;j++)

{

cout<<"pleaseinputstudent["<

cin>>name;

cout<<"pleaseinputstudent["<

cin>>no;

cout<<"pleaseinputstudent["<

for(i=0;i

cin>>score[i];

CStudentS1;

cout<<"student["<

cout<

S1.setstudent(name,no,score);

S1.showstudent();

if(score[i]<60)

k++;

}

cout<<"不及格人数:

"<

}

实验五派生与继承—单基派生

5.1实验目的

1.理解继承的概念;

2.理解公有派生、私有派生和保护派生;

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

5.2实验内容

5.2.1程序阅读

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

#include"iostream.h"

classCBase

{

public:

CBase(inta)

:

a(a)

{

}

protected:

voidprint()

{

cout<<"a="<

}

private:

inta;

};

classCDerive:

publicCBase

{

public:

voidprint()

{

CBase:

:

print();

cout<<"b="<

}

private:

intb;

};

voidmain()

{

CDerived;

d.print();

CBaseb;

b.print();

}

问题一:

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

答:

派生类CDerive中没有定义CDerive(),主函数中没有给d,b对象赋值。

#include"iostream.h"

classCBase

{

public:

CBase(inta)

:

a(a)

{

}

voidprint()

{

cout<<"a="<

}

private:

inta;

};

classCDerive:

publicCBase

{

public:

CDerive(inta,intc)

:

CBase(a)

{

b=c;

}

voidprint()

{

CBase:

:

print();

cout<<"b="<

}

private:

intb;

};

voidmain()

{

CDerived(1,3);

d.print();

CBaseb

(2);

b.print();

}

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

#include"iostream.h"

classCBase

{

public:

CBase(inta)

:

a(a)

{

cout<<"basestructure"<

}

~CBase()

{

cout<<"basedestructure"<

}

voidprint()

{

cout<<"a="<

}

protected:

inta;

};

classCDerive:

publicCBase

{

public:

CDerive(inta,intb,intc)

:

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

{

cout<<"derivestructure"<

}

~CDerive()

{

cout<<"derivedestructure"<

}

voidprint()

{

CBase:

:

print();

cout<<"b.a="<

cout<<"c="<

}

private:

CBaseb;

intc;

};

voidmain()

{

CDerived(1,2,3);-----------------------------------------------------①

d.print();

}

问题一:

以上程序的输出结果是什么,为什么?

答:

程序错误,不能输出结果。

“cout<<"b.a="<

问题二:

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

答:

b.a=2。

实验七多态性—函数与运算符重载

7.1实验目的

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

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

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

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

7.2实验内容

7.2.1程序阅读

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

#include"iostream.h"

classCComplex

{

public:

CComplex()

{

real=0;

imag=0;

}

CComplex(intx,inty)

{

real=x;

imag=y;

}

intreal;

intimag;

CComplexoperator+(CComplexobj1)-----------------------------------------------①

{

CComplexobj2(real+obj1.real,imag+obj1.imag);

returnobj2;

}

};

voidmain()

{

CComplexobj1(100,30);

CComplexobj2(20,30);

CComplexobj;

obj=obj1+obj2;------------------------------------------------------------------②

cout<

cout<

}

问题一:

①处的运算符重载,为什么该函数的返回值要设计成CComplex类型?

答:

运算符重载函数的返回值与其操作类的类型相同。

问题二:

②处

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

当前位置:首页 > 小学教育 > 语文

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2