面向对象程序设计实验教学大纲_第1页
面向对象程序设计实验教学大纲_第2页
面向对象程序设计实验教学大纲_第3页
面向对象程序设计实验教学大纲_第4页
已阅读5页,还剩28页未读 继续免费阅读

下载本文档

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

文档简介

1、面向对象程序设计实验教学大纲课程总学时: 64学分: 4实验学时: 16实验个数: 6 个实验学分: 1 分课程性质: 专业必修课适用专业: 计算机类专业教材及参考书:C+语言程序设计(第四版) ,郑莉、董渊编著,北京:清华大学出版社, 2011大纲执笔人:大纲审定人:一、实验课的性质与任务本课程实验大纲是面向计算机专业学生开设的C+程序设计实验课计划指导大纲,是依据面向对象程序设计课程教学计划指导大纲编制。本课程主要讲述了利用C+进行程序设计的思想和方法,既有面向过程和面向对象的程序设计的理论知识,又包括极强的实践应用能力的培养。本实验大纲力求结合该课程教学计划大纲的相应内容,由浅入深的指导

2、学生了解和掌握如何利用C+程序设计语言进行程序设计,提高学生的动手能力,做到理论和实践相结合,培养学生理解,分析程序,编写,调试程序的能力,使之能把程序设计应用到今后的专业学习中。二、实验目的与要求1实验目的通过本课程的学习, 使学生掌握面向过程的程序设计思想和编程思路, 初步掌握面向对象的程序设计思想,学会调试程序,能独立编写实用的小型程序。2实验要求(1)学生应该自始至终贯彻课程中所介绍的程序设计风格,养成良好的编程习惯;(2) 应独立完成所布置习题。为保证尽量在统一安排的上机时间内编译运行通过程序,学生应事先设计好程序。三、实验项目及内容提要面向对象程序设计实验课程( 071016)实实

3、验类型学序验学必基选号编实验名称时做分本验综设内容提要做号数操证合计作函数重载 , 类11类与对象4的设计与使用C+ 程序的结作用域与生222存期,静态成构员数组、指针与三种常见编334程元素的使字符串用44继承与派生2派生类的设计与使用55多态性2运算符重载、动态多态模板,异常处66模板和文件2理机制的设计四、实验内容安排:实验一( 设计性实验类与对象4学时)1. 目的要求:1) 掌握类的定义和使用;掌握类对象的声明;练习具有不同访问属性的成员的访问方式;观察构造函数和析构函数的执行过程;2) 学习类组合使用方法;3) 使用 VC+ 的 debug 调试功能观察程序流程,跟踪观察类的构造函数

4、、析构函数、成员函数的执行顺序。2. 实验内容:1) 编写重载函数 Max1 可分别求取两个整数,三个整数,两个双精度数,三个双精度数的最大值。#include <iostream>using namespace std;int Max1(int a,int b)/ 两个整形求最大值int c;c=(a>b)?a:b;cout<<c<<endl;return c;double Max1(double a,double b)/ 两个双精度数求最大值double c;c=(a>b)?a:b;cout<<c<<endl;retu

5、rn c;int Max1(int a,int b,int c)/三个整形求最大值int d;d=(a>b)?(a>c)?a:c):(b>c)?b:c);cout<<d<<endl;cout<<"n"<<endl;return d;double Max1(double a,double b,double c)/ 三个双精度数求最大值double d;d=(a>b)?(a>c)?a:c):(b>c)?b:c);cout<<d<<endl;cout<<&quo

6、t;n"<<endl;return d;void main()int a=1,b=8,c=12;double a1=4.2,b1=1.8,c1=6.7;cout<<" 原数为: n"<<endl;cout<<"a="<<a<<""<<"b="<<b<<""<<"c="<<c<<endl;cout<<"n

7、"<<endl;cout<<"a1="<<a1<<" "<<"b1="<<b1<<" "<<"c1="<<c1<<endl; cout<<"n"<<endl;cout<<"(a,b) 两个整形求最大值"<<endl;Max1(a,b);cout<<"n&quo

