面向对象程序设计C++实验报告.doc

上传人:小** 文档编号:3027611 上传时间:2020-06-23 格式:DOC 页数:58 大小:89.25KB
返回 下载 相关 举报
面向对象程序设计C++实验报告.doc_第1页
第1页 / 共58页
面向对象程序设计C++实验报告.doc_第2页
第2页 / 共58页
点击查看更多>>
资源描述

《面向对象程序设计C++实验报告.doc》由会员分享,可在线阅读,更多相关《面向对象程序设计C++实验报告.doc(58页珍藏版)》请在得力文库 - 分享文档赚钱的网站上搜索。

1、.-电子科技大学信息与软件工程学院标 准 实 验 报 告(实验)课程名称:面向对象程序设计C+电子科技大学教务处制表学生姓名:赵天豪 学 号:2014220902010 指导教师:李巧勤 实验地点:信软学院实验室 实验时间:15/12/21一、实验室名称:信软学院软件实验室 二、实验项目名称: 基于MFC的C+桌面应用开发三、实验学时:16学时四、实验原理:本次实验基于MFC开发C+桌面应用程序,实现可视化操作。五、实验目的:充分运用所学的C+的数据封装、继承与派生、多态等全部核心内容,补充完整一个基于MFC的应用,使学生能够基本掌握OOA、OOD方法;熟练掌握OOP方法;初步了解基于Wind

2、ows平台的桌面图形化应用的开发过程,掌握编程工具的使用;初步掌握建模工具的使用;基本掌握阅读、调试程序的能力。六、实验内容:一位小学教师Ken希望完成这样的任务:针对于小学生正在学习四边形(quadrangle)的特性,编写一个小软件,能够随机在屏幕上显示矩形(rectangle)、正方形(square)、平行四边形(parallelogram)、梯形(trapezoid)和菱形(diamond)五种形体之一,同时显示该形体的特性和关键数据(随机产生),学生复习形体的特性,然后根据给出的关键数据计算形体的面积,软件判断其结果的正确性。在学习过程中,软件记录产生的每一个形体,在学生选择不再继续

3、后,将其学习的过程重放一遍,用以重温,加深印象。根据上述描述,需要完成:1) 必做内容根据Ken老师的要求,需要至少编写六个类:l Rect /注意:类名不要使用Rectanglel Squarel Parallelograml Trapezoidl Diamondl List(注:这六个类必须以上述名字命名)其中,前五个类用于描述五种形体。五种形体不用顶点坐标的表示形式,而只是简单地用它们的特征值表示。例如:矩形、平行四边形用长和高表示;正方形用边长表示;梯形用两条平行边长和高表示;菱形用两条对象线长表示。要求为这五种形体编写相应的类,每个类的设计要求如下:(1) Quadrangle类必须

4、成为抽象类,是其它形体类的祖先。它拥有如下成员:成员类型是否纯虚name数据/area函数是draw函数是what函数可选(2) 其它形体类之间的继承关系请自行拟定。其中,Parallelogram类必须拥有如下虚成员:l Width(); /返回宽l Height(); /返回高(3) 每个形体类必须完成如下操作:1 设置标志名属性name。五种形体的标志名必须是Parallelogram、Rectangle、Diamond、Trapezoid和Square之一(第一个字母大写,其余小写);2 重载area()成员;3 重载draw()成员;4 重载Width()成员以返回宽;5 重载Hei

5、ght()成员以返回高;6 提供Width2()成员以返回第二条平行边长(仅对梯形)(4) List类必须拥有如下成员函数:l size();/返回列表中的节点数l push_back();/将数据添加到列表末尾l operator;/返回指定下标的数据l traverse();/遍历,需要一个访问函数作为参数l pop_back() /用来删除列表的最后一个节点,其原型为:void pop_back();(5) 重温过程实际上就是遍历List类对象的过程。2) 选作内容List类实际上是一种称为“容器”的类。除了容纳形体指针,其实它还可以容纳任何类型的对象。在本选作内容中,(1) 请将Lis

