




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、实验报课程名称VC+程序设计专业班级电子0942电气与信息学院和谐勤奋求是创新实验教学考核和成绩评定办法课内实验考核成绩,严格按照该课程教学大纲中明确规定的比重执行。实验成绩不合格者,不能参 加课程考试,待补做合格后方能参加考试。单独设立的实验课考核按百分制评分,考核内容应包括基本理论、实验原理和实验。3)1)实验考核内容包括:1)实验预习;2)实验过程(包括实验操作、实验记录和实验态度、表现); 实验报告;权重分别为 0.2、0.4、 0.4;原则上根据上述三个方面进行综合评定。学生未取得 和2)项成绩时,第 3)项成绩无效。实验指导教师应严格按照考核内容分项给岀评定成绩,并及时批改实验报告
2、,给岀综合成绩,反馈 实验中岀现的问题。实验成绩在教师手册中有记载。-一一实验目的二.实验仪器及设备实验原理四.实验步骤五.实验记录及原始记录、.六.数据处理及结论七.实验体会(可选项)注:1.为了节省纸张,保护2.3.实验报告主要内容,便于保管实验报告,统一采用A4纸,实验报告建议双面打印(正文采用宋体五号字) 或手写,右侧装订。实验题目实验五构造函数与析构函数的编程实验室电信机房实验时间年 月日实验类别验证同组人数1实验类别指验证、演示、综合、设计、创新(研究)、操作六种类型实验。验证性实验:是指为了使学生巩固课程基本理论知识而开设的强调演示和证明,注重实验结果(事实、概念或 理论)的实验
3、。4. 综合性实验:是指实验内容涉及本课程的综合知识或本课程相关的课程知识的实验。5. 设计性实验:是指给定实验目的、要求和实验条件,由学生自行设计实验方案并加以实现的实验。成绩指导教师签字:一、实验目的1 .进一步加深对类和对象的理解。2 .掌握类的构造函数和析构函数的概念、意义和使用方法。3.掌握重载构造函数的含义和使用。4编写一个较为复杂的类和对象的应用程序。二、实验内容1 设计一个程序,定义一个矩形类,包括数据成员和函数成员。要求有构造函数、析构函数,还有一个成 员函数area()用来计算并显示矩形的面积,并编写main函数进行测试。程序:# in clude<iostream.
4、h> # in clude<math.h> class Rect public:Rect(i nt l, i nt w);Recto; int Area(); private: int nLen gth; int nWidth;Rect:Rect(i nt l, i nt w) cout<<"executi ng con structor."<<e ndl; nLen gth=l;n Width=w;cout< <"nLen gth=" <<nLen gth<<e ndl<
5、; <"n Width=" <<n Width<<e ndl; Rect:Rectocout<<"executi ng con structor."<<e ndl;cout< <"nLen gth=" <<nLen gth<<e ndl< <"n Width=" <<n Width<<e ndl; int Rect:Area() i nt t;t=nLen gth* nWidth;retur
6、n t;int mai nO Rect A(3,4); cout<<"s="<<A.Area()<<e ndl;return 0;我"E:XDebufXvbfbf. exe*运行结果:execut in ST nLengt 11=3 nWidth=4 =12 executing nLength=3 nWidth=4 Press anyCOnstrULCtOK.constpuctop-.Key to continuedouble型记2 .重载构造函数。修改上题,一种构造函数用整型变量记录矩形的长和宽,另一种构造函数用 录矩形的长和
7、宽,然后完成成员函数及主函数。程序:# in clude<iostream.h># in clude<math.h>class Rectp ublic:Rect(i nt l, int w);Rect(double u, double v);int Area();double Area(double u, double v);Rect();private:int nLen gth;int nWidth;double mLen gth; double mWidth;;Rect:Rect(i nt l, i nt w)cout<<"executi ng
8、 con structor."<<e ndl;nLen gth=l;n Width=w;cout< <"nLen gth=" <<nLen gth<<e ndl< <"n Width=" <<n Width<<e ndl;Rect:Rect(double u, double v)cout<<"executi ng con structor."<<e ndl; mLen gth=u;mWidth=v;cout<&l
9、t;"mLe ngth="<<mLe ngth<<e ndl<<"mWidth="<<mWidth<<e ndl; Rect:Rect()cout<<"executi ng con structor."<<e ndl;int Rect:Area()i nt t;t=nLe ngth* nWidth;return t;double Rect:Area(double u, double v) double r;r=mLe ngth*mWidth;retur
10、n r;void mai n()Rect A(3,4);cout<<"a 的面积="<<A.Area()<<e ndl;Rect B(22,33);cout<<"b 的面积="<<B.Area()<<e ndl;C* "E:Debujvhfbf.eie*远行结果:execut ing constructor. nLength=3 nWidtIi=4|的面积12execut ing constkuctai'. nLength=22 nWidth=33h的面积刃毋 ex
11、ecuting constvuctov. executing constructor., PreESkey to continue3 .构造一个类countstr ,要求用构造函数设置计数器 何值,它要求用户输入一段文字, 返回cou nt的整数值。程序:#in clude<iostream.h>count的初始值为0,成员函数countcharO不返回任按Enter键后结束计算,用count记录输入的字符数,成员函数getchar()class COU ntstr p ublic:cou ntstrO cou nt=O;void cou ntcharO cout<<&
12、quot;cin> >str;请输入字符串:"实验题目实验六利用友元编程实验室电信机房实验时间年 月日实验类别验证同组人数1成绩指导教师签字:while(strcou nt!='O') cou nt+; int getchar() retur n cou nt; p rivate:i nt cou nt;char str200; ; void main() cou ntstr s;s.cou ntchar(); cout<<"the n umber="<<s.getchar()<<e ndl;一、实验
13、目的1. 掌握友元函数和友元类的概念、定义和作用。2. 会使用友元编程。3. 进一步学习内存空间动态分配和释放的方法。二、实验内容1有一个向量类 Vector,包括一个点的坐标位置x和y,设计两个友元函数,实现两个向量的加法和减法运算。Print显示该直线方2定义一个由y=ax+b确定的直线类 Line,该类的构造函数初始化直线,成员函数程,友元函数Set Poi nt()求解两条直线的交点。3.下列程序是有关友元类的,程序中将A类声明为B类的友元类,A类中所有的成员函数都是B类的友元函数,请给程序填空。#in lcude <iostream.h>class B;class A p
14、rivate: int x; p ublic: A(i nt xx)x=xx; int Set(B&); int Get()return x; ;class B private: int x;public:B(i nt xx) frie nd;int A:Set( _ retur n x=b.x;void mai nO A a(10);B b(20);cout<<a.Get()<<e ndl;a.Set(b); cout<<a.Get()<<e ndl;三、编程分析及运行结果1、#include <iostream.h>cla
15、ss Vector p ublic:Vector(double m=0,double n=0)x=m ;y=n;void dis pl ay() cout<<"("<<x<<","<<y<<")"<<e ndl; frie nd Vector op erator+(Vector &a,Vector &b) Vector t; t.x=a.x+b.x; t.y=a.y+b.y; retur n t; frie nd Vector op erator-
16、(Vector &a,Vector &b) Vector t; t.x=a.x-b.x;t.y=a.y-b.y; retur n t; Vector()private:double x,y;void mai n() Vector a1(8,6),a2(1,2.1),a3,a4;cout<<" 向量 a1=" a1.display();cout<<"向量 a2=" a2.display();a3=a1+a2;cout<<"a1+a2=" a3.dis playO; a4=a1-a2;c
17、out<<"a1-a2=" a4.dis play();2、#include <iostream.h>class Line p ublic:Lin e(double m=0,double n=0)a=m;b=n;void print() cout<<"直线 y="<<a<<"x+"<<b<<""<<endl;void frie nd set poi nt(Li ne & l1,Li ne & l2)H.
18、double x,y;if(l1.a=l2.a) cout<<"两直线平行,无交点else X=(l1.b-l2.b)/(l2.a-l1.a);y=l1.a*x+l1.b;cout<<"两直线交点为("<<x<<","<<y<<")"<<endl;Li ne() private:double a,b;void mai nO Line L1(3,5),L2(-2,6);L1. prin t();L2. prin t(); set poi nt(
19、L1,L2);3、填空后程序:#in clude <iostream.h>class B;class A private: int x;public:A(i nt xx)x=xx;int Set(B &);int Get()return x;class Bprivate:int x;p ublic:B(i nt xx)x=xx;frie nd class A;int A:Set(B &b) retur n x=b.x;void mai nO A a(12);B b(34);cout<<a.Get()<<e ndl;a.Set(b);cout&
20、lt;<a.Get()<<e ndl;四、解决方法及难点难点:函数重载时,注意重载的实现方法:通过形参类型或个数来区分调用哪个函数。一、实验目的了解继承在面向对象程序设计中的重要作用。理解继承与派生的概念,掌握单继承和多继承的定义方法。熟悉公有派生和私有派生的访问特性。掌握继承中基类和派生类的构造函数定义和调用过程。了解虚基类的作用和用法,会使用虚基类,学习虚基类在解决二义性问题中的作用2.3.4.5.二、实验内容有以下程序,请完成下面的工作:阅读程序,写岀运行后的输岀结果;然后上机运行,验证结果是否正确;分析程序执行过程,尤其是调用构造函数和析构函数的过程。1)2)3)#i
21、n elude <iostream.h>class Ap ublic:A()cout<<A()cout<<“constructing A” <<endl;“destructing A ” <<endl;实验题目实验七继承与派生的编程实验室电信机房实验时间年月日实验类别验证同组人数1成绩指导教师签字:;class B:p ublic Ap ublic:B()cout<<B()cout<<class C:p ublic Bp ublic:C()cout<<C()cout<<void mai
22、n() C c1;2. 定义一个"con struct ing B "destruct ing B"con struct ing C “destruct ing CPoint类,派生岀” <<endl; ” <<endl;” <<endl;<<e ndl;Rectangle类和Circle类,计算各派生类对象的面积 Area()。编写一个完整程序进行测试。3. 定义并描述一个人员类Person,它派生岀学生类 Student和教师类Teacher,学生类和教师类共同派生岀在职读书的教师类Stu_Tech。人员类有姓名
23、、性别、身份证号、岀生年月等信息;学生类有学号、成绩等信息;教师类有职务、职称等信息。编写一个完整程序进行测试。三、编程分析及运行结果1、运行结果:2、class Point public:void inpoin t(double x,double y) Px=x; Py=y;double get PxO return P x;double get PyO return P y;p rotected:double Px, Py;class Recta ngle:p ublic Point public:Recta ngle(double x,double y,double h,double w
24、) inpoin t(x,y); High=h; Wide=w;double getHigh()retur n High; double getWide()return Wide; double Area() return High*Wide; p rotected:double High,Wide;class Circle: public P oi nt public:Circle(double x,double y,double r) inp oi nt(x,y); R=r;double getR() return R;double Area() double pi=3.1415926;
25、return pi*R*R; p rotected:double R;#in clude <iostream.h> void mai n() double x,y,h,w,r;coutvv"请输入矩形的位置坐标(x,y)、长、宽:"cin> >x>>y>>h>>w;Recta ngle Rect1(x,y,h,w);coutvv"矩形的数据:"<<endlvv"坐标("<<Rect1.getPx()vv"," <<Rec
26、t1.get Py ()vv"),theigh="vvRect1.getHigh()vv",twidth=" vvRect1.getWide()vv",tArea="vvRect1.Area()vve ndl;coutvv"请输入圆的位置坐标(x,y)及半径:"cin> >x>>y>>r;Circle C1(x,y,r);coutvv" 圆的数据:"vvendlvv" 坐标("vvCI.getPx()vv","vvC1.
27、getPy() vv"),tR="vvC1.getR()vve ndl;3、#include viostream.h> #in clude <stri ng.h> class Person p ublic:P ers onO;P ers on( char *n ame1,char sex1,char *id1,char *birth); void dis playO;P ers onO;p rotected:char *n ame;char *id;char sex;char *birthday;;Person: Person() n ame='0
28、'sex=O; id='O:birthday='0'Person: Person (char *n ame1,char sex1,char *id1,char *birth) n ame=new charstrle n(n ame1)+1; n ame=strc py(n ame ,n ame1); sex=sex1;id=new charstrle n(id1)+1;id=strc py(id,id1);birthday=new charstrle n( birth)+1;birthday=strc py (birthday,birth);void Perso
29、n:dis pl ay() cout< <"n ame:"< <n ame<<"nsex:"<<sex<<"ni d:"<<id<<"nbirthday:"<<birthday<<e ndl;Person: Person() delete name;deleteid; deletebirthday;class Stude ntvirtual p ublic P ers on p ublic:Stude nt(
30、char *n ame,char sex,char *id,char *birthday,l ong int s_n um1,float score1); void dis play() Person:dis pl ay();cout<< "s_n umber:"<<s_ num<<"nscore:"<<score<<e ndl;Stude nt()p rotected:long int s_num; float score;;Stude nt:Stude nt(char *n ame1,cha
31、r sex1,char *id1,char *birth,l ong int s_n um1,float scorel): Pers on(n ame1,sex1,id1,birth) s_n um=s_ num1;score=score1;class Teacher:virtual p ublic P ers on public:Teacher(char *n ame1,char sex1,char *id1,char *birth,char *po sit,char *title1): Perso n(n ame1,sex1,id1,birth) po siti on=new charst
32、rle n(po sit)+1; p ositi on=strc py(po siti on,po sit);title=new charstrle n(title1)+1; title=strc py(title ,titl e1);void dis play() Person:dis pl ay();cout<< "Po sitio n:"<<p ositi on< <"nTitle:"<<title<<e ndl;Teacher() delete positi on;deletetitle
33、;p rotected:char *po siti on;char *title;class Stu_teach:p ublic Stude nt, public Teacher public:Stu_teach(char *n ame1,char sex1,char *id1,char *birth,l ong int s_n um1,float score1,char *p osit,char *title1): Person(n ame1,sex1,id1,birth),Stude nt(n ame1,sex1,id1,birth,s_ num1,score1),Teacher (n a
34、me1,sex1,id,birth ,po sit,title1) void dis play();Stu_teach();void Stu_teach:dis playO Stude nt:dis pl ay();cout<< "Po sitio n:"< <po sitio n<<"nTitle:"<<title<<e ndl;void mai nOs1.dis playO;t1.dis playO;s_t1.dis playO;四、解决方法及难点难点:用虚基类解决继承和派生时的二义性问题.
35、通过虚基类来实现.实验八利用虚函数编程一、实验目的了解多态性的概念和虚函数的作用及使用方法。了解静态关联和动态关联的概念和用法。了解纯虚函数和抽象类的概念和用法。学习使用虚函数的继承实现动态关联。 学习理解静态数据成员和静态成员函数的使用。、实验内容2.3.4.5.定义基类声明输岀函数受保护成员,派生类可访问1事先阅读程序,给岀其运行结果,上机验证虚函数的作用。 有如下程序:#in clude <iostream.h>#in clude <stri ng.h>class Stude nt/p ublic:Stude nt(i nt,stn ngfloat); void
36、dis pl ay();/protected:/int num;stri ng n ame;float score;Stude nt :Stude nt(i nt n, stri ng n am,float s)num=n;n ame=n am;score=s;void Stude nt:dis play()声明公有派生类cout< <n um: <<n um< <nn ame:< <n ame< <n score:<<score<<e ndl<<e ndl;class Graduate:p ubl
37、ic Stude nt /p ublic:声明输岀函数Graduate(i nt,stri ng,float,flaot);void dis play();/private:float p ay;Graduate:Graduate(i nt n, stri ng n am,float s,flaot p ):Stude nt(n,n am,s), pay(p)void Graduate:dis play()cout< <<n um: <<n um< <nn ame:< <n ame< <n score:<<score
38、< <np ay: <<p ay<<e ndl;void mai n() Stude nt stud(1001, Li,87.5);Graduate grad(2001, Wan g,98.5,563.5);Stude nt *pt=& stud;p t->dis pl ay();pt=&grad;p t->dis pl ay();1) 分析结果,并验证之。2) 利用虚函数,对程序作一点修改,在Stude nt类中声明dis play 函数时,在其前面加上关键字virtual,即virtual void disp lay();,再
39、编译运行,注意分析运行结果。2. 事先编写一个程序,计算正方体、球体和圆柱体的表面积和体积。(提示:声明一个抽象基类 container派生类cube、sphere和cylinder ,基类中求表面积和体积的成员函数声明为纯虚函数。)3定义一个类Student记录学生计算机课程的成绩,要求使用静态成员变量或静态成员函数计算全班计 算机课程的总成绩和平均成绩。三、编程分析及运行结果1、2、#inelude <iostream.h>const double pi=3.1415; class containerp ublic: virtualvoidvirtualvoidvirtualv
40、oiddisp lay()Area()Volume();class cube:p ublic containerp ublic:cube(double a)Le ngth=a;void dis play()cout<<"cube: nLen gth="<<Le ngth<<e ndl; void Area()cout<<"Area="<<6*Le ngth*Le ngth<<e ndl;void Volume()cout<<"Volume="<&
41、lt;Le ngth*Le ngth*Le ngth<<e ndl; cube()p rotected:double Len gth;class sp here:p ublic container public:sp here(double r)R=r;void dis playOcout<<"s phere: nR="<<R<<e ndl;void Area()cout<<"Area="<<4* pi *R*R<<e ndl;void Volume()cout<&
42、lt;"Volume="<<4.0/3* pi*R*R*R<<e ndl;sp here() p rotected:double R;class cyli nder public:cyli nder(double r,double h)R=r;H=h;void dis play()cout<<"cyli nder:nR="<<R<<",thight="<<H<<e ndl;void Area()cout<<"Area="
43、<<2* pi *R*R+2* pi *R*H<<e ndl;void Volume()cout<<"Volume="<< pi*R*R*H<<e ndl;cyli nder() p rotected:double R,H;void mai n() cube cu1(5); cul.dis pl ay(); cu1.Area(); cu1.Volume(); sp here s1(6);sl.dis playO;s1.Area();s1.Volume(); cyli nder cy1(3,8); cy1.dis p
44、lay(); cy1.Area(); cy1.Volume();3、 #inelude <iostream.h> #in clude <stri ng.h> class Stude ntp ublic:Stude nt(l on g,char *,float); void dis playO;Stude nt()delete name; p rotected:static float average;static int sum;long n um;char *n ame;float score;;float Stude nt:average=O;int Stude n
45、t:sum=0;Stude nt:Stude nt(l ong n, char *n am,float s) num=n;n ame=new charstrle n(n am)+1;n ame=strc py( name, nam);score=s;average=(average*sum+s)/(sum+1);sum+;void Stude nt:dis pl ay()coutvv"Number:"v <n um<<"nN ame:"< <n ame<<"nScore:"<<sc
46、ore<<"naverage:"<<average<<e ndl; void mai nOstu1.dis playO;stu2.dis playO;stu3.dis playO;四、解决方法及难点难点:实现函数多态性时虚函数与抽象基类的定义与运用;用静态数据成员变量体现类中的总体情况。一、实验目的了解运算符重载的概念和使用方法掌握几种常见的运算符重载的方法,通过编程实践,理解运算符重载的意义。 学习内存空间动态分配和释放的方法。了解在Visual C+6.0 环境下进行运算符重载要注意的问题。2.3.实验九运算符重载编程4.二、实验内容
47、1 设计一个2行3列的矩阵类 Matrix,定义其构造函数、 输入成员函数input和输岀成员函数 dis play , 重载运算符“ + ”,求两个矩阵的加法。”、“ <”和“ >”,用于两2. 定义一个字符串类Stri ng,用来存放不定长的字符串,重载运算符“ 个字符串的等于、小于和大于的比较运算。<<”和流提取符“ >> ”,使之能用于该矩3. 设计一个2行3列的矩阵类Matrix,重载流插入运算符 阵的输入和输岀。三、编程分析及运行结果1、#inelude <iostream.h> #i nclude <ioma nip .h&g
48、t; class Matrix p ublic:void inp ut();Matrix();void dis play();Matrix op erato 叶(Matrix & m);Matrix()private:float matrix23;void Matrix:i np ut() int i,j;cout<<"请输入矩阵数据:"for(i=0;i<2;i+)for(j=0;j<3;j+)ci n>> matrixij;Matrix:Matrix()int i,j;for(i=0;i<2;i+) for(j=0;j&
49、lt;3;j+) matrixij=0;void Matrix:dis play() int i,j;cout<<"矩阵为:n"for(i=0;i<2;i+) for(j=0;j<3;j+) cout<<setw(8)<<matrixij; if(j=2) cout<<e ndl;Matrix Matrix:o perato r +(Matrix &m) Matrix t; int i,j;for(i=0;i<2;i+)fora=0;j<3;j+)t.matrixij=matrixij+m.ma
50、trixij;return t; void mai n() Matrix M1,M2,M3;M1.i np ut();Ml.dis play();M2.i np ut();M2.dis play(); M3=M1+M2; cout<<"M1+M2"M3.dis pl ay();II I "F:C + +i:mDebugDebugiCppl23.e.蠶崇阵数据"124 5 请输入矩阵数据;5 矩阵为:53 M卄H2矩阵为:8 IM to106 continue612Press any keyl2、#includeviostream.h>#
51、in clude<stri ng.h>class Stri ngprivate:char str20;public:Stri ng();frie nd void op erator=(Stri ng &s1,Stri ng &s2); frie nd void op erator>(Stri ng &s1,Stri ng &s2);frie nd void op erator<(Stri ng &s1,Stri ng &s2); ;Stri ng:Stri ng()cout<<"输入字符串:"cin> >str;cout<<str<<e ndl;void op erator=(Stri ng &s1,Stri ng &s2)if(strc mp (s1.str,s2.str)=0)cout<<s1.str<<" 等于"<<s2.str<<
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 住宅商用装修合同范例
- 乡村开荒保洁合同标准文本
- 养生馆员工协议合同标准文本
- 供应商月结合同标准文本超市
- 个人分期还款合同标准文本
- 企业管理项目合同标准文本
- 中国总代理合同样本
- 全开发合同标准文本
- 乡村维修农具合同标准文本
- 中介使用合同标准文本
- 《民航客舱设备操作与管理》课件-项目一 飞机主要机型概述
- 一例甲状腺癌术后并发神经损伤患者的护理案例
- 2024年演出经纪人考试必背1000题附答案(黄金题型)
- 第11课《送东阳马生序》(课件)九年级语文下册
- 2024年中储粮集团招聘笔试参考题库附带答案详解
- 万达宝软件逻辑计算笔试题
- 专题16.7 二次根式章末八大题型总结(拔尖篇)-八年级数学下册(人教版)(解析版)
- 《养老护理员》-课件:协助卧床老年人使用便器排便
- 国家开放大学《JavaScript程序设计》复习指导参考答案
- 特种加工技术课件
- 禽屠宰兽医卫生检验人员岗位技能要求
评论
0/150
提交评论