版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、内蒙古财经学院程序设计基础下课程实验指导书计算机信息管理系2008年3月第一部分 说明 程序设计基础下的上机实习分为课内上机实习和课外上机实习两部分,课内上机实习学期内共安排54学时,每周安排一次,平均每次约3节课,即学期内共安排36次课内上机实习,由机房统一安排,任课老师跟班辅导。学期内还安排18学时的课外上机实习,主要作为课内上机实习的补充,学生用来完成在课内上机实习中没有作完的工作,由学生自己安排上机时间。第二部分 实验要求及实验内容实验一 类和对象实验目的:学习类的声明方法;掌握类成员函数的定义形式;掌握多文件结构C+程序的组织形式。实验内容:一、 阅读下列程序,找出程序中的错误,修改
2、错误并上机验证。#include#includeclass Pointpublic:void Set(double ix,double iy) x=ix; y=iy; double xOffset()return x;double yOffset()return y;double angle()return (180/3.14159)*atan2(y,x);double radius()return sqrt(x*x+y*y);protected:double x;double y;void main()Point p;double x,y;coutxy;p.Set(x,y);p.x+=5;p
3、.y+=6;coutangle=p.angle(),radius=p.radius(),xoffset=p.xOffset(),yoffset=p.yOffset()endl;二、 编程实现设计一个长方体类Box,它能计算并输出长方体的体积和表面积。三、 定义一个时间类 Time,能提供和设置由时、分、秒组成的时间,并编出应用程序,定义时间对象,设置时间,输出该对象提供的时间。并将类定义作为接口,用多文件结构实现之。 (2)、在该类中包含一个带默认形参值的成员函数,即设置时间的函数setTime()。 (3)、以内联形式定义一个带默认形参值的函数setTime1()。四、 请写出在实验过程中遇
4、到的问题?你是如何解决的?目前仍存在什么问题? 实验二 类和构造函数和析构函数实验目的:理解对象的概念,掌握对象的定义和使用方法;学习构造函数和析构函数的作用、特点和使用方法。实验内容:一、 写出下列程序的运行结果并上机验证。#includeclass Samplepublic: Sample()x=y=1; Sample(int a,int b) x=a; y=b; Sample() if(x=y) coutx=yendl; else coutx!=yendl; void disp() coutx=x,y=yendl; private: int x,y;void main() Sample
5、s1(2,3),s2; s2.disp(); s1.disp(); coutendl;二、 写出下列程序的运行结果并上机验证。#includeclass A int a,b; public: A(int i,int j); void printA();class B A c; int n; public: B(int i,int j,int k); void printB();B:B(int i,int j,int k):c(i,j) n=k;void B:printB() coutn=n,; c.printA();A:A(int i,int j) a=i;b=j; void A:printA
6、() couta=a,b=bendl;void main() B m(7,8,2); m.printB();三、 编程实现定义一个满足如下要求的Date 类。1. 用下面的格式输出日期 ; 日/月/年 2. 可实现在日期上加一天的操作; 3. 设置日期。 4. 使用初始化列表建立类的构造函数; 5. 建立它的带默认参数值的构造函数;四、 请写出在实验过程中遇到的问题?你是如何解决的?目前仍存在什么问题?实验三 类和拷贝构造函数实验目的:掌握拷贝构造函数的作用和使用方法,切实体会在什么情况下需要提供拷贝构造函数来实现深拷贝的功能。实验内容:一、 写出下列程序的运行结果并上机验证 #include
7、 class Sample int x; public: Sample(); Sample(int a)x=a; Sample(Sample &a) x=a.x+10; void disp(char *ob) coutobx=xendl; ;void main() Sample s1(2),s2(s1),s3; s3=s2; s1.disp(s1.); s2.disp(s2.); s3.disp(s3.);二、 写出下列程序的运行结果并上机验证,程序运行时会出现什么问题?请用增加拷贝构造函数的方法避免存在的问题。#include#includeclass Vectorpublic:Vector
8、(int s=100);Vector(Vector &);int& Elem(int ndx);void Display();void Set();Vector();protected:int size;int *buffer;Vector:Vector(int s)buffer=new intsize=s;for(int i=0;isize;i+)bufferi=i*i;int& Vector:Elem(int ndx)if(ndx=size)couterror in indexendl; exit(1);return bufferndx;void Vector:Display()for(i
9、nt j=0;jsize;j+)coutbufferj ;coutendl;void Vector:Set()for(int j=0;jsize;j+)bufferj=j+1;Vector:Vector()deletebuffer;Vector:Vector(Vector &ss)buffer=new intsize=ss.size;for(int i=0;isize;i+)bufferi=ss.bufferi;void main()Vector a(10);a.Display();Vector b(a);a.Set();a.Display();b.Display();三、 编程实现以下功能1
10、. 创建一个Employee类,该类中有字符数组,表示姓名、街道地址、市、省和邮政编码。把表示构造函数、ChangeName()、Display()的函数原型放在类定义中,构造函数初始化每个成员,Display()函数把完整的对象数据打印出来。其中的数据成员是保护的,函数是公共的。2. 修改上面的类,将姓名构成类Name,其名和姓在Name 类中是保护数据成员,其构造函数为接收一个指向完整姓名字符串的指针,其Display()函数输出姓名。然后将Employee类中的姓名成员换成Name 类的对象。3. 根据上面的类定义,编制主函数如下,构成一个完整的多文件程序。void mian()Empl
11、oyee em(“MarkBrooks”,”5 West St.”,”Revere”,”CA”,”12290”);em.Display();em.ChangeName(“Richard Voss”);em.Display();四、请写出在实验过程中遇到的问题?你是如何解决的?目前仍存在什么问题? 实验四 静态成员与友员实验目的:理解类的静态数据成员的特性及使用方法;类的静态成员函数与普通成员函数的不同点;掌握友元的定义和使用方法。实验内容:一、 写出下列程序的运行结果并上机验证。 #includeclass Point private: int X,Y; static int countP;
12、public: Point(int xx=0,int yy=0) X=xx; Y=yy; countP+; Point(Point &p); int GetX()return X; int GetY()return Y; static void GetC() coutObject id=countPendl; ;Point:Point(Point &p) X=p.X; Y=p.Y; countP+;int Point:countP=0;void main() Point:GetC(); Point A(4,5); coutPoint A:(A.GetX(),A.GetY() ; A.GetC(
13、); Point B(A); coutPoint B:(B.GetX(),B.GetY() ; Point:GetC();二、给出程序的运行结果#include using namespace std; int Average(int i) static int sum = 0, count = 0; sum = sum + i; count+; return sum / count; int main() int num; /局部变量,具有动态生存期do cout num; if(num != -1) cout Running average is: Average(num); cout -
14、1); return 0; 三、阅读下列程序,完成以下两项要求1. 找出程序中的错误;2. 使用增加公有成员函数和友元函数两种方法来改正错误。#includeclass Animal;void SetValue(Animal&,int);void SetValue(Animal&,int,int);class Animalpublic:friend void SetValue(Animal&, int);void disp()coutitsWeight=itsWeight itsAge=itsAgeSety(99); 和p-Gety()代替?#includeclass base int x;
15、public: void Setx(int i)x=i; int Getx()return x;class Derived:public base int y; public: void Sety(int i)y=i; int Gety()return y;void main() base *p; base b_obj; Derived d_obj; p=&b_obj; p-Setx(11); coutbase object x:Getx()Setx(55); coutDerived object x:Getx()endl; d_obj.Sety(99); coutDerived object
16、 y:d_obj.Gety()endl;二、 写出下列程序的运行结果并上机验证。#includeclass person public: person()coutperson class constructor.endl; person()coutperson class destructor.endl; protected: char *name; int age; char *add;class student:public person char *department; int level; public: student()coutstudent class constructor.
17、endl; student()coutstudent class destructor.endl;class teacher:public person char *major; float salary; public: teacher()coutteacher class constructor.endl; teacher()coutteacher class destructor.endl;void main() student s1; teacher t1;三、改正程序后给出结果#include using namespace std; class Baseint x,y;/私有数据成
18、员protected:int a,b; /保护数据成员public:int m,n; /公有数据成员void SetX(int X) x=X;/为私有数据成员设置接口void SetY(int Y) y=Y;/为私有数据成员设置接口int GetX()return x;/为私有数据成员设置接口int GetY()return y;/为私有数据成员设置接口;class Derived:protected Basepublic:void SetXY(int X, int Y) SetX(X); SetY(Y);/语句1:使用基类接口函数设置数据void SetAB(int A, int B) a=
19、A; b=B;/派生类可以直接访问基类的保护数据void SetMN(int M, int N) m=M; n=N; /派生类可以直接访问基类的公有数据int GetSumXY() return (GetX()+GetY();/语句2:使用基类接口函数提取数据int GetSumAB() return (a+b);/派生类可以直接访问基类的保护数据int GetSumMN() return (m+n);/派生类可以直接访问基类的公有数据;int main() Derived objD;objD.SetXY(1,2);objD.SetAB(10,20);objD.SetMN(100,200);c
20、outX+Y=objD.x+objD.yendl; /派生类对象不能直接访问私有数据coutA+B=objD.a+objD.bendl; /派生类对象不能直接访问保护数据coutM+N=objD.m+objD.nendl; /派生类对象不能直接访问公有数据return 0;四、试给出下列题目的运行结果#include #include using namespace std; class TwoDShape / 二维图形类 double width,height; public: TwoDShape(double w, double h) width = w; height = h; /构造函
21、数.void showDim() cout Width and height are width and height n; double getWidth() return width; double getHeight() return height; void setWidth(double w) width = w; void setHeight(double h) height = h; ; class Triangle : public TwoDShape / Triangle公有派生 TwoDShape. char style20; / 私有成员public: Triangle(
22、char *str, double w, double h) : TwoDShape(w, h) strcpy(style, str); double area() return getWidth() * getHeight() / 2; void showStyle() cout Triangle is style n; ; class CTriangle : public Triangle / 扩展 Triangle. char color20; public: CTriangle(char *clr, char *style, double w, double h) : Triangle
23、(style, w, h) strcpy(color, clr); void showColor() cout Color is color n; / 显示颜色.; int main() CTriangle ct(Blue, right, 7.0, 12.0); cout Info for CTriangle: ; ct.showStyle(); ct.showDim(); ct.showColor(); cout Area is ct.area() endl; return 0; 五、程序,声明一个Shape基类,再派生出 Rectange类和Circle 类,二者都有GetArea() 函
24、数计算对象的面积。六、写出在实验过程中遇到的问题?你是如何解决的?目前仍存在什么问题?实验六 类的多重继承实验目的:掌握多重继承时派生类对象的构造次序;理解虚拟继承的需要和实现方法;掌握三种继承方式对派生类的影响。实验内容: 一、 写出下列程序的运行结果并上机验证 #includeclass base1 public: int n; void fun()coutThis is base1,n=nendl;class base2 public: int n; void fun()coutThis is base2,n=nendl;class Derive:public base1,public
25、base2 public: int n; void fun()coutThis is Derive,n=nendl;void main() Derive obj; obj.n=1; obj.fun(); obj.base1:n=2; obj.base1:fun(); obj.bsse2:n=3; obj.base2:fun();二、 写出下列程序的运行结果并上机验证。 #includeclass base public: base()coutthis is base class!endl;class base1 public: base1()coutthis is base1 class!en
26、dl;class level1:public base1,virtual public base public: level1()coutthis is level1 class!endl;class level2:public base1,virtual public base public: level2()coutthis is level2 class!endl;class toplevel:public level1,virtual public level2 public: toplevel()coutthis is toplevel class!endl;void main()
27、toplevel topobj; return;三、试给出下列题目的运行结果#include using namespace std; class Base1public:Base1(void)coutBase1 constructor calledendl;Base1(void)coutBase1 destructor calledendl;class Base2public:Base2(void)coutBase2 constructor calledendl;Base2(void)coutBase2 destructor calledendl;class Derived:public B
28、ase1,public Base2public:Derived(void):Base1(),Base2()coutDerived constructor calledendl;Derived(void)coutDerived destructor calledendl;int main()Derived objD;/块作用域1开始Base1 objB1;/块作用域2开始Base2 objB2;/块作用域2结束/块作用域1结束return 0;四、试给出下列题目的运行结果#include using namespace std; class Base1public:Base1(void)cout
29、Base1 constructor calledendl;Base1(void)coutBase1 destructor calledendl;class Base2public:Base2(void)coutBase2 constructor calledendl;Base2(void)coutBase2 destructor calledendl;class Base3public:Base3(void)coutBase3 constructor calledendl;Base3(void)coutBase3 destructor calledendl;class Derived:publ
30、ic Base2,public Base1,public Base3Base3 member3;Base1 member1;Base2 member2;public:Derived(void)coutDerived constructor calledendl;Derived(void)coutDerived destructor calledendl;int main()Derived objD;return 0;五、声明一个圆类 Circle 和一个桌子类 Table,另外声明一个圆桌类 RoundTable,它是由 Circle 和Table两个类派生的,要求声明一个圆桌类的对象,并输出
31、圆桌的高度、面积和颜色。六、 在实验过程中遇到的问题?你是如何解决的?目前仍存在什么问题?实验七 多态实验目的:理解多态的重要性; 实验内容:一、写出下列程序的运行结果并上机验证。#includeclass point private: int x,y; public: point() point(int xx,int yy)x=xx; y=yy; point operator ,(point r) point t; t.x=r.x; t.y=r.y; return t; point operator +(point r) point t; t.x=x+r.x; t.y=y+r.y; retu
32、rn t; void disp() coutarea:x*yendl; ;void main() point p1(1,2),p2(3,4),p3(5,6); p1.disp(); p2.disp(); p3.disp(); p1=(p1,p2+p3,p3); p1.disp();二、试给出下列题目的运行结果#include using namespace std; class Base1int b1;public:Base1(int i)b1=i;coutBase1 constructor called endl;void disp() coutBase1: b1=b1endl;class
33、Base2int b2;public:Base2(int i)b2=i;coutBase2 constructor called endl;void disp() coutBase2: b2=b2endl;class Derived:public Base2,public Base1int d;public:Derived(int i,int a,int b):Base1(a),Base2(b)d=i;coutDerived constructor called endl;void disp() coutDerived: d=dendl;int main()Derived objD(1,2,3
34、);objD.disp();/语句1:同名隐藏objD.Base1:disp();/语句2:使用作用域运算符:解决二义性问题objD.Base2:disp();/语句3:使用作用域运算符:解决二义性问题return 0;三、试给出下列题目的运行结果#include using namespace std; class Baseprotected:int b;public:Base(int i) b=i; coutBase constructor called endl; void disp() coutBase: b=bendl;class ExtBase:public Baseint eb;
35、public:ExtBase(int i,int j):Base(j) eb=i; coutExtBase constructor called endl;void disp() coutExtBase: eb=ebendl;class Derived:public ExtBaseint d;public:Derived(int i,int a):ExtBase(i,a) d=i; coutDerived constructor called endl;void disp() coutDerived: d=ddisp();/语句5:可以将派生类的对象的地址赋给基类指针Base *pb=&obj
36、D; pb-disp();return 0; 四、定义一个哺乳动物Mammal类,再由哺乳动物派生狗Dog类。问题: 观察构造函数和析构函数的调用顺序。 观察虚函数Evolve()的工作状态。/ 源程序代码如下:#include enum myColorBLACK,WHITE;class Mammal/声明基类Mammalprotected:int itsAge;int itsWeight;public:/外部接口/构造函数Mammal();Mammal();/访问数据int GetAge()constreturn itsAge;void SetAge(int age)itsAge = age
37、;int GetWeight()constreturn itsWeight;void SetWeight(int weight)itsWeight = weight;void Speak()constcoutMammal sound!endl;virtual void Evolve()constcoutIt first be a Mammal.and then endl;class Dog:public Mammal/声明基类Dogprotected:myColor itsColor;public:/外部接口/构造函数Dog();Dog();/访问数据myColor GetColor()con
38、streturn itsColor;void SetColor(myColor color)itsColor = color;void WagTail()coutTail Waged.endl;virtual void Evolve()constMammal:Evolve();/为了演示Dog完整的进化过程,要调用Mammal的Evolve()函数。用:指定代码/所在的区域,而无须在Dog中创建Mammal对象来实现对其函数的调用。couta dog.and then endl;Mammal:Mammal():itsAge(1),itsWeight(5)coutMammal construct
39、or.endl;Mammal:Mammal()coutMammal destructor.endl;Dog:Dog():itsColor(WHITE)coutDog constructor.endl;Dog:Dog()coutDog destructor.endl;void main()Dog Jack;coutHow Dog Evolve .endl;Jack.Evolve();/由于Evolve()是虚函数,所以调用最近派生的类的函数。coutendl;Jack.Speak();/由于派生类对象指针与基类的指针是一样的,所以这里调用基类Mammal的函数。Jack.WagTail();/由于基类无该函数,因此调用最靠近基类的派生类的函数。coutJack is Jack.GetAge() years oldend
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2026年浙江省诸暨市高二生物下册期末考试模拟试卷附参考答案【基础题】
- 山西省交通安全常识测试卷及答案
- 监理工程师考试建设工程质量管理知识点巩固习题及答案
- 湖南省长沙市一中高二地理自然地理单元测试卷及答案
- 2026年部编版初中英语写作专项训练习题及答案
- 2026年药学法律法规与伦理知识测试卷及答案
- 信息技术支持与服务手册
- 企业销售管理与营销策略手册
- 产品经理主管工作手册
- 商业数据分析与报告编写手册(标准版)-1
- GB 48145-2026井工煤矿机电设备完好性要求
- 2026-2027学年四年级上册英语基础过关第一次月考试卷
- 新版2026秋新北师大版数学五年级上册全册教案教学设计含综合实践合集
- 《南泥湾》教案2026-2027学年湘艺版六年级上册音乐
- 2026年呼吸与睡眠医学考试试题及答案
- 2026年注册电气工程师考试《电力系统分析》历年真题汇编
- 雨课堂学堂在线学堂云《人工智能与创新(南开)》单元测试考核答案
- 激光冷却技术进展-洞察及研究
- LATP复合固态电解质的制备工艺与电化学性能的深度剖析
- 完全平方公式说课课件
- 2025年国家能源笔试题及答案
评论
0/150
提交评论