6、t类改为模板类,使其能容纳任何类型的对象;(2) 请为List类编写迭代器。要求如下:l 该迭代器的类名为Iterator;l 该迭代器必须重载如下运算符:l +:迭代器后移l !=:两个迭代器比较l *:返回迭代器指向节点的数据域中的值,即quad指针。l =:两个迭代器的复制l +。该运算符函数的原型为:Iterator operator+(int i);其功能为:假设当前迭代器(的内部指针)指向了第k个节点(从0开始计数),那么+运算符返回一个迭代器,该迭代器是在当前迭代器的基础上向后移动i个位置得到的,即该迭代器指向了第k+i个节点。例如:Iterator itr = list.beg

7、in() + 5;那么迭代器itr就指向了从列表的头节点往后数第5个结点。为Iterator类编写begin()和end()成员。七、实验器材(设备、元器件):PC计算机、Windows 系列操作系统 、Visual Studio2013软件八、实验步骤:1)完成类的设计;2)根据设计结果编写各种形体类的代码;完成学习模式的编程与测试3)完成List类的编码与测试;4)完善程序流程,实现复习模式;5)对前面完成的所有文件进行整合,生成一个解决方案,并进行调试;6)程序优化。 九、实验程序及结果分析:Canvas.h:#pragma once#include quad.hclass Quadra

8、ngle;/ Canvas 视图class Canvas : public CScrollViewDECLARE_DYNCREATE(Canvas)/protected:public:Canvas(); / 动态创建所使用的受保护的构造函数virtual Canvas();public:#ifdef _DEBUGvirtual void AssertValid() const;#ifndef _WIN32_WCEvirtual void Dump(CDumpContext& dc) const;#endif#endifprotected:virtual void OnDraw(CDC* pDC

9、); / 重写以绘制该视图virtual void OnInitialUpdate(); / 构造后的第一次DECLARE_MESSAGE_MAP()public:static Canvas * pCanvas;static void drawQuad(const Quadrangle *quad);static bool isLearning;bool toggleMode(void);double area;private:enum _QUAD_ para = 0, rect, sqr, diam, trap ;typedef pair KV;map quadMap;Diam:#pragm

10、a once#include#include#include Canvas.h#includepara.husing namespace std;class Diamond : public Parapublic:Diamond(int d1 = 4, int d2 = 8, string nm = Diamond) :Para(d1,d2,nm);Diamond();void draw() const Para:draw(); ;double area() const return (double)width*height/2; ;string what() const return nam

11、e; ;int& Width1() return d1; ;int& Width2() return d2; ;friend ostream&operator(ostream& os, const Diamond &c2);Diamond& operator= (const Diamond& di)name = di.name;d1 = di.d2;d2 = di.d2;return *this;protected:int d1, d2;Experiment:/ experiment.h : PROJECT_NAME 应用程序的主头文件/#pragma once#ifndef _AFXWIN_

12、H_#error 在包含此文件之前包含“stdafx.h”以生成 PCH 文件#endif#include resource.h/ 主符号/ CexperimentApp:/ 有关此类的实现,请参阅 experiment.cpp/class CexperimentApp : public CWinApppublic:CexperimentApp();/ 重写public:virtual BOOL InitInstance();/ 实现DECLARE_MESSAGE_MAP();extern CexperimentApp theApp;Experimentdlg.h:/ experimentDl

13、g.h : 头文件/#pragma once#include canvas.h/ CexperimentDlg 对话框class CexperimentDlg : public CDialogEx/ 构造public:CexperimentDlg(CWnd* pParent = NULL);/ 标准构造函数/ 对话框数据enum IDD = IDD_EXPERIMENT_DIALOG ;protected:virtual void DoDataExchange(CDataExchange* pDX);/ DDX/DDV 支持/ 实现protected:HICON m_hIcon;/ 生成的消息

