您好,欢迎来到华佗健康网。
搜索
您的当前位置:首页C++实验多态性实验报告

C++实验多态性实验报告

来源:华佗健康网


贵州大学实验报告

学院:电子信息学院

专业:通信工程

班级:

学号

姓名 实验时间 实验项目名称

实验组 成绩

5

指导教师

多态性

实 验 目 的 实

通过让学生进行实验,使其对于动态多态性有一个较为深入的了解和熟悉。

最终可以

熟练使用。

1. 编写 4 个重载函数 Double( x),返回值为输入参数的两倍;

long 、float 、 double ,返回值类型与参数类型一样。 2. 请编写一个抽象类

计算对象面积的函数

Shape,在此基础上派生出类

GetArea ()和计算周长函数

参数类型分别为 int 、

Rectangle 和 Circle ,二者都有

GetPerim ()。

3. 对类 Point 重载 ++(自增)、 -- (自减)运算符。

实 验 原 理 实 验

安装了 Visual C++ 的 PC机器

仪 器 实 验

按照实验要求的内容逐一完成实验的要求。顺序是编写、编译、运行。

步 骤

Visual C++的编译环境下,完成实验要求的内容,完成编写、编译以及运行

的过程

实 验

1. 编写 4 个重载函数 Double(x),返回值为输入参数的两倍;参数类型分别为int

long 、 float 、 double ,返回值类型与参数类型一样。 2. 请编写一个抽象类 Shape,在此基础上派生出类

算对象面积的函数 GetArea ()和计算周长函数 3. 对类 Point 重载 ++(自增)、 -- (自减)运算符。

Rectangle 和 Circle GetPerim ()。

,二者都有计

1、 代码如下: #include using namespace std; int Double(int x); long Double(long x); float Double(float x); double Double(double x); int main()

{ int myInt = 6500;

cout<long myLong = 65000; cout<cout<double myDouble = 6.5e20;

cout<验 数

int Double(int x)

long Double(long x) float Double(float x) double Double(double x) 运行结果:

{ return 2*x;} { return 2*x;} { {

return 2*x;}

return 2*x;}

2、 代码:

#include #define PI 3.1415926; using namespace std; class Shape // 抽象类的 { public:

virtual double GetArea()

= 0; //纯虚函数

定义

virtual double GetPerim() };

= 0;

// 纯虚函数

class Rectangle : public Shape

// 矩形类,公有继承

{

public: Rectangle(double aa, double bb)

{

a=aa; b=bb;

cout<<\" 长 \"<virtual double GetArea() {

return a * b; }

virtual double GetPerim() {

return 2*( a + b ); } private: double a; double b; };

class Circle : public Shape

{

public: Circle(double rr)

// 带参数的 构造函数

//圆类,公有继承

// 带参数的

构造函数

{ r=rr;

cout<<\" 半径 \"<}

virtual double GetArea() {

return r * r * PI; }

virtual double GetPerim() {

return 2 * r * PI; }

private: double r; };

void main() {

double length, width; cout << \" 输入长和宽 : \";

cin >> length >> width;

Rectangle rect(length, width);

cout << \" 面积是: \"<< rect.GetArea() << endl<<\" 周长是: \"<cout << \" 输入半径 : \"; cin >> rr; Circle cir(rr);

cout << \" 面积是: \"<运行结果:

3、 代码如下:

#include class Point { public:

Point(int xx,int yy):x(xx),y(yy) {} void display()const; Point &operator++(); Point operator++(int); Point &operator--(); Point operator--(int); private:

int x,y;

};

void Point::display()const {

cout<<\" 当前 Point(\"<}

Point &Point::operator++() {

x++; y++;

cout<<\" 执行 x++,y++ 操作 !\"<}

Point Point::operator++(int) {

cout<<\" 执行 ++x,++y 操作 !\"<}

Point &Point::operator--(){x--;y--; cout<<\" 执行 x--,y-- 操作 !\"<Point Point::operator--(int)

{cout<<\" 执行 --x,--y 操作 !\"<int main() { int x,y;

cout<<\"Input x&y:\"; cin>>x>>y; Point point1(x,y);

point1.display();point1++; point1.display();++point1; point1.display();point1--; point1.display();--point1; point1.display();return 0; }

运行结果:

学习使用虚函数实现动态多态性。 而虚函数就是在基类中被关键字 virtual 说明,实 并在

派生类中重新定义的函数,且在派生类中重工业新定义时,函数原型,包括返回 验

类型、函数名、参数个数与参数类型的顺序,都必须与基类中的完全相同。此外,构 造函数不能是虚函数,但析构函数可以是虚函数。

函数的重载方法有一参数个数相同,但是类型不同;二参数个数不同;三

(常量)。

coust

签名: 年 月 日

注:各学院可根据教学需要对以上栏木进行增减。表格内容可根据内容扩充。

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- huatuo0.com 版权所有 湘ICP备2023021991号-1

违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务