C++语言程序设计 .doc
计算机科学与技术系上机实验报告课 程: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<iostream>#include<string>using namespace std;class Dogpublic:Dog(string n,int ag,int we);void get();void show();Dog()cout<<"called "<<endl;private:string name;int weight;int age;Dog.cpp#include"4-8.h"Dog:Dog(string n,int ag,int we):name(n),age(ag),weight(we)void Dog:get()cout<<"input the name age and weight of dog"<<endl;cin>>name>>age>>weight;void Dog:show()cout<<"the message of dog is:"<<endl;cout<<"name:"<<name<<" age:”<<age<<” weight:”<<weight<<endl;Dogmain.cpp#include"4-8.h"int 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<iostream>#include<string>using namespace std;class Rectanglepublic:void get();void show();private:int x1,x2,y1,y2;Rectangle.cpp#include"4-9.h"void Rectangle:get()cout<<"输入左下角的坐标:"<<endl;cin>>x1>>y1;cout<<"输入右上角的坐标:"<<endl;cin>>x2>>y2;void Rectangle:show()cout<<"两点的坐标"<<endl;cout<<"左下角的坐标:("<<x1<<","<<y1<<")"<<endl;cout<<"右上角的坐标:("<<x2<<","<<y2<<")"<<endl;cout<<"面积为:"<<(x2-x1)*(y2-y1)<<endl;Rectangle main.cpp#include"4-9.h"int 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,humain·h 文件#include<iostream>#include<string>using namespace std;class datepublic:date(int y=0,int m=0,int d=0);void get ();void show();date()cout <<"called date"<<endl;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;Humain·cpp文件#include"4-10.h"date:date(int y,int m,int d):year(y),month(m),day(d)void inline date:get() cout<<"input year month and day"<<endl;cin>>year>>month>>day;void date:show() cout<<year<<"/"<<month<<"/"<<day<<endl;humain:humain()name=" "CD=" "six=" "number=""humain:humain(humain &p)berthday=p.berthday;name=p.name;CD=p.CD;six=p.six;number=p.number;void humain:get(humain &p)cout<<"input the name six CD and number:"<<endl;cout<<"name:"cin>>name;cout<<"six:"cin>>six;cout<<"CD:"cin>>CD;cout<<"number:"cin>>number;cout<<"input berthday:"p.berthday.get();void humain:show(humain &p)cout<<"name:"<<name<<endl;cout <<"six:"<<six<<endl<<"CD:"<<CD<<endl<<"number:"<<number<<endl;cout<<"berthday:"p.berthday.show();Humain main·cpp文件#include"4-10.h"int main()humain p1;cout<<"the frist one befor set"<<endl;p1.show(p1);cout<<"the frist one after set"<<endl;p1.get(p1);p1.show(p1);humain p2(p1);cout<<"p2"<<endl;p2.show(p2);return 0;2,Complex·h文件#include<iostream>using namespace std;class complexpublic:complex(float a=0,float b=0);void add(complex &p);void show();private:float real;float imag;Complex·cpp文件#include"4-20.h"complex: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()cout<<real<<"+"<<imag<<"i"<<endl;Complex main·cpp文件#include"4-20.h"int 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<<static>>+getnumofcat(): void<<static>> - numofcat: int- name: string- num : stringboat+boat(n: string, nu :string, we: string)<<friend>>+gettoalweight(p1: boat&,p2:car&)+get(): void+show(): void- name:string - num : string- weight: intcar+car(n: string, nu :string, we: string)<<friend>>+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, cat·h文件#include<iostream>#include<string>using namespace std;class catpublic:cat()name=" "numofcat+;num=numofcat;void get();void show();static void getnumofcat();private:static int numofcat;string name;string num;Cat·cpp文件#include"5-7.h"int cat:numofcat=0;void cat:get()cout<<"input the name "cin>>name;void cat:show()cout<<"name:"<<name<<endl;cout<<"num:"<<num<<endl;cout<<"numofcat:"<<numofcat<<endl;void cat:getnumofcat()cout<<"call static"<<endl;cout<<"numofcat:"<<numofcat<<endl;Cat main·cpp文件#include"5-7.h"int main()cat cat1;cat1.get();cat1.show();cat1.getnumofcat();cat cat2;cat2.get();cat2.show();cat2.getnumofcat();return 0;2,Car and boat·h文件#include<iostream>#include<string>using 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 boat·cpp文件#include"5-14.h"void gettoalweight(boat &p1,car &p2)cout<<p1.weight+p2.weight<<endl;boat:boat(string n,string nu,int we):name(n),num(nu),weight(we)void boat:get()cin>>name>>num>>weight;void boat:show()cout<<"boat"<<endl;cout<<"name:"<<name<<endl<<"num:"<<num<<endl<<"weight:"<<weight<<endl;car:car(string n,string nu,int we):name(n),num(nu),weight(we)void car:get()cin>>name>>num>>weight;void car:show()cout<<"car"<<endl;cout<<"name:"<<name<<endl<<"num:"<<num<<endl<<"weight:"<<weight<<endl;Car and boat main·cpp文件#include"5-14.h"int main()boat b("feng","012002",60);car c("ben","0054",80);b.show();c.show();gettoalweight(b,c);b.get();c.get(); b.show();c.show();gettoalweight(b,c);return 0;实验四一、实验名称MFC对话框应用程序二、实验目的及要求了解并设计一个MFC对话框三、实验环境Microsoft Visual Studio 2010四、实验内容设计一个对话框应用程序,在对话框模板上添加控件,实现简单计算功能运行时界面如下五、算法描述及实验步骤1.创建一个对话框2 修改创建的对话框项目模板。删除模板上自动创建的两个控件。加入需要的静态文本框、编辑框和按钮控件,更改按钮的标题。3. 用ClassWizard为编辑框添加变量。4. 用ClassWizard为按钮控件添加消息映射:5. 添加初始化代码:(在CDlg.cpp文件)6. 在实现文件(cpp文件)中加入单击按钮时的事件响应代码。(双击一个按钮可直接进入函数编辑)六、调试过程及实验结果1, 保存源程序代码,并声称解决方案。2,调试并执行。3,运行结果:七、总结MFC是类和对象的简单应用。八、附录在BOOL Cmfc1Dlg:OnInitDialog()函数中添加:m_1=m_2=m_3;UpdateData(FALSE);程序段。char f;void Cmfc1Dlg:OnBnClickedRadio1()f='+'void Cmfc1Dlg:OnBnClickedRadio2()f='-'void Cmfc1Dlg:OnBnClickedRadio3()f='*'void Cmfc1Dlg:OnBnClickedRadio4()f='/'void Cmfc1Dlg:OnBnClickedButton1()UpdateData(TRUE);switch(f)case '+':m_3=m_1+m_2;break;case '-':m_3=m_1-m_2; break;case '*':m_3=m_1*m_2; break;case '/':if(m_2=0 )MessageBox(_T("除y数ºy不?能¨¹为a零¢?!ê?"); else m_3 = m_1/m_2; break;default:m_3=m_1+m_2;void Cmfc1Dlg:OnBnClickedButton2()UpdateData(TRUE);m_1=0;m_2=0;m_3=0;UpdateData(FALSE);实验五一、实验名称MFC单文档应用程序二、实验目的及要求了解并设计一个简单的MFC单文档应用程序三、实验环境Microsoft Visual Studio 2010四、实验内容用MFC单文档设计一个直方图和一个滚动字幕五、算法描述及实验步骤A 直方图使用AppWixard生成单文档工程tt在ttDoc.h中类 CttDoc的定义之前加入类Max的定义,并在ttDoc.cpp的文件尾部加入类Max的实现部分。用来求四个数中的最大数。为CttDoc类增加一个公有的void类型的成员函数Find和一个公有int数据成员数组m_num5,使用类产生对象并计算它们的最大值,将它们转换成int,存入整数数组m_num。为CttDoc类的成员函数OnNewDocument增加调用Find函数语句,为画图准备数据。在视类中的OnDraw函数添加画图写字代码。B 滚动字幕1. 创建一个简单的单文档应用程序gd; 2. 在gdView.h中定义一个私有变量n,用于控制输出文字的初始位置和移动。并在构造函数中对其初始化。3. 在gdView.cpp文件中修改OnDraw函数。4. 添加三条消息映射:双击鼠标左右键及时间映射,并加入相应代码。添加方法:项目è类向导è消息è相应的函数è添加项目è编译代码。5. 添加菜单控制与代理。方法:点击视图è资源视图èMenu文件夹 èMAINFRAME,进入菜单编辑。添加运行子菜单:启动和停止及其消息映射(右键->添加事件处理程)。 在消息类型中选择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; i<6; i+, ch+)pDC->Rectangle(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(n>r.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);实验六一、实验名称单文档串行化编程二、实验目的及要求了解简单的单文档串行化编程方法。三、实验环境Micro