14、映射函数virtual BOOL OnInitDialog();afx_msg void OnSysCommand(UINT nID, LPARAM lParam);afx_msg void OnPaint();afx_msg HCURSOR OnQueryDragIcon();DECLARE_MESSAGE_MAP()public:Canvas *pCanvas;afx_msg void OnBnClickedOk();double answer;CString result;afx_msg void OnClickedButtonNext();afx_msg void OnClickedB

15、uttonStop();afx_msg void OnClickedButtonSubmit();public:QUADPTR createAquad(void);private:bool lastDone;virtual BOOL OnCommand(WPARAM wParam, LPARAM lParam);List.h:#pragma once#include quad.h#include#include/typedef void(*ACCESSFUN)(QUADPTR); /定义遍历时节点处理函数类型class Listprotected:struct NodeQUADPTR data

16、;Node *next;Node(QUADPTR d) :data(d) *head, *tail; /链表的头尾指针/typedef Node* _range;size_t len; /链表中节点的数目,即链表的长度public:List() :head(NULL), tail(NULL) len = 0; /平凡构造函数List(List const& l)head = tail = NULL;Node *p = l.head;while (p != NULL)this-push_back(p-data);p = p-next;len = 0; /复制构造函数List()Node *p =

17、 head, *q;while (p != NULL)q = p;p = p-next;delete q;Node *Tail() return tail; /size_t size() return len; ; /获取链表的长度size_t List:size()Node *p = head;size_t i = 0;while (p != NULL)p = p-next;+i;return i;void push_back(const QUADPTR quad) /尾部添加Node *p = new Node(quad);p-next = NULL;if (tail = NULL) he

18、ad = tail = p;else tail-next = p; tail = p; len+;int pop_back()if(len = 0)return len;Node *c = head;if (c = tail) delete c; head = tail = NULL;elsewhile (c-next != tail) c+;delete tail;tail = c; tail-next = NULL;len-; return len;void traverse(void(*f)(const QUADPTR&)Node *p = head;int i = 1;while (p

19、 != NULL)cout 第 i+ 个 data);p = p-next;/请自行添加指针移动部分/遍历List& operator=(const List& l) head = tail = NULL; Node *p = l.head; while (p != NULL) this-push_back(p-data); p = p-next;len = 0; return *this; List& operator+=(const QUADPTR& data)Node *p = new Node(data);p-next = NULL;if (tail = NULL)head = tai

20、l = p;elsetail-next = p;tail = p;len+;return *this;QUADPTR& operator(size_t n)Node *p = head;for (size_t i = 1; i next;return p-data;Para.h:#pragma once#include#include#includequad.h#include Canvas.husing namespace std;class Para : public Quadranglepublic:Para(int w = 5, int h = 7, string nm = Paral

21、lelogram) :width(w), height(h), Quadrangle(nm);Para();void draw() const Canvas:drawQuad(this); ;double area() const return width*height; ;string what() const return Quadrangle:what(); ;int& Width() return width; ; /获取矩形的宽。如果是梯形,还需要一个成员int& Width2()来获取另一条平行边的长度int& Height() return height; ;/获取矩形的高fri

22、end ostream&operator(ostream& os, const Para &c3);Para&operator=(const Para&pa)name = pa.name;width = pa.width;height = pa.height;return *this;protected:int width;int height;Quad.h:#pragma once#include#include Canvas.husing namespace std;class Quadrangleprotected:public:string name; /形体的标识Quadrangle

23、(string nm =Quadrangle ) :name(nm)virtual double area() const =0;string what() const return name; ;virtual void draw() const=0;typedef Quadrangle * QUADPTR;Rect.h:#pragma once#include#include#includepara.h#include Canvas.husing namespace std;class Rect : public Parapublic:Rect(int w = 4, int h = 8,

