VC++程序设计课内实验报告.doc_第1页
VC++程序设计课内实验报告.doc_第2页
VC++程序设计课内实验报告.doc_第3页
VC++程序设计课内实验报告.doc_第4页
VC++程序设计课内实验报告.doc_第5页
免费预览已结束,剩余17页可下载查看

下载本文档

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

文档简介

明德致远 笃行务实实 验 报 告课程名称 VC+程序设计 专业班级 电子0942 姓 名 学 号 电气与信息学院和谐 勤奋 求是 创新实验教学考核和成绩评定办法1 课内实验考核成绩,严格按照该课程教学大纲中明确规定的比重执行。实验成绩不合格者,不能参加课程考试,待补做合格后方能参加考试。2 单独设立的实验课考核按百分制评分,考核内容应包括基本理论、实验原理和实验。3 实验考核内容包括:1)实验预习;2)实验过程(包括实验操作、实验记录和实验态度、表现);3)实验报告;权重分别为0.2 、0.4 、 0.4;原则上根据上述三个方面进行综合评定。学生未取得1)和2)项成绩时,第3)项成绩无效。4 实验指导教师应严格按照考核内容分项给出评定成绩,并及时批改实验报告,给出综合成绩,反馈实验中出现的问题。实验成绩在教师手册中有记载。实验报告主要内容一 实验目的 二 实验仪器及设备三 实验原理四 实验步骤五 实验记录及原始记录六 数据处理及结论七 实验体会(可选项)注:1. 为了节省纸张,保护环境,便于保管实验报告,统一采用A4纸,实验报告建议双面打印(正文采用宋体五号字)或手写,右侧装订。2. 实验类别指验证、演示、综合、设计、创新(研究)、操作六种类型实验。3. 验证性实验:是指为了使学生巩固课程基本理论知识而开设的强调演示和证明,注重实验结果(事实、概念或理论)的实验。4. 综合性实验:是指实验内容涉及本课程的综合知识或本课程相关的课程知识的实验。5. 设计性实验:是指给定实验目的、要求和实验条件,由学生自行设计实验方案并加以实现的实验。实验题目实验五 构造函数与析构函数的编程实验室电信机房实验时间 年 月 日 实验类别验证同组人数1 成 绩指导教师签字:一、实验目的1进一步加深对类和对象的理解。2掌握类的构造函数和析构函数的概念、意义和使用方法。3掌握重载构造函数的含义和使用。4编写一个较为复杂的类和对象的应用程序。二、实验内容1设计一个程序,定义一个矩形类,包括数据成员和函数成员。要求有构造函数、析构函数,还有一个成员函数area( )用来计算并显示矩形的面积,并编写main函数进行测试。程序:# include# includeclass Rectpublic: Rect(int l, int w); Rect(); int Area(); private: int nLength; int nWidth; ;Rect:Rect(int l, int w)coutexecuting constructor.endl;nLength=l;nWidth=w;coutnLength=nLengthendlnWidth=nWidthendl;Rect:Rect()coutexecuting constructor.endl;coutnLength=nLengthendlnWidth=nWidthendl;int Rect:Area()int t;t=nLength*nWidth;return t;int main()Rect A(3,4);couts=A.Area()endl;return 0;运行结果:2重载构造函数。修改上题,一种构造函数用整型变量记录矩形的长和宽,另一种构造函数用double型记录矩形的长和宽,然后完成成员函数及主函数。程序:# include# includeclass Rectpublic: Rect(int l, int w);Rect(double u, double v); int Area();double Area(double u, double v); Rect(); private: int nLength; int nWidth; double mLength; double mWidth;Rect:Rect(int l, int w)coutexecuting constructor.endl;nLength=l;nWidth=w;coutnLength=nLengthendlnWidth=nWidthendl;Rect:Rect(double u, double v)coutexecuting constructor.endl;mLength=u;mWidth=v;coutmLength=mLengthendlmWidth=mWidthendl;Rect:Rect()coutexecuting constructor.endl;int Rect:Area()int t;t=nLength*nWidth;return t;double Rect:Area(double u, double v)double r;r=mLength*mWidth;return r;void main()Rect A(3,4);couta的面积=A.Area()endl;Rect B(22,33);coutb的面积=B.Area()endl;远行结果:3构造一个类countstr,要求用构造函数设置计数器count的初始值为0,成员函数countchar()不返回任何值,它要求用户输入一段文字,按Enter键后结束计算,用count记录输入的字符数,成员函数getchar()返回count的整数值。程序:#include class countstr public:countstr() count=0;void countchar() coutstr; while(strcount!=0) count+; int getchar() return count; private:int count;char str200; ; void main() countstr s;s.countchar(); coutthe number=s.getchar()endl;实验题目实验六 利用友元编程实验室电信机房实验时间 年 月 日 实验类别验证同组人数1 成 绩指导教师签字:一、实验目的1掌握友元函数和友元类的概念、定义和作用。2会使用友元编程。3进一步学习内存空间动态分配和释放的方法。二、实验内容1有一个向量类Vector,包括一个点的坐标位置x和y,设计两个友元函数,实现两个向量的加法和减法运算。2定义一个由y=ax+b确定的直线类Line,该类的构造函数初始化直线,成员函数Print显示该直线方程,友元函数SetPoint()求解两条直线的交点。3. 下列程序是有关友元类的,程序中将A类声明为B类的友元类,A类中所有的成员函数都是B类的友元函数,请给程序填空。 #inlcude class B; class A private: int x; public: A(int xx)x=xx; int Set(B&); int Get()return x; ; class B private: int x; public: B(int xx) friend ;int A:Set( ) return x=b.x;void main() A a(10); B b(20); couta.Get()endl;a.Set(b); couta.Get()endl;三、编程分析及运行结果1、#include class Vector public: Vector(double m=0,double n=0)x=m;y=n; void display() cout(x,y)endl; friend Vector operator+(Vector &a,Vector &b) Vector t; t.x=a.x+b.x; t.y=a.y+b.y; return t; friend Vector operator-(Vector &a,Vector &b) Vector t; t.x=a.x-b.x;t.y=a.y-b.y; return t; Vector() private: double x,y;void main() Vector a1(8,6),a2(1,2.1),a3,a4; cout向量a1=; a1.display(); cout向量a2=; a2.display(); a3=a1+a2; couta1+a2=; a3.display(); a4=a1-a2; couta1-a2=; a4.display();2、#include class Line public: Line(double m=0,double n=0)a=m;b=n; void print() cout直线y=ax+b;endl; void friend setpoint(Line &l1,Line &l2) 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; Line() private: double a,b;void main() Line L1(3,5),L2(-2,6); L1.print(); L2.print(); setpoint(L1,L2); 3、填空后程序:#include class B; class A private: int x; public: A(int xx)x=xx; int Set(B &); int Get()return x; ; class B private: int x; public: B(int xx)x=xx; friend class A; ;int A:Set(B &b) return x=b.x;void main() A a(12); B b(34); couta.Get()endl; a.Set(b); couta.Get()endl;四、解决方法及难点 难点:函数重载时,注意重载的实现方法:通过形参类型或个数来区分调用哪个函数。实验题目实验七 继承与派生的编程实验室电信机房实验时间 年 月 日 实验类别验证同组人数1 成 绩指导教师签字:一、 实验目的1了解继承在面向对象程序设计中的重要作用。2理解继承与派生的概念,掌握单继承和多继承的定义方法。3熟悉公有派生和私有派生的访问特性。4掌握继承中基类和派生类的构造函数定义和调用过程。5了解虚基类的作用和用法,会使用虚基类,学习虚基类在解决二义性问题中的作用二、 实验内容1有以下程序,请完成下面的工作:1)阅读程序,写出运行后的输出结果;2)然后上机运行,验证结果是否正确;3)分析程序执行过程,尤其是调用构造函数和析构函数的过程。#include class Apublic: A()cout“constructing A”endl; A()cout“destructing A”endl;class B:public Apublic: B()cout“constructing B”endl; B()cout“destructing B”endl;class C:public Bpublic: C()cout“constructing C”endl; C()cout“destructing C”endl;void main() C c1; 2定义一个Point类,派生出Rectangle类和Circle类,计算各派生类对象的面积Area( )。编写一个完整程序进行测试。3. 定义并描述一个人员类Person,它派生出学生类Student和教师类Teacher, 学生类和教师类共同派生出在职读书的教师类Stu_Tech。人员类有姓名、性别、身份证号、出生年月等信息;学生类有学号、成绩等信息;教师类有职务、职称等信息。编写一个完整程序进行测试。三、 编程分析及运行结果1、运行结果:2、class Point public: void inpoint(double x,double y) Px=x; Py=y; double getPx() return Px; double getPy() return Py;protected:double Px,Py;class Rectangle:public Point public:Rectangle(double x,double y,double h,double w) inpoint(x,y); High=h; Wide=w;double getHigh()return High;double getWide()return Wide;double Area() return High*Wide;protected:double High,Wide;class Circle:public Point public:Circle(double x,double y,double r) inpoint(x,y); R=r;double getR() return R;double Area() double pi=3.1415926; return pi*R*R; protected:double R;#include void main() double x,y,h,w,r; coutxyhw; Rectangle Rect1(x,y,h,w); cout矩形的数据:endl坐标(Rect1.getPx(), Rect1.getPy(),theigh=Rect1.getHigh(),twidth= Rect1.getWide(),tArea=Rect1.Area()endl; coutxyr; Circle C1(x,y,r); cout圆的数据:endl坐标(C1.getPx(),C1.getPy() ),tR=C1.getR()endl;3、#include #include class Person public: Person(); Person(char *name1,char sex1,char *id1,char *birth);void display(); Person();protected:char *name;char *id;char sex;char *birthday;Person:Person() name=0; sex=0; id=0; birthday=0;Person:Person(char *name1,char sex1,char *id1,char *birth) name=new charstrlen(name1)+1; name=strcpy(name,name1); sex=sex1; id=new charstrlen(id1)+1; id=strcpy(id,id1); birthday=new charstrlen(birth)+1; birthday=strcpy(birthday,birth);void Person:display() coutname:namensex:sexnid:idnbirthday:birthdayendl;Person:Person() deletename; deleteid; deletebirthday;class Student:virtual public Person public: Student(char *name,char sex,char *id,char *birthday,long int s_num1,float score1); void display() Person:display(); couts_number:s_numnscore:scoreendl; Student()protected:long int s_num;float score;Student:Student(char *name1,char sex1,char *id1,char *birth,long int s_num1,float score1):Person(name1,sex1,id1,birth) s_num=s_num1; score=score1;class Teacher:virtual public Person public:Teacher(char *name1,char sex1,char *id1,char *birth,char *posit,char *title1):Person(name1,sex1,id1,birth) position=new charstrlen(posit)+1; position=strcpy(position,posit);title=new charstrlen(title1)+1;title=strcpy(title,title1); void display() Person:display(); coutPosition:positionnTitle:titleendl; Teacher() deleteposition; deletetitle; protected:char *position;char *title;class Stu_teach:public Student,public Teacher public: Stu_teach(char *name1,char sex1,char *id1,char *birth,long int s_num1,float score1,char *posit,char *title1):Person(name1,sex1,id1,birth), Student(name1,sex1,id1,birth,s_num1,score1),Teacher(name1,sex1,id,birth,posit,title1) void display(); Stu_teach();void Stu_teach:display() Student:display(); coutPosition:positionnTitle:titleendl; void main() Student s1(谢建平,M1990-06-06,904451226,97.5); Teacher t1(朱亚梅,M1978-06-23,English teacher,Professor); Stu_teach s_t1(刘国松,F1980-1-13,904451208,96.0,physical teacher,Professor); s1.display(); t1.display(); s_t1.display();四、解决方法及难点 难点:用虚基类解决继承和派生时的二义性问题.通过虚基类来实现.实验八 利用虚函数编程一、实验目的1了解多态性的概念和虚函数的作用及使用方法。2了解静态关联和动态关联的概念和用法。3了解纯虚函数和抽象类的概念和用法。4学习使用虚函数的继承实现动态关联。5学习理解静态数据成员和静态成员函数的使用。二、实验内容1事先阅读程序,给出其运行结果,上机验证虚函数的作用。 有如下程序: #include #include class Student /定义基类public: Student(int,string,float); void display(); /声明输出函数 protected: /受保护成员,派生类可访问int num;string name;float score; ;Student :Student(int n,string nam,float s)num=n;name=nam;score=s;void Student:display()coutnum:numnname:namenscore:scoreendlendl;class Graduate:public Student /声明公有派生类public: Graduate(int,string,float,flaot); void display(); /声明输出函数 private: float pay;Graduate:Graduate(int n,string nam,float s,flaot p):Student(n,nam,s),pay(p)void Graduate:display()coutnum:numnname:namenscore:scorenpay:paydisplay(); pt=&grad; pt-display();1)分析结果,并验证之。2)利用虚函数,对程序作一点修改, 在 Student类中声明display函数时,在其前面加上关键字virtual,即 virtual void display();,再编译运行,注意分析运行结果。2事先编写一个程序,计算正方体、球体和圆柱体的表面积和体积。(提示:声明一个抽象基类 container,派生类cube、sphere和cylinder,基类中求表面积和体积的成员函数声明为纯虚函数。)3定义一个类Student记录学生计算机课程的成绩,要求使用静态成员变量或静态成员函数计算全班计算机课程的总成绩和平均成绩。三、编程分析及运行结果1、2、#include const double pi=3.1415;class container public: virtual void display() virtual void Area() virtual void Volume();class cube:public containerpublic: cube(double a)Length=a; void display()coutcube:nLength=Lengthendl; void Area()coutArea=6*Length*Lengthendl; void Volume()coutVolume=Length*Length*Lengthendl; cube()protected:double Length;class sphere:public container public: sphere(double r)R=r;void display()coutsphere:nR=Rendl; void Area()coutArea=4*pi*R*Rendl; void Volume()coutVolume=4.0/3*pi*R*R*Rendl;sphere()protected:double R;class cylinder public: cylinder(double r,double h)R=r;H=h; void display()coutcylinder:nR=R,thight=Hendl; void Area()coutArea=2*pi*R*R+2*pi*R*Hendl; void Volume()coutVolume=pi*R*R*Hendl;cylinder()protected:double R,H;void main() cube cu1(5); cu1.display(); cu1.Area(); cu1.Volume(); sphere s1(6); s1.display(); s1.Area(); s1.Volume(); cylinder cy1(3,8); cy1.display(); cy1.Area(); cy1.Volume();3、#include #include class Student public: Student(long,char *,float); void display(); Student()deletename;protected:static float average;static int sum;long num; char *name; float score; ;float Student:average=0;int Student:sum=0;Student:Student(long n,char *nam,float s) num=n; name=new charstrlen(nam)+1; name=strcpy(name,nam); score=s; average=(average*sum+s)/(sum+1); sum+;void Student:display()coutNumber:numnName:namenScore:scorenaverage:averageendl;void main() Student stu1(904451226, xie,87.5),stu2(904451223, Wang,98.5),stu3(904451220,shen,96); stu1.display(); stu2.display(); stu3.display();四、 解决方法及难点 难点:实现函数多态性时虚函数与抽象基类的定义与运用; 用静态数据成员变量体现类中的总体情况。实验九 运算符重载编程一、实验目的1了解运算符重载的概念和使用方法2掌握几种常见的运算符重载的方法,通过编程实践,理解运算符重载的意义。3学习内存空间动态分配和释放的方法。4了解在Visual C+6.0环境下进行运算符重载要注意的问题。二、实验内容1设计一个2行3列的矩阵类Matrix,定义其构造函数、输入成员函数input和输出成员函数display,重载运算符“+”,求两个矩阵的加法。2定义一个字符串类String,用来存放不定长的字符串,重载运算符“= =”、“”,用于两个字符串的等于、小于和大于的比较运算。3 设计一个2行3列的矩阵类Matrix,重载流插入运算符 “”,使之能用于该矩阵的输入和输出。三、编程分析及运行结果1、#include #include class Matrix public: void input(); Matrix();void display();Matrix operator+(Matrix &m);Matrix() private: float matrix23; void Matrix:input() int i,j; cout请输入矩阵数据:; for(i=0;i2;i+) for(j=0;jmatrixij;Matrix:Matrix()int i,j; for(i=0;i2;i+)for(j=0;j3;j+)matrixij=0;void Matrix:display() int i,j; cout矩阵为:n; for(i=0;i2;i

温馨提示

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

评论

0/150

提交评论