8、t;<<endl;cout<<"(a,b,c) 三个整形求最大值"<<endl;Max1(a,b,c);cout<<"(a1,b1) 两个双精度数求最大值"<<endl;Max1(a1,b1);cout<<"n"<<endl;cout<<"(a1,b1,c1) 三个双精度数求最大值"<<endl;Max1(a1,b1,c1);2) 写一个函数, 具有一个引用作为形参参数, 在函数中改变引用变量的值, 观察实参

9、变量的变化。#include<iostream>using namespace std;swap(int &x);void main()int a;cout<<"please input a number: a="scanf("%d",&a);swap(a);cout<<"now: "cout<<"a="<<a<<endl;swap(int &x)cout<<"please input a numb

10、er: x="scanf("%d",&x);return x;3) 定义一个 CPU 类,包含等级( Rank)、频率( frequency)、电压 (voltage) 等属性,有两个公有成员函数run、 stop。其中,rank 为枚举类型CPU_Rank, 定义为enumCPU_RankP1=1,P2,P3,P4,P5,P6,P7,frequency 为单位是MHz 的整型数, voltage 为浮点型的电压值。观察构造函数和析构函数的调用顺序。第一种:#include<iostream>using namespace std;class

11、CPUpublic:int run();int stop();CPU(int frequency1,float voltage1)cout<<"build!"<<endl;frequency=frequency1;voltage=voltage1;cout<<"frequency= "<<frequency<<" MHz"<<endl;cout<<"voltage= "<< voltage<<endl;CPU

12、()cout<<"destory!"<<endl;private:int frequency;float voltage;enum CPU_RankP1=1,P2,P3,P4,P5,P6,P7;void main()CPU a(3500,30);第二种:#include<iostream>using namespace std;class CPUpublic:int run();int stop();CPU( int frequency1,float voltage1):frequency(frequency1),voltage(volt

13、age1)cout<<"build!"<<endl;cout<<"frequency= "<<frequency<<" MHz"<<endl;cout<<"voltage= "<< voltage<<endl;CPU()cout<<"destory!"<<endl;private:int frequency;float voltage;enum CPU_RankP1

14、=1,P2,P3,P4,P5,P6,P7;void main()int frequency;float voltage;cout<<"frequenc= "cin>>frequency;cout<<"voltage= "cin>>voltage;CPU a(frequency,voltage);4)定义一个简单的Computer类,有数据成员芯片(cpu)、内存 (ram)、光驱 (cdrom) 等等,有两个公有成员函数run、stop。cpu 为CPU类的一个对象,ram 为RAM类的一个对象,cdrom

15、 为 CDROM 类的一个对象, 定义并实现这个类, 为以上的类编写构造和析构函数,观察组合类和内嵌类的构造函数和析构函数的调用顺序。#include<iostream>using namespace std;class CPUpublic:CPU()cout<<"cpu build!"<<endl;CPU()cout<<"cpu destory!"<<endl;class RAMpublic:RAM()cout<<"ram build!"<<endl

16、;RAM()cout<<"ram destory!"<<endl;class CDROMpublic:CDROM()cout<<"cdrom build!"<<endl;CDROM()cout<<"cdrom destory!"<<endl;class Computerprivate:CPU cpu;RAM ram;CDROM cdrom;public:Computer()cout<<"Computer build!"<<

17、;endl;Computer()cout<<"Computer destory!"<<endl;int main()Computer A;5) 为题目 2)的类编写复制构造函数, 在主函数中利用复制构造的方式创建新的对象,观察对象的状态。#include<iostream>using namespace std;class Aint a;public:A(int a1=0)a=a1;A(A &p);int getA()return a;A:A(A &p)a=p.a;void main()A a(2);cout<<

18、;" 原本结果: "<<a.getA()<<endl;A b(a);cout<<" 复制结果: "<<b.getA()<<endl;6) 思考并回答以下概念:函数重载,引用,类,对象,数据成员,函数成员,访问属性,构造函数,析构函数,类的组合,内嵌对象,初始化列表,复制构造函数。3. 主要仪器设备及软件: PC+Windows 2000+VC 6.0实验二( 设计性实验C+ 程序的结构2 学时)1. 目的要求:1) 观察程序运行中变量的作用域、生存期和可见性;2) 学习类的静态成员的使用;3)

