当前位置:文档之家› C++实验多态性实验报告

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

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

贵州大学实验报告

学院:电子信息学院专业:通信工程班级:

姓名学号实验组5实验时间指导教师成绩

实验项目名称多态性

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

1.编写 4 个重载函数 Double( x),返回值为输入参数的两倍;参数类型分别为int 、验long 、float 、 double ,返回值类型与参数类型一样。

2.请编写一个抽象类Shape,在此基础上派生出类Rectangle和Circle,二者都有

计算对象面积的函数GetArea ()和计算周长函数GetPerim ()。

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

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

原的过程

安装了 Visual C++ 的 PC机器

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

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

long 、 float 、 double ,返回值类型与参数类型一样。

2. 请编写一个抽象类Shape,在此基础上派生出类Rectangle 和 Circle,二者都有计

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

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<

float myFloat = 6.5F;

cout<

double myDouble = 6.5e20;

cout<

验int Double(int x){return 2*x;}

数long Double(long x){return 2*x;} float Double(float x){return 2*x;}

据double Double(double 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<<" 周长是: "<

double rr;

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++ 操作 !"<

return *this;

}

Point Point::operator++(int)

{

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

return Point(++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 (常量)。

见签名:年月日

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

相关主题
文本预览
相关文档 最新文档