收藏 分销(赏)

c++面向对象--实验8-面向对象程序设计综合练习.doc

上传人:天**** 文档编号:4317052 上传时间:2024-09-05 格式:DOC 页数:6 大小:53.50KB 下载积分:6 金币
下载 相关 举报
c++面向对象--实验8-面向对象程序设计综合练习.doc_第1页
第1页 / 共6页
c++面向对象--实验8-面向对象程序设计综合练习.doc_第2页
第2页 / 共6页


点击查看更多>>
资源描述
中北大学计算机与控制工程学院 实验报告 课程名称 面向对象程序设计 学号1507084143 学生姓名刘安光 辅导教师 韩慧妍 专业 网络工程 实验室名称 软件基础实验室 实验时间 2016.12.3 1.实验名称 实验八 面向对象程序设计综合练习 2. 实验目的 (1)全面了解面向对象程序设计的设计方法; (2)对继承、虚函数、多态性有更深一步的了解; (3)学会编制结构清晰、风格良好、符合C++语言风格的C++程序。 3. 实验内容 设计一个可在屏幕上作图(点、线矩形、圆等图形)的简单实例,要求是不必真正在屏幕上实现作图,只是有一个示意即可。例如:画一个矩形,不必真正画出矩形,只需输出一句话:This is a rectangle !即可。要用到继承,虚函数,多态,数据的封装,构造函数的实现等等各种面向对象程序设计的特性。 4. 实验原理或流程图 继承: 通过继承机制,可以利用已有的数据类型来定义新的数据类型。所定义的新的数据类型不仅拥有新定义的成员,而且还同时拥有旧的成员。我们称已存在的用来派生新类的类为基类,又称为父类。由已存在的类派生出的新类称为派生类,又称为子类。 公有继承(public)、私有继承(private)、保护继承(protected)是常用的三种继承方式。 虚函数: 虚函数是 C++ 实现动态单分派子类型多态的方式。 动态:在运行时决定的(相对的是静态,即在编译期决定,如函数重载、模板类的非虚函数调用) 单分派:基于一个类型去选择调用哪个函数(相对于多分派,即由多个类型去选择调用哪个函数) 子类型多态:以子类型-超类型关系实现多态(相对于用参数形式,如函数重载、模版参数) 多态性: 所谓的多态即用父类型的指针指向子类对象,然后通过父类的指针调用实际之类的成员函数,因此父类的指针具有多种形态。多态性可以简单地概括为“一个接口,多种方法”,程序在运行时才决定调用的函数,它是面向对象编程领域的核心概念。   C++多态性是通过虚函数来实现的,虚函数允许子类重新定义成员函数,而子类重新定义父类的做法称为覆盖(override),或者称为重写。(这里我觉得要补充,重写的话可以有两种,直接重写成员函数和重写虚函数,只有重写了虚函数的才能算作是体现了C++多态性)而重载则是允许有多个同名的函数,而这些函数的参数列表不同,允许参数个数不同,参数类型不同,或者两者都不同。编译器会根据这些函数的不同列表,将同名的函数的名称做修饰,从而生成一些不同名称的预处理函数,来实现同名函数调用时的重载问题。但这并没有体现多态性。 5. 实验过程或源代码 //point.h #pragma once class Point{ private: int x, y; public: Point(); Point(const Point &p); Point(int x_pos,int y_pos); int getx(); int gety(); }; //point.cpp #include "Point.h" Point::Point(){} Point::Point(int x_pos,int y_pos){ x=x_pos; y=y_pos; } Point:: Point(const Point &p) { x=p.x; y=p.y; } int Point::getx(){ return x; } int Point::gety(){ return y; } //shape.h #pragma once enum ColorType{White,Black,Red,Green,Blue,Yellow,Magenta,Cyan}; class Shape{ private: protected: ColorType color; public: Shape(ColorType c); virtual void Draw(); }; //shape.cpp #include <iostream> #include "Shape.h" using namespace std; Shape::Shape(ColorType c){ color = c; cout << "This is Shape's constructor !" << endl; } void Shape::Draw(){ cout<<"Draw not overridden"<<endl; exit(1); } //line.h #include "Shape.h" #include "Point.h" class Line:public Shape{ private: Point head, end; //直线首尾点 public: Line(ColorType c, Point h, Point e); virtual void Draw(); }; //line.cpp #include <iostream> #include "Line.h" using namespace std; Line::Line(ColorType c, Point h, Point e):Shape( c ){ head = h; end = e; } void Line::Draw(){ cout << "This is a line !" << endl; cout << "The head point is (" << head.getx() << "," << head.gety() << ")" << endl; cout << "The end point is (" << end.getx() << "," << end.gety() << ")" << endl; } //circle.h #include "Shape.h" #include "Point.h" class Circle:public Shape{ private: Point center; //圆心 int radius; //半径 public: Circle(ColorType c, Point e, int r); virtual void Draw(); }; //circle.cpp #include <iostream> #include "Circle.h" using namespace std; Circle::Circle(ColorType c, Point e, int r):Shape( c ){ center=e; radius=r; } void Circle::Draw(){ cout << "This is a circle !" << endl; cout << "The center is (" << center.getx() << "," << center.gety() << ")" << endl; cout << "The radius is " << radius << endl; } //rectangle.h #include "Shape.h" #include "Point.h" class Rectangle:public Shape{ private: Point upperleft, lowerright; //矩形有两个点决定,左上角的点和右下角的点 public: Rectangle(ColorType c, Point l, Point r); void virtual Draw(); }; //rectangle.cpp #include <iostream> #include "Rectangle.h" using namespace std; Rectangle::Rectangle(ColorType c, Point l, Point r):Shape( c ){ upperleft = l; lowerright = r; } void Rectangle::Draw(){ cout << "This is a rectangle !" << endl; cout << "The upperleft point is (" << upperleft.getx() << "," << upperleft.gety() << ")" << endl; cout << "The lowerright point is (" << lowerright.getx() << "," << lowerright.gety() << ")" << endl; } //picture.h #include "Shape.h" class picture{ private: Shape *s[6]; public: picture(Shape* s1, Shape* s2, Shape* s3,Shape* s4, Shape* s5, Shape* s6); void paint(); }; //picture.cpp #include "picture.h" picture::picture(Shape* s1, Shape* s2, Shape* s3,Shape* s4, Shape* s5, Shape* s6) { s[0]=s1;s[1]=s2;s[2]=s3; s[3]=s4;s[4]=s5;s[5]=s6; } void picture::paint() { for(int i=0;i<6;i++) s[i]->Draw(); } //main.cpp #include "Point.h" #include "Line.h" #include "Circle.h" #include "Rectangle.h" #include "picture.h" void main() { Line l1(Red,Point(1,1),Point(250,300)); Line l2(White,Point(3,5),Point(100,200)); Circle c1(Blue,Point(100,75),50); Circle c2(Green,Point(50,200),20); Rectangle r1(Yellow,Point(10,10),Point(255,150)); Rectangle r2(Magenta,Point(20,30),Point (100,125)); picture p(&l1,&l2,&c1,&c2,&r1,&r2); p.paint(); } 6.实验结论及心得
展开阅读全文

开通  VIP会员、SVIP会员  优惠大
下载10份以上建议开通VIP会员
下载20份以上建议开通SVIP会员


开通VIP      成为共赢上传

当前位置:首页 > 包罗万象 > 大杂烩

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        抽奖活动

©2010-2026 宁波自信网络信息技术有限公司  版权所有

客服电话:0574-28810668  投诉电话:18658249818

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :微信公众号    抖音    微博    LOFTER 

客服