19、学习多文件结构在 C+ 程序中的使用。2. 实验内容:7)实现客户机 (CLIENT) 类。定义字符型静态数据成员ServerName,保存其服务器名称 ; 整 型 静 态 数 据 成 员ClientNum , 记 录 已 定 义 的 客 户 数 量 ; 定 义 静 态 函 数ChangeServerName()改变服务器名称。#include<iostream>using namespace std;class CLIENTpublic:static void ChangeServerName()/静态函数ChangeServerName() 改变服务器名称cout<<

20、;" the ClientNum:ClientNum="<<ClientNum<<endl; cout<<" the ServerName:ServerName="<<ServerName<<endl;cout<<"change the ServerName:ServerName="cin>>ServerName;cout<<"n"<<endl;cout<<"now the Server

21、Name:ServerName="<<ServerName<<endl;private:static char ServerName;/ static int ClientNum;/字符型静态数据成员ServerName, 保存其服务器名称整型静态数据成员ClientNum, 记录已定义的客户数量;char CLIENT:ServerName='t'int CLIENT:ClientNum=0;void main()CLIENT A;CLIENT:ChangeServerName();8)利用多文件结构实现题目1),在头文件client.h 中

22、定义类,在文件client.cpp 中实现该类,在文件test.cpp 中测试这个类,观察相应的成员变量取值的变化情况,要求ClientNum 能够实时记录客户机对象的数量。在文件 client.cpp 中实现该类#include"client.h"#include<iostream>using namespace std;char CLIENT:ServerName='t'intCLIENT:ClientNum=0;void CLIENT:inputServerName() /静态函数ChangeServerName()改变服务器名称cout&

23、lt;<"input the ServerName:ServerName="cin>>ServerName;/cout<<"nowthe ServerName:ServerName="<<ServerName<<endl;ClientNum+;cout<<"nowthe ClientNum:ClientNum="<<ClientNum<<endl;CLIENT:CLIENT()ClientNum-;cout<<"nowthe

24、 ClientNum:ClientNum="<<ClientNum<<endl;在文件 test.cpp 中测试这个类#include"client.h"#include<iostream>using namespace std;void main()cout<<"begain!"<<endl;CLIENT A;CLIENT:inputServerName();CLIENT B;CLIENT:inputServerName();cout<<"end!"&

25、lt;<endl;在头文件client.h 中定义类class CLIENTpublic:static void inputServerName();CLIENT();private:static char ServerName;/ 字符型静态数据成员ServerName,保存其服务器名称static int ClientNum;/ 整型静态数据成员ClientNum, 记录已定义的客户数量;9)思考并回答以下概念:类的静态数据成员,类的静态函数成员,多文件结构,文件包含。3.主要仪器设备及软件:Windows 2000+VC 6.0实验三数组、指针与字符串实验(设计性实验4 学时)1.

26、 目的要求:1) 学习使用数组;学习字符串数据的组织和处理;学习标准C+ 库的使用;2) 掌握指针的使用方法;练习通过 debug 观察指针的内容及其所指的对象的内容;练习通过动态内存分配实现动态数组,并体会指针在其中的作用;3) 分别使用字符数组和标准 C+ 库练习处理字符串的方法。2. 实验内容:10) 编写一个类用于处理 3× 3 矩阵转置,测试转置的效果,输出转置前后的矩阵。#include<iostream>using namespace std;class Pointpublic:Point()/ 构造函数里赋值for(i=0;i<3;i+)for(j=

27、0;j<3;j+)cin>>aij;move()/ 转置for(i=0;i<3;i+)for(j=0;j<3;j+)bij=aji;void show()/ 显示转置结果for(i=0;i<3;i+)for(j=0;j<3;j+)cout<<bij;cout<<'t'<<endl;private:int i;/ 行int j;/ 列int a33;/ 原数组int b33;/ 转置数组;int main()int i,j;int x;cout<<"assignment begin

28、!"<<endl;cout<<"Please enter thevalues"<<endl;Point a1;a1.move();cout<<"result:"<<endl;a1.show();return 0;11)定义一个具有构造函数和析构函数的类,如实验一的CPU 类,定义一个CPU 的对象数组,观察构造函数的析构函数的调用过程。#include<iostream>using namespace std;class CPUpublic:CPU(int xx,int y