24、string nm = Rectangle) :Para(w,h,nm);Rect();string what()const return Para:what(); ;double area()const return Para:area(); void draw()const Canvas:drawQuad(this); int Width()const return width; int Height()const return height; friend ostream&operator(ostream& os, const Rect &c1);Rect&operator=(const

25、 Rect&re) name = re.name; width = re.width; height = re.height; return *this;Resource.h:/NO_DEPENDENCIES/ Microsoft Visual C+ generated include file./ Used by experiment.rc/#define IDM_ABOUTBOX 0x0010#define IDD_ABOUTBOX 100#define IDS_ABOUTBOX 101#define IDD_EXPERIMENT_DIALOG 102#define IDR_MAINFRA

26、ME 128#define IDC_EDIT_ANSWER 1000#define IDC_BUTTON_SUBMIT 1001#define IDC_BUTTON_NEXT 1002#define IDC_BUTTON_STOP 1003#define IDC_EDIT_RESULT 1004#define IDC_STATIC_GROUP 1005/ Next default values for new objects/ #ifdef APSTUDIO_INVOKED#ifndef APSTUDIO_READONLY_SYMBOLS#define _APS_NEXT_RESOURCE_V

27、ALUE 129#define _APS_NEXT_COMMAND_VALUE 32771#define _APS_NEXT_CONTROL_VALUE 1006#define _APS_NEXT_SYMED_VALUE 101#endif#endifSqua.h:#pragma once#include#include#includerect.h#include Canvas.husing namespace std;class Square : public Rectpublic:Square(int w = 6, string nm = Square) :Rect(w,w,nm);Squ

28、are();void draw() const Rect:draw(); ;double area() const return width*width; ;string what() const return Rect:what(); ;int& Width() return width; ;friend ostream&operator(ostream& os, const Square &c4);Square&operator=(const Square&sq) name = sq.name;width = sq.width; return *this;Stdafx.h:/ stdafx

29、.h : 标准系统包含文件的包含文件,/ 或是经常使用但不常更改的/ 特定于项目的包含文件#pragma once#ifndef _SECURE_ATL#define _SECURE_ATL 1#endif#ifndef VC_EXTRALEAN#define VC_EXTRALEAN / 从 Windows 头中排除极少使用的资料#endif#include targetver.h#define _ATL_CSTRING_EXPLICIT_CONSTRUCTORS / 某些 CString 构造函数将是显式的/ 关闭 MFC 对某些常见但经常可放心忽略的警告消息的隐藏#define _AFX

30、_ALL_WARNINGS#include / MFC 核心组件和标准组件#include / MFC 扩展#include / MFC 自动化类#ifndef _AFX_NO_OLE_SUPPORT#include / MFC 对 Internet Explorer 4 公共控件的支持#endif#ifndef _AFX_NO_AFXCMN_SUPPORT#include / MFC 对 Windows 公共控件的支持#endif / _AFX_NO_AFXCMN_SUPPORT#include / 功能区和控件条的 MFC 支持#ifdef _UNICODE#if defined _M_I

31、X86#pragma comment(linker,/manifestdependency:type=win32 name=Microsoft.Windows.Common-Controls version=6.0.0.0 processorArchitecture=x86 publicKeyToken=6595b64144ccf1df language=*)#elif defined _M_X64#pragma comment(linker,/manifestdependency:type=win32 name=Microsoft.Windows.Common-Controls versio

32、n=6.0.0.0 processorArchitecture=amd64 publicKeyToken=6595b64144ccf1df language=*)#else#pragma comment(linker,/manifestdependency:type=win32 name=Microsoft.Windows.Common-Controls version=6.0.0.0 processorArchitecture=* publicKeyToken=6595b64144ccf1df language=*)#endif#endif#include #include / 你编写的头文

