C++语言程序设计.doc_第1页
C++语言程序设计.doc_第2页
C++语言程序设计.doc_第3页
C++语言程序设计.doc_第4页
C++语言程序设计.doc_第5页
已阅读5页,还剩28页未读 继续免费阅读

下载本文档

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

文档简介

计算机科学与技术系上机实验报告课 程:c+语言程序设计老 师:姓 名:(老挝留学生)班 级:计科 101 班学 号:学 院:计算机科学与信息学院 实验日期: 年 月 日实验一一、实验名称类和对象二、实验目的及要求设计一个类,并对其属性进行操作。三、实验环境microsoft visual studio 2010四、实验内容1,定义一个dog类,包含age,weight等属性。以及对这些属性的操作方法。实现并测试这个类。2,设计一个rectangle类,其属性为矩形的左下角与右上角的坐标,根据坐标计算矩形的面积。五、算法描述及实验步骤dog+dog(n: string , ag: int ,we: int)+get()+show()+dog()-name: string-weight: int-age: intrectangle+get(): void+show(): void-x1:int-x2: int-y1: int-y2: int六、调试过程及实验结果1, 保存源程序代码,并声称解决方案。2,调试并执行。3,输出为:the message of dog is:name:tutu age:2 weight:20input the name age and weight of dog花花 3 60the message of dog is:name:花花 age:3 weight:60the message of dog is:name:hua age:4 weight:60calledcalled请按任意键继续. . .输入左下角的坐标:3 6输入右上角的坐标:4 7两点的坐标左下角的坐标:(3,6)右上角的坐标:(4,7)面积为:1输入左下角的坐标:1 0输入右上角的坐标:2 6两点的坐标左下角的坐标:(1,0)右上角的坐标:(2,6)面积为:6请按任意键继续. . .七、总结1,构造函数用于对对象的初始化,在定义类时,如果没有定义构造函数,系统将自动生成一个简单的构造函数。2,构造函数没有返回值,不允许显示调用,创建对象时,系统将自动调用相应的构造函数。3,析构函数是对对象进行最后的清理工作,它不允许有参数,没有返回值。如果没有定义析构函数,系统将自动生成。4,对象的私有成员,可以通过成员函数访问,类外不能访问对象的私有成员。八、附录1, dog.h#include#includeusing namespace std;class dogpublic:dog(string n,int ag,int we);void get();void show();dog()coutcalled endl;private:string name;int weight;int age;dog.cpp#include4-8.hdog:dog(string n,int ag,int we):name(n),age(ag),weight(we)void dog:get()coutinput the name age and weight of dognameageweight;void dog:show()coutthe message of dog is:endl;coutname:name age:”age” weight:”weightendl;dogmain.cpp#include4-8.hint main()dog dog(tutu,2,20);dog.show();dog.get();dog.show();dog dog1(hua,4,60);dog1.show();return 0;2, rectangle.h#include#includeusing namespace std;class rectanglepublic:void get();void show();private:int x1,x2,y1,y2;rectangle.cpp#include4-9.hvoid rectangle:get()cout输入左下角的坐标:x1y1;cout输入右上角的坐标:x2y2;void rectangle:show()cout两点的坐标endl;cout左下角的坐标:(x1,y1)endl;cout右上角的坐标:(x2,y2)endl;cout面积为:(x2-x1)*(y2-y1)endl;rectangle main.cpp#include4-9.hint main()rectangle r1;r1.get();r1.show();rectangle r2;r2.get();r2.show();return 0;实验二一、实验名称类与对象二、实验目的及要求熟悉设计简单类及测试的方法。三、实验环境microsoft visual studio 2010四、实验内容1,设计一个人事管理类,其属性有编号、性别、出生年月、身份证号等其中出生年月声明为一个日期类内嵌子对象。实现对成员信息的录入和输出。要求包括:构造函数析构函数、复制构造函数、内联成员函数、带默认形参值的成员函数、类的组合。2,定义一个复数类complex,是下面的程序代码能够工作:complex c1(3,5); /用复数5+3i初始化c1complex c2=4.5; /用实数4.5初始化c2c1.add(c2); /将c1与c2相加,结果保留在c1中c1.show(); /将c1输出,结果应为7.5+5i五、算法描述及实验步骤1,date+date(y:int=0,m:int=0,d:int=0)+get():void+show():void+date()-year:int-month:int -day:inthumain+humain()+humain(p: &humain)+get(p :&humain):void+show(p:&humain):void-berthday:date-name:string-cd:string-six:string-number:string2,complex+complex(a:float=0,b:float=0)+add(p:&complex):void+show(p:&complex):void-real:float-imag:float六、调试过程及实验结果1, 保存源程序代码,并声称解决方案。2,调试并执行。3,输出为:the first one before setname:six:cd:number:berthday:0/0/0the first one after setinput the name six cd and number:name:喻福松six:男cd:5210452015number:1008060028input berthday:input year month and day1990 5 6name:喻福松six:男cd:5210452015number:1008060028berthday:1990/5/6p2name:喻福松six:男cd:5210452015number:1008060028berthday:1990/5/6called datecalled date请按任意键继续. . . number:7.5+5i请按任意键继续.七、总结1,类的组合 当当前类所使用的类还没有定义时,在当前类中只能建立此类的引用。如果已经定义在前,就可以直接建立对象。2,访问子对象中的可访问成员用“当前类子对象成员”的形式。3,内联成员函数:用关键字“inline”声明,在程序调用内联成员函数时,并不真正执行函数的调用过程,如保留返回地址等处理,而是把函数代码嵌入程序的调用点,这样可以减少调用成员函数的时间。4,带默认参数的函数: 由于函数调用时,实参和形参的结合是从左到右顺序进行的,因此指定默认值的参数必须放在参数列表的最右端。八、附录1,humainh 文件#include#includeusing namespace std;class datepublic:date(int y=0,int m=0,int d=0);void get ();void show();date()cout called dateendl;private:int year;int month;int day;lass humainpublic:humain();humain(humain &p);void get(humain &p);void show(humain &p);private:date berthday;string name;string cd;string six;string number;humaincpp文件#include4-10.hdate:date(int y,int m,int d):year(y),month(m),day(d)void inline date:get() coutinput year month and dayyearmonthday;void date:show() coutyear/month/dayendl;humain:humain()name= ;cd= ;six= ;number=;humain:humain(humain &p)berthday=p.berthday;name=;cd=p.cd;six=p.six;number=p.number;void humain:get(humain &p)coutinput the name six cd and number:endl;coutname;coutsix;coutcd;coutnumber;coutinput berthday:;p.berthday.get();void humain:show(humain &p)coutname:nameendl;cout six:sixendlcd:cdendlnumber:numberendl;coutberthday:;p.berthday.show();humain maincpp文件#include4-10.hint main()humain p1;coutthe frist one befor setendl;p1.show(p1);coutthe frist one after setendl;p1.get(p1);p1.show(p1);humain p2(p1);coutp2endl;p2.show(p2);return 0;2,complexh文件#includeusing namespace std;class complexpublic:complex(float a=0,float b=0);void add(complex &p);void show();private:float real;float imag;complexcpp文件#include4-20.hcomplex:complex(float a,float b):real(a),imag(b)void complex:add(complex &p)real=real+p.real;imag=imag+p.imag;void complex:show()coutreal+imagiendl;complex maincpp文件#include4-20.hint main()complex c1(3,5);complex c2=4.5;c1.add(c2);c1.show();return 0;实验三一、实验名称数据的共享与保护二、实验目的及要求掌握对静态成员、静态函数、友元函数的使用方法。三、实验环境microsoft visual studio 2010四、实验内容1,定义一个cat类,拥有静态成员numofcat,记录cat的个体数目;静态成员函数getnumofcat(),读取numofcat。2,定义boat与car两个类,二者都有weight属性,定义二者的友元函数gettoalweight(),计算二者的重量和。五、算法描述及实验步骤1,cat+cat()+get(): void+show(): void+getnumofcat(): void - numofcat: int- name: string- num : stringboat+boat(n: string, nu :string, we: string)+gettoalweight(p1: boat&,p2:car&)+get(): void+show(): void- name:string - num : string- weight: intcar+car(n: string, nu :string, we: string)+gettoalweight(p1: boat&,p2:car&)+get(): void+show(): void- name:string - num : string- weight: int六、调试过程及实验结果1, 保存源程序代码,并声称解决方案。2,调试并执行。3,输出为:1, input the name tutuname:tutunum:1numofcat:1call staticnumofcat:1input the name huaname:huanum:2numofcat:2call staticnumofcat:2请按任意键继续. . .2,boatname:fengnum:012002weight:60carname:bennum:0054weight:80140tutu 0124 90baoma 0142453 100boatname:tutunum:0124weight:90carname:baomanum:0142453weight:100190请按任意键继续七、总结1,静态成员,不属于任何对象,系统单独非配空间。静态数据成员具有一般静态数据的特征,即只在第一次赋值。2,静态数据成员可以被所在类的成员函数访问,由于静态成员函数的形参中没有this指针,因此静态成员函数不能直接访问非静态成员数据,可以通过对象名的方式访问。3,如果一个函数被声明为以为类的友元函数,那么这个函数就可以访问这个类中的所有成员。4,有元不可以传递:如声明a是b的有元,b是c的有元。那么b不是a的有元,c也不是b的有元,a不是c的有元。八、附录1, cath文件#include#includeusing namespace std;class catpublic:cat()name= ;numofcat+;num=numofcat;void get();void show();static void getnumofcat();private:static int numofcat;string name;string num;catcpp文件#include5-7.hint cat:numofcat=0;void cat:get()coutname;void cat:show()coutname:nameendl;coutnum:numendl;coutnumofcat:numofcatendl;void cat:getnumofcat()coutcall staticendl;coutnumofcat:numofcatendl;cat maincpp文件#include5-7.hint main()cat cat1;cat1.get();cat1.show();cat1.getnumofcat();cat cat2;cat2.get();cat2.show();cat2.getnumofcat();return 0;2,car and boath文件#include#includeusing namespace std;class car;class boatpublic:boat(string n,string nu,int we);void friend gettoalweight(boat &p1,car &p2);void get();void show();private:string name;string num;int weight;class carpublic:car(string n,string nu,int we);void friend gettoalweight(boat &p1,car &p2);void get();void show();private:string name;string num;int weight;car and boatcpp文件#include5-14.hvoid gettoalweight(boat &p1,car &p2)coutp1.weight+p2.weightnamenumweight;void boat:show()coutboatendl;coutname:nameendlnum:numendlweight:weightnamenumweight;void car:show()coutcarendl;coutname:nameendlnum:numendlweight:weight添加事件处理程)。 在消息类型中选择command,在类列表中选择cgdview,并在函数处理程序名称栏修改函数名。点击添加编辑添加相应的函数。六、调试过程及实验结果1, 保存源程序代码,并声称解决方案。2,调试并执行。3,输出为:ab输出如下图:(点击运行菜单中的启动和停止按钮,或者双击鼠标的左右键,字幕会执行相应的动作)七、总结microsoft visual studio 2008与microsoft visual studio 2010有很大的差别。八、附录a 添加的类max定义:class maxdouble x1,x2,x3,x4;double max2(double,double);public:max(double,double,double,double);double max4();2添加的类max实现部分:double max:max2(double a, double b)if(a=b) return a;else return b;max:max(double a, double b, double c, double d)x1=a;x2=b;x3=c;x4=d;double max:max4()return max2(max2(x1,x2),max2(x3,x4);3. 添加的find函数:void cplotdoc:find()max a(110.5, 120.8, 110, 68);max b(130, 256.5, 90, 200);max c(125, 406.8, 350, 330);max d(120, 356.8, 300, 280.5);max e(102, 256.8, 120, 105);m_num0 = (int) a. max4();m_num1 = (int) b. max4();m_num2 = (int) c. max4();m_num3 = (int) d. max4();m_num4 = (int) e. max4();4. 修改后的ondraw函数:void cplotview:ondraw(cdc* pdc)cplotdoc* pdoc = getdocument();assert_valid(pdoc);/ todo: add draw code for native data herepdc-setmapmode(mm_isotropic);pdc-setviewportorg(50,250);pdc-moveto(0,0);pdc-lineto(1100,0);pdc-moveto(0,0);pdc-lineto(0,600);int width = 40;int ch = a; cstring str1;cbrush brush;brush.createsolidbrush(rgb(50, 250,0);pdc-selectobject(brush);for(int i = 1; irectangle(200*i, 0, 200*i+width, pdoc-m_numi-1); str1.format(_t(%c),ch); /整型以字符格式赋给str1 pdc-textout(200*i+10,-10, str1); /输出abcdecfont font;font.createfont(0,0,0,0,800,0,0,0,oem_charset, out_default_precis,clip_default_precis,default_quality,default_pitch,_t(楷体));pdc-selectobject(&font);pdc-textout(200,550, _t(各公司销售点水果月销售量直方图);b1. 在gdview.cpp文件中修改后的ondraw函数:void cxxx2view:ondraw(cdc* pdc)cxxx2doc* pdoc = getdocument(); assert_valid(pdoc);if (!pdoc)return;pdc-settextcolor(rgb(0,0,235);pdc-setbkmode(transparent);cfont font;font.createfont(28,15,0,0,fw_normal,false,false,false,default_charset,out_device_precis,clip_default_precis,default_quality,default_pitch,_t(隶书);pdc-selectobject(&font);pdc-textout(n,100,_t(世上无难事,只要肯登攀!);n=n+20;rect r;getclientrect(&r); /获得窗口if(nr.right-r.left)/窗口如果n 右坐标减去左坐标n=0; 添加的消息映射:void cgdview:onlbuttondblclk(uint nflags, cpoint point) /鼠标左双击函数/ todo: add your message handler code here and/or call defaultsettimer(1,500,null);cview:onlbuttondblclk(nflags, point);void cgdview:onrbuttondblclk(uint nflags, cpoint point) /鼠标右双击函数/ todo: add your message handler code here and/or call defaultkilltimer(1);cview:onrbuttondblclk(nflags, point);void cgdview:ontimer(uint nidevent) / todo: add your message handler code here and/or call defaultinvalidate();cview:ontimer(nidevent);添加的启动及停止对应的消息void cxxx2view:onmove() /启动对应消息/ todo: add your command handler code heresettimer(1,300,null);void cxxx2view:onstop() /停止对应的消息/ todo: add your command handler code herekilltimer(1);实验六一、实验名称单文档串行化编程二、实验目的及要求了解简单的单文档串行化编程方法。三、实验环境microsoft visual studio 2010四、实验内容设计应用程序串行化一个矩形数据,用对话框修改数据,并显示图形。对话框如下:五、算法描述及实验步骤1. 用appwizard建立一个普通单文档ff工程,按下一步,直到出现下图,将cffview的基类设为cformview。2. 在对话框中添加相应的控件,并添加变量,如下图:其中第五个编辑框添加为:控件类型,如下图:3.为四个编辑框添加事件处理程序,如下图:并添加相应的函数。3. 在doc头文件ffdoc.h中添加变量,并在ffdoc.cpp的构造函数中初始化变量。4. 在ffdoc.cpp的串行

温馨提示

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

评论

0/150

提交评论