29、y,int tt)x=xx;y=yy;t=tt;cout<<t<<" build !"<<endl;CPU()cout<<t<<" No !"<<endl;private:int x,y,t;int main()CPU cpu3=CPU(1,2,1),CPU(3,4,2),CPU(5,6,3);return 0;12)利用动态内存分配的方式重新完成题目2)。#include<iostream>using namespace std;class CPUpublic:CPU(

30、):x(0),y(0),t(0)cout<<"begain !"<<endl;CPU(int xx,int yy,int tt):x(xx),y(yy),t(tt)void move(int xx,int yy,int tt)x=xx;y=yy;t=tt;cout<<t<<" build !"<<endl;CPU()cout<<t<<" No !"<<endl;private:int x,y,t;int main()CPU *p=new C

31、PU3;p0.move(1,2,1);p1.move(3,4,2);p2.move(5,6,3);cout<<"end !"<<endl;delete p;return 0;13) 使用系统提供的 string 类定义字符串对象并初始化, 实现从原始字符串中提取一个子串。#include<iostream>#include<string>using namespace std;int main()int i,j;string A="abcdefghijklnm"string B="12345678

32、9"cout<<"A="<<A<<endl;cout<<" 起始位置: "cin>>i;cout<<" 元素个数: "cin>>j;string a=A.substr(i-1,j);/ 从位置三开始的九个字符cout<<a<<endl;cout<<"n"<<endl;cout<<"B="<<B<<endl;cout<

33、;<" 起始位置: "cin>>i;cout<<" 元素个数: "cin>>j;string b=B.substr(i-1,j);/ 从位置七开始的两个字符cout<<b<<endl;return 0;14) 选做:定义一个 Point(二维点类)的对象数组,利用该数组实现直线的线性拟合。Point.h#ifndef _POINT_H#define _POINT_Hclass Pointpublic:Point(float x=0,float y=0):x(x),y(y)float get

34、X() const return x;float getY() const return y;private:float x,y;#endif3_5.cpp/* 定义一个Point(二维点类)的对象数组,利用该数组实现直线的线性拟合。#include"Point.h"#include<iostream>#include<cmath>using namespace std;*/float lineFit(const Point points,int nPoint)int i;float avgX=0,avgY=0;float lxx=0,lyy=0,l

35、xy=0;for(i=0;i<nPoint;i+)avgX+=pointsi.getX()/nPoint;avgY+=pointsi.getY()/nPoint;for(i=0;i<nPoint;i+)lxx+=(pointsi.getX()-avgX)*(pointsi.getX()-avgX);lyy+=(pointsi.getY()-avgY)*(pointsi.getY()-avgY);lxy+=(pointsi.getX()-avgX)*(pointsi.getY()-avgY);cout<<"This line can be fitted by y

36、=ax+b."<<endl;cout<<"a="<<lxy/lxx<<""cout<<"b="<<avgY-lxy*avgX/lxx<<endl;return static_cast<float>(lxy/sqrt(lxx*lyy);int main()Point p10=Point(6,10),Point(14,20),Point(26,30),Point(33,40),Point(46,50), Point(54,60),Po

37、int(67,70),Point(75,80),Point(84,90),Point(100,100);float r=lineFit(p,10);cout<<"Line coefficient r="<<r<<endl;return 0;15) 选做:定义一个动态数组类。#include<iostream>#include<cassert>using namespace std;class Pointpublic:Point():x(0),y(0)cout<<"Build !1!"

38、<<endl;Point(int x,int y):x(x),y(y)cout<<"Build !2!"<<endl;Point()cout<<"Destructor !3!"<<endl;void move(int newx,int newy)x=newx;y=newy;private:int x,y;class ArrayOfPointspublic:ArrayOfPoints(int size):size(size)points=new Pointsize;ArrayOfPoints()c