33、件都包含在这里/using namespace std;Targetver.h:#pragma once/ 包括 SDKDDKVer.h 将定义最高版本的可用 Windows 平台。/ 如果要为以前的 Windows 平台生成应用程序,请包括 WinSDKVer.h,并将/ WIN32_WINNT 宏设置为要支持的平台,然后再包括 SDKDDKVer.h。#include Trap.h:#pragma once#include#include#includequad.h#include Canvas.husing namespace std;class Trapezoid : public Q

34、uadranglepublic:Trapezoid(int w1 = 5.0, int w2 = 7.0, int h = 8.0, string nm = Trapezoid) :width1(w1), width2(w2),height(h), Quadrangle(nm);Trapezoid();void draw() const Canvas:drawQuad(this); ;double area() const return (double)(width1+width2)*height/2; ;string what() const return name; ;int Width1

35、() return width1; ;int Width2() return width2; ;int Height() return height; ;friend ostream&operator(ostream& os, const Trapezoid &c5);Trapezoid&operator=(const Trapezoid&tr) name = tr.name; width1 = tr.width1; width2 = tr.width2; height = tr.height; return *this;private:int width1;int width2;int he

36、ight;Canvas.cpp:/ Canvas.cpp : 实现文件/#include stdafx.h#include experiment.h#include Canvas.h#includequad.h#includediam.h#includelist.h#includepara.h#includerect.h#includesqua.h#includetrap.h/ 外部说明添加到这里extern List list;/ CanvasCanvas * Canvas:pCanvas = NULL;bool Canvas:isLearning = false;IMPLEMENT_DYN

37、CREATE(Canvas, CScrollView)Canvas:Canvas()pCanvas = this;quadMap.insert(KV(Parallelogram, para);quadMap.insert(KV(Rectangle, rect);quadMap.insert(KV(Square, sqr);quadMap.insert(KV(Diamond, diam);quadMap.insert(KV(Trapezoid, trap);static CString formatString = _T(宽=%d, 高=%d, 面积=),_T(对角线1=%d, 对角线2=%d,

38、 面积=),_T(平行边1=%d, 平行边2=%d, 高=%d, 面积=);Canvas:Canvas()BEGIN_MESSAGE_MAP(Canvas, CScrollView)END_MESSAGE_MAP()/ Canvas 绘图void Canvas:OnInitialUpdate()CScrollView:OnInitialUpdate();/CSize sizeTotal;/ TODO: 计算此视图的合计大小/sizeTotal.cx = sizeTotal.cy = 100;/SetScrollSizes(MM_TEXT, sizeTotal);union _QUADPTR_Q

39、uadrangle *q;Para *p;Rect *r;Diamond *d;Square *s;Trapezoid *t;void Canvas:OnDraw(CDC* pDC)CDocument* pDoc = GetDocument();/ TODO: 在此添加绘制代码CRect rect;GetClientRect(&rect);CBrush br(RGB(220, 200, 170);CBrush * oldBrush = reinterpret_cast(pDC-SelectObject (&br);int xc, yc;int vw = rect.Width (), vh = rect.Height ();int sw = static_cast(vw * 0.75 / 2.0), sh = static_cast(vh * 0.75 / 2.0);SIZE ad54 = -sw + 50, -sh , sw + 50, -sh , sw - 50, sh , -sw - 50, sh , -sw, -sh , sw, -sh , sw, sh , -sw, sh

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 技术资料 > 其他杂项

本站为文档C TO C交易模式,本站只提供存储空间、用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。本站仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知得利文库网,我们立即给予删除!客服QQ:136780468 微信:18945177775 电话:18904686070

工信部备案号:黑ICP备15003705号-8 |  经营许可证:黑B2-20190332号 |   黑公网安备:91230400333293403D

© 2020-2023 www.deliwenku.com 得利文库. All Rights Reserved 黑龙江转换宝科技有限公司 

黑龙江省互联网违法和不良信息举报
举报电话:0468-3380021 邮箱:hgswwxb@163.com