实验4 类.docx
- 文档编号:5513627
- 上传时间:2022-12-17
- 格式:DOCX
- 页数:17
- 大小:159.84KB
实验4 类.docx
《实验4 类.docx》由会员分享,可在线阅读,更多相关《实验4 类.docx(17页珍藏版)》请在冰豆网上搜索。
实验4类
南昌大学实验报告
学生姓名:
学号:
专业班级:
实验类型:
□验证□综合
设计□创新实验日期:
实验成绩:
一、实验名称
实验4类与对象
二、实验目的
类是C++扩展数据类型,可以封装不同类型的数据成员和函数成员,类是面向对象程序设计的基础。
本次实验内容包括面向对象的基本概念、构造函数与析构函数,从实际问题抽象出类等,通过实验要求:
1、掌握面向对象的基本概念,类的定义方法;
2、掌握类成员的访问权限及访问类成员的方法;
3、掌握内联函数;
4、掌握引用概念及应用,引用作为函数参数;
5、掌握构造函数与析构函数的意义及使用方法。
三、实验内容
1、验证课本例子4-7
2、
4-11定义并实现一个矩形类,有长、宽两个属性,用成员函数计算矩形的面积。
4-13定义一个Circle类,有数据成员Radius(半径),成员函数GetArea(),计算圆的面积构造一个Circle的对象进行测试。
3、程序输出的结果:
classComplex
{
private:
doublem_real;
doublem_imag;
public:
Complex(void)
{
m_real=0.0;
m_imag=0.0;
cout<<"调用了构造函数Complex(void)"< } Complex(doublereal,doubleimag) { m_real=real; m_imag=imag; cout<<"调用了构造函数Complex(doublereal,doubleimag)"< } Complex(constComplex&c) { //将对象c中的数据成员值复制过来 m_real=c.m_real; m_imag=c.m_imag; cout<<"调用了拷贝构造函数Complex(constComplex&c)"< } Complex: : Complex(doubler) { m_real=r; m_imag=0.0; cout<<"调用了构造函数Complex(doubler)"< } Complex&operator=(constComplex&rhs) { //首先检测等号右边的是否就是左边的对象本,若是本对象本身,则直接返回 if(this==&rhs) { return*this; } //复制等号右边的成员到左边的对象中 this->m_real=rhs.m_real; this->m_imag=rhs.m_imag; //把等号左边的对象再次传出 //目的是为了支持连等eg: a=b=c系统首先运行b=c //然后运行a=(b=c的返回值,这里应该是复制c值后的b对象) cout<<"调用了=操作符重载"< return*this; } }; voidmain() { Complexc1,c2; Complexc3(1.0,2.5); Complexc4=Complex(1.0,2.5); c1=c3; c2=5.2; Complexc5(c2); Complexc6=c2; } (1)仔细观察复制构造函数,为什么函数中可以直接访问对象c的私有成员? Complex(constComplex&c) { //将对象c中的数据成员值复制过来 m_real=c.m_real; m_img=c.m_img; } (2)分析引用与传值的区别 Complextest1(constComplex&c) { returnc; } Complextest2(constComplexc) { returnc; } voidmain() { Complexa,b; //下面函数执行过程中各会调用几次构造函数,调用的是什么构造函数? test1(a); test2(a); test2(1.2); //下面这条语句会出错吗? test1(1.2); //test1(Complex(1.2))呢? } 4、有一根27厘米的细木杆,在第3厘米、7厘米、11厘米、17厘米、23厘米这五个位置上各有一只蚂蚁。 木杆很细,不能同时通过一只蚂蚁。 开始时,蚂蚁的头向(右,左,右,左,左),它们只会朝前走或调头,但不会后退。 当任意两只蚂蚁碰头时,两只蚂蚁会同时调头朝反方向走。 假设蚂蚁们每秒钟可以走一厘米的距离。 定义一个蚂蚁类Ant,编写程序,求所有蚂蚁都离开木杆的时间。 四、实验环境 PC微机 Windows操作系统 MicrosoftVisualStudio6.0集成开发环境; MicrosoftVisualStudio6.0集成开发环境的MSDN 五、实验步骤 1、解决方案: #include usingnamespacestd; classemployee { protected: intindividualEmpNo; intgrade; floataccumPay; public: employee(); ~employee(); voidIncreaseEmpNo(int); voidpromote(int); voidSetaccumPay(float); intGetindividualEmpNo(); intGetgrade(); floatGetaccumPay(); }; employee: : employee() { individualEmpNo=1000; grade=1; accumPay=0.0; } employee: : ~employee(){} voidemployee: : IncreaseEmpNo(intsteps) { individualEmpNo+=steps; } voidemployee: : promote(intincrement) { grade+=increment; } voidemployee: : SetaccumPay(floatpa) { accumPay=pa; } intemployee: : GetindividualEmpNo() { returnindividualEmpNo; } intemployee: : Getgrade() { returngrade; } floatemployee: : GetaccumPay() { returnaccumPay; } voidmain() { employeem1; employeet1; employeesm1; employees1; cout<<"请输入下一个雇员的月薪: "; floatpa; cin>>pa; m1.IncreaseEmpNo(0); m1.promote(3); m1.SetaccumPay(pa); cout<<"请输入下一个雇员的月薪: "; cin>>pa; t1.IncreaseEmpNo (1); t1.promote (2); t1.SetaccumPay(pa); cout<<"请输入下一个雇员的月薪: "; cin>>pa; sm1.IncreaseEmpNo (2); sm1.promote (2); sm1.SetaccumPay(pa); cout<<"请输入下一个雇员的月薪: "; cin>>pa; s1.IncreaseEmpNo (1); s1.SetaccumPay(pa); //显示m1信息 cout<<"编号"< //显示t1信息 cout<<"编号"< //显示sm1信息 cout<<"编号"< //显示s1信息 cout<<"编号"< } 2、解决方案: #include usingnamespacestd; constdoublePI=3.14; classCircle { private: floatRadius; public: Circle(); Circle(doubleradius); ~Circle(); doubleGetArea(doubleradius); }; Circle: : Circle() {Radius=0.0;} Circle: : Circle(doubleradius) { Radius=radius; } Circle: : ~Circle(){} doubleCircle: : GetArea(doubleradius) { returnPI*radius*radius; } classRectangle { protected: floatlength; floatwidth; public: Rectangle(); Rectangle(floatx,floaty); ~Rectangle(); floatGetArea(floatlength,floatwidth); }; Rectangle: : Rectangle(floatx,floaty) { length=x; width=y; } Rectangle: : Rectangle() { length=0; width=0; } Rectangle: : ~Rectangle(){} floatRectangle: : GetArea(floatlength,floatwidth) { returnlength*width; } voidmain() { Rectanglerec; floatx,y; cout<<"求矩形的面积"< cout<<"请输入矩形的长x: "; cin>>x; cout<<"请输入矩形的宽y: "; cin>>y; cout<<"请输出矩形的面积area: "< cout<<"求圆形的面积"< Circlecir; floatradius; cout<<"请输入圆形的半径radius: "; cin>>radius; cout<<"请输出圆形的面积area: "< 4、解决方案: #include usingnamespacestd; constboolRight=true; constboolLeft=false; classAnt { public: Ant(intAnt_Number,intPosition,boolInitial_Direction); ~Ant(); voidDisPlay(intSeconds); intGetPosition(void); voidChangeDirection(void); boolGetDirection(void); Ant&operator++(void); Ant&operator--(void); private: intAntNum;//蚂蚁号码 intTime;//所需时间 intIniPosition;//位置 boolAnt_Ini_Direction;//方向 boolOver;//判断是否出杆 }; Ant: : Ant(intAnt_Number,intPosition,boolInitial_Direction) { if(Position<=0||Position>=27) cout<<"蚂蚁的位置错误"< if(Ant_Number>5) cout<<"蚂蚁数量超过5."< AntNum=Ant_Number; IniPosition=Position; Ant_Ini_Direction=Initial_Direction; Time=0; Over=false; } Ant: : ~Ant(){} voidAnt: : DisPlay(intSeconds) { if(! Over) { cout<<"第"< } } boolAnt: : GetDirection(void) { returnAnt_Ini_Direction; } intAnt: : GetPosition(void) { returnIniPosition; } voidAnt: : ChangeDirection(void) {if(! Over) { if(Ant_Ini_Direction==Right) { Ant_Ini_Direction=Left; } else { Ant_Ini_Direction=Right; } } } Ant&Ant: : operator++(void) { IniPosition++; if(IniPosition>27) { DisPlay(Time); Over=true; } Time++; return*this; } Ant&Ant: : operator--(void) { IniPosition--; if(IniPosition<0) { DisPlay(Time); Over=true; } Time++; return*this; } voidmain() { Antant1(1,3,Right); Antant2(2,7,Left); Antant3(3,11,Right); Antant4(4,17,Left); Antant5(5,23,Left); cout<<"蚂蚁爬杆问题"< while (1) { {if(ant1.GetDirection()==Right) ant1.operator++(); else ant1.operator--(); } {if(ant2.GetDirection()==Right) ant2.operator++(); else ant2.operator--(); } {if(ant3.GetDirection()==Right) ant3.operator++(); else ant3.operator--(); } {if(ant4.GetDirection()==Right) ant4.operator++(); else ant4.operator--(); } {if(ant5.GetDirection()==Right) ant5.operator++(); else ant5.operator--(); } if(ant1.GetDirection()! =ant2.GetDirection()) { if(ant1.GetPosition()==ant2.GetPosition()) { ant1.ChangeDirection(); ant2.ChangeDirection(); } } if(ant2.GetDirection()! =ant3.GetDirection()) { if(ant2.GetPosition()==ant3.GetPosition()) { ant2.ChangeDirection(); ant3.ChangeDirection(); } } if(ant3.GetDirection()! =ant4.GetDirection()) { if(ant3.GetPosition()==ant4.GetPosition()) { ant3.ChangeDirection(); ant4.ChangeDirection(); } } if(ant4.GetDirection()! =ant5.GetDirection()) { if(ant4.GetPosition()==ant5.GetPosition()) { ant4.ChangeDirection(); ant5.ChangeDirection(); } } } } }六、实验数据及处理结果 1、 2、 3、 (1)拷贝函数本就在类的自身当中,可以对其所有成员进行访问。 (2)在传值调用中,形参的改变不会影响实参,他们并不在一个内存空间里;而引用中,形参的改变也就导致了实参的改变,形参获得实参的地址,共享内存空间。 4、 七、实验总结及体会
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 实验4 实验