39、out<<"Delete !4!"<<endl;delete points;Point &element(int index)assert(index>=0 && index<size);return pointsindex;private:Point *points;int size;int main()int count;cout<<"Please enter the cout of points: "cin>>count;ArrayOfPoints points(c

40、ount);points.element(0).move(5,0);points.element(1).move(15,20);return 0;16) 思考并回答:数组,指针,对象数组,动态内存分配,默认构造函数,标准类库,字符串类 string,线性拟合。3.主要仪器设备及软件:Windows 2000+VC 6.0实验四(设计性实验继承与派生2 学时)1. 目的要求:1) 学习定义和使用类的继承关系,定义派生类;熟悉不同继承方式下对基类成员的访问控制;2) 学习利用虚基类解决二义性问题。2. 实验内容:17)定义一个基类Animal ,有私有整型成员变量age,构造其派生类dog,在其成

41、员函数SetAge(int n)中直接给age 赋值,看看会有什么问题,把age 改为公有成员变量,还会有问题吗?编程试试看。#include<iostream>using namespace std;class Animal/private:public:int age;class dog:public Animalpublic:SetAge(int n)age=n;cout<<"age="<<age<<endl;int main()dog a;int n;cout<<"n= "cin>

42、>n;a.SetAge(n);return 0;18) 定义一个基类 BaseClass,有整型成员变量 Number ,构造其派生类 DerivedClass, 定义该派生类的对象 ,观察构造函数和析构函数的执行情况。#include<iostream>using namespace std;class BaseClassprivate:int Number;public:BaseClass(int number)Number=number;/ cout<<"BaseClass.Number="<<Number<<en

43、dl; cout<<"build BaseClass"<<endl;BaseClass()cout<<"destroied BaseClass"<<endl;class DerivedClass:public BaseClassprivate:int a;public:DerivedClass(int aa):BaseClass(aa)a=aa;/ cout<<"DerivedClass.a="<<a<<endl; cout<<"

44、build DerivedClass"<<endl;DerivedClass()cout<<"destroied DerivedClass"<<endl;int main()DerivedClass derived(5);return 0;19) 定义一个车( vehicle )基类,具有 MaxSpeed、 Weight 等成员变量, Run、 Stop 等成员函数,由此派生出自行车(bicycle )类,汽车( motorcar )类。自行车( bicycle )类有高度(Height )等属性,汽车(motorcycle

45、)类有座位数(SeatNum)等属性。从bicycle和motorcycle派生出摩托车(Motorcar) 类,在继承过程中, 注意把vehicle设置为虚基类。如果不把vehicle 设置为虚基类,会有什么问?编程实验及分析原因。#include<iostream>using namespace std;class vehicleprivate:int MaxSpeed;int Weight;public:void Run()cout<<"1vehicle Run !"<<endl; void Stop()cout<<&q

46、uot;1vehicle Stop !"<<endl;vehicle(intWeight1=0):MaxSpeed(MaxSpeed1),Weight(Weight1)cout<<"1vehicle()cout<<"1vehicle end !"<<endl;MaxSpeed1=0,int vehicle start !"<<endl;class bicycle:virtual public vehicleprivate:int Height;public:bicycle(int He

47、ight1=0):Height(Height1)cout<<"2bicycle()cout<<"2bicycle end !"<<endl;bicycle start !"<<endl;class motorcycle:virtual public vehicleprivate:int SeatNum;public:motorcycle(int SeatNum1=0):SeatNum(SeatNum1)cout<<"3motorcycle()cout<<"3mot

48、orcycle end !"<<endl;bicycle start !"<<endl;class Motorcar:public bicycle,public motorcyclepublic:Motorcar()cout<<"4Motorcar()cout<<"4Motorcar start !"<<endl;Motorcar end !"<<endl;int main()Motorcar Motorcar1;return 0;若没有设为虚基类,截图为:20) 思考并回答:继承,派生,子类对基类成员的访问权限,继承方式,继承时的构造函数和析构函数的调用顺序,虚基类3. 主要仪器设备及软件: PC+Windows 2000+VC 6.0实验五多态和运算符重载( 设计性实验2 学时)1. 目的要求:掌握运算符重载的方法;学习使用虚函数实现动态多态性。2. 实验内容:21) 定义 Point 类,有坐标 x,y 两个私有成员变量 ;对 Point 类重载 “ +”(相加)、“ -”(相减)和“ =”(相等)运算符,实现对坐标的改变,要求用友元函数和成员函数两种方法实现。对 Point 类重载

温馨提示

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

评论

0/150

提交评论