文档库 最新最全的文档下载
当前位置:文档库 › Ch6 多态性与虚函数

Ch6 多态性与虚函数

第六章

多态性与虚函数

本章内容

?§6.1 多态性的概念

?§6.2 多态性的典型例子?§6.3 虚函数

?§6.4 纯虚函数与抽象类

§6.2 多态性的典型例子class Point

{

public:

Point( float=0, float=0 );

void void setPoint setPoint setPoint( float, float );( float, float );

float float getX getX getX( ) const { return x; }( ) const { return x; }

float float getY getY getY( ) const { return y; }( ) const { return y; }

friend friend ostream ostream ostream &operator << &operator <<

(ostream ostream &, const Point & ); &, const Point & );

private:

float x, y;

};

Point::Point Point::Point( float a, float b )( float a, float b )

{

x = a;

y = b;

}

void void Point::setPoint Point::setPoint Point::setPoint( float a, float b )( float a, float b )

{ x=a;y=b; }

ostream ostream & operator << ( & operator << ( & operator << (ostream ostream ostream & out, const & out, const Point &P )

{

out << "[ " << out << "[ " << P.x P.x P.x << ", " << << ", " << << ", " << P.y P.y P.y << "]" << << "]" << << "]" << endl endl endl;;}

int int main( )

main( ){

Point p(2.5, -56.4);

cout cout << p << << p << << p << endl endl endl;

;p.setPoint(5.4, 45.2);

cout cout << p << << p << << p << endl endl endl;

;return 0;

}

class class Circle:public Circle:public Circle:public Point Point

{

public:

Circle( float=0, float=0, float=1 );

void void setRadius(float setRadius(float setRadius(float););

float float getRadius getRadius getRadius( ) const;( ) const;

float area( ) const;

friend friend ostream ostream ostream &operator << &operator <<

( ( ostream ostream ostream &, const Circle & ); &, const Circle & );

private:

float radius;

};

Circle::Circle Circle::Circle( float a, float b, float r ):( float a, float b, float r ):( float a, float b, float r ):Point(a Point(a Point(a,

, b), b), radius(r radius(r radius(r) {}

) {}void void Circle::setRadius Circle::setRadius Circle::setRadius( float r )

( float r ){ radius = r; }

float float Circle::getRadius Circle::getRadius Circle::getRadius( ) const { return

( ) const { return radius; }

float float Circle::area

Circle::area Circle::area( ) const ( ) const {return 3.14159return 3.14159**radius radius**radius radius;;}

ostream ostream & operator << ( & operator << ( & operator << ( ostream ostream ostream & out, const

& out, const Circle & C )

{

out << out << ““Center = [ Center = [ ”” << C.getX C.getX( )( ) << << ““, , ”

” << << C.getY C.getY( )( ) << " ]" << << " ]" << endl endl endl;

;out << "Radius = " << out << "Radius = " << C.radius C.radius C.radius << << << endl endl endl;

;out << "Area = " << out << "Area = " << C.area C.area C.area( ) << ( ) << ( ) << endl endl endl;

;return out;

}

int int main( )

main( ){

Circle c( 2.5, 6.4, 5 );

cout cout << c << << c << << c << endl endl endl;

;c.setRadius

c.setRadius( 3.4 );( 3.4 );c.setPoint

c.setPoint( 5.4, 45.2 );( 5.4, 45.2 );cout cout << c << << c << << c << endl endl endl;

;Point &p = c;

cout cout << p << << p << << p << endl endl endl;;return 0;

}

#ifndef ifndef CYLINDER_H

CYLINDER_H #define CYLINDER_H

#include <#include

>#include "#include "point.h point.h point.h"

"#include "#include "circle.h circle.h circle.h"

"class class Cylinder:public Cylinder:public Cylinder:public Circle

Circle {public:

Cylinder( float=0, float=0, float=0, float=0 );

void void setHeight setHeight setHeight( float );

( float );float float getHeight getHeight getHeight( ) const;

( ) const;float area( ) const;

float volume( ) const;

friend friend ostream ostream ostream & operator << ( & operator << ( & operator << ( ostream ostream ostream &,

&, const Cylinder & );

protected:

float height;

};

#endif

#include <#include

>#include "#include "cylinder.h cylinder.h cylinder.h"

"Cylinder::Cylinder

Cylinder::Cylinder( float a, float b, float r, float h )( float a, float b, float r, float h ):Circle(a Circle(a, b, r), , b, r), , b, r), height(h height(h height(h){}

){}void void Cylinder::setHeight(float Cylinder::setHeight(float Cylinder::setHeight(float h) { height = h; } h) { height = h; }float float Cylinder::getHeight Cylinder::getHeight Cylinder::getHeight( ) const { return height; }( ) const { return height; }float float Cylinder::area Cylinder::area Cylinder::area( ) const

( ) const {

return

2*Circle::area Circle::area( )+2( )+2( )+2**3.141593.14159**getRadius getRadius( )( )( )*

*height;}

float float Cylinder::volume

Cylinder::volume Cylinder::volume( ) const ( ) const { return { return Circle::area Circle::area Circle::area( )( )( )*

*height; }ostream ostream & operator << ( & operator << ( & operator << ( ostream ostream ostream & out, const & out, const Cylinder & Cy )

{

out << "Center = [ " << out << "Center = [ " << Cy.getX Cy.getX Cy.getX() << ", " <<

() << ", " << Cy.getY Cy.getY Cy.getY() << " ]" << () << " ]" << () << " ]" << endl endl endl;

;out << "Radius = " << out << "Radius = " << Cy.getRadius Cy.getRadius Cy.getRadius( ) <<

( ) << endl endl endl;

;out << "Area = " << out << "Area = " << Cy.area Cy.area Cy.area( ) << ( ) << ( ) << endl endl endl;

;out << "Volume = " << out << "Volume = " << Cy.volume Cy.volume Cy.volume( ) << ( ) << ( ) << endl endl endl;

;return out;

}

#include <#include >

#include "#include "point.h point.h point.h""

#include "#include "circle.h circle.h circle.h""

#include "#include "cylinder.h cylinder.h cylinder.h""

using namespace std;

int int main( ) main( )

{

Cylinder cy( 2.5, 6.4, 5, 11 );

cout cout << "Original Cylinder:" << << "Original Cylinder:" << << "Original Cylinder:" << endl

endl endl;;

cout cout << cy << << cy << << cy << endl endl endl;;

cy.setPoint cy.setPoint( 5.4, 45.2 );( 5.4, 45.2 );

cy.setRadius cy.setRadius( 3.4 );( 3.4 );

cy.setHeight cy.setHeight( 12 );( 12 );

cout cout << "New Cylinder:" << << "New Cylinder:" << << "New Cylinder:" << endl endl endl;

;cout cout << cy << << cy << << cy << endl endl endl;

;Point &p = cy;

cout cout << "Point:" << << "Point:" << << "Point:" << endl endl endl;

;cout cout << p << << p << << p << endl endl endl;

;cout cout << "Circle:" << << "Circle:" << << "Circle:" << endl endl endl;

;Circle &c = cy;

cout cout << c << << c << << c <

;return 0;

}

§6.3 虚函数

作用:允许在派生类中重新定义与基类同名的函数,并且可以通过基类指针或者引用来访问基类和派生中的同名函数。

格式::

格式

virtual 类型 函数名(形参表){…}或者

virtual 类型 函数名(形参表);

类型 类名::函数名(形参表){…}

#include <#include >

#include

using namespace std;

class Student

{

public:

Student( Student( int int int num, string num, string num, string nam

nam nam, float

, float , float sco sco sco ) ){

number = num;

name = name = nam nam nam;;

score = score = sco sco sco;;

}

void display( )

{

cout cout << "\ << "\ << "\nStudent nStudent nStudent Information!\n" <<

Information!\n" << endl endl;

;cout cout << " << " << "Nnmber Nnmber Nnmber: " << number <<

: " << number << endl endl;

;cout cout << "Name: " << name << << "Name: " << name << << "Name: " << name << endl endl endl;

;cout cout << "Score: " << score << << "Score: " << score << << "Score: " << score << endl endl endl;

;}

protected:

int

int number; number;string name;

float score;

};

class Graduate: public Student

{

public:

 Graduate(int Graduate(int num, string num, string num, string nam nam nam, float , float , float sco sco sco, float wag):

, float wag): Student(num Student(num, , , nam nam nam, , , sco sco sco), ), ), wage(wag wage(wag wage(wag) {}

) {} void display( )

 {

cout cout << "\ << "\ << "\nGraduate nGraduate nGraduate Information!\n" << Information!\n" << Information!\n" << endl endl endl;

;cout cout << " << " << "Nnmber Nnmber Nnmber: " << number << : " << number << : " << number << endl endl endl;

;cout cout << "Name: " << name << << "Name: " << name << << "Name: " << name << endl endl endl;

;cout cout << "Score: " << score << << "Score: " << score << << "Score: " << score << endl endl endl;

;cout cout << "Salary: " << wage << << "Salary: " << wage << << "Salary: " << wage << endl endl endl;

; }

private:

 float wage;

};

int int main( )

main( ){

Student stud(1001, "Qin", 78);

Graduate grad(2025, "Ivy", 95.5, 1024.35);

Student Student *

*pt = &stud;pt->display( );

pt = &grad;

pt->display( );

system("pause system("pause");

");return 0;

}

实验6 多态性(一)

福建农林大学实验报告 实验6 多态性(一) 一、实验目的和要求 (1)掌握虚函数的定义与使用方法,进一步理解多态性的概念和分类。 (2)了解纯虚函数和抽象类的使用方法。 二、实验内容和原理 1、分析并调试下列程序,回答以下问题:(1)指出抽象类(2)指出虚函数,并说明它的作用(3)每个类的作用是什么?整个程序的作用是什么? 2、使用虚函数编写程序求球体、圆柱体和圆锥的体积,由于球体、圆柱体和圆锥都可以看做由圆继 承而来,所以可以定义圆类作为基类。在圆类中定义数据成员半径和一个求体积的虚函数。由圆类 派生出球体类、圆柱体类和圆锥类,在派生类中对圆类中的虚函数重新定义。编写一个外部函数求 各类形状的总体积。最后在main()函数中构造若干形状,并求它们的体积和。

三、实验环境 1. 硬件:PC机; 2. 软件:Windows操作系统、Visual C++ 6.0 四、算法描述及实验步骤 1、算法描述及步骤如下: (1)根据题目要求编写好程序代码并在VC环境下输入源程序。 (2)检查程序有无错误(包括语法错误和逻辑错误),有则改之。 (3)编译和连接,仔细分析编译信息,如有错误应找出原因并改正之。本题改正后的代码如下: #include const double PI=3.1415; class Shap { public:virtual double Area()=0; }; class Triangle:public Shap { public:Triangle(double h,double w) { H=h; W=w; } double Area() { return 0.5*H*W; } private:double H,W; }; class Circle:public Shap { public:Circle(double r) { R=r; } double Area() { return PI*R*R; } private:double R; }; double Total(Shap*s[],int n) { double sum=0; for(int i=0;i

实验8 多态性与虚函数

实验八多态性与虚函数 一、实验目的和要求 1.了解多态的概念; 2.了解虚函数的作用及使用方法; 3.了解静态关联和动态关联的概念和用法; 4.了解纯虚函数和抽象类的概念和用法 二、实验内容和结果 1.阅读下面的程序 1.1请写出程序的执行结果,并在上机时对照理解 class Vehicle {public: void run() const { cout << "run a vehicle. "<

airplane.run(); cout<<"(b) 用指向基类的指针访问成员函数: "<run(); vp=&airplane; vp‐>run(); } 1.2 如果将Vehicle 类的定义修改为虚函数,其余不变,请写出程序的执行结果,并在上机时对照理解 class Vehicle {public: virtual void run() const { cout << "run a vehicle. "<

c++多态性与虚函数习题

作业题 一、写出下列程序运行结果 1.#include using namespace std; class A { public: virtual void func( ) {cout<<”func in class A”< using namespace std; class A{ public: virtual ~A( ){ cout<<”A::~A( ) called “<

}; void fun(A *a) { delete a; } int main( ) { A *a=new B(10); fun(a); } 二、程序设计题 1有一个交通工具类vehicle,将它作为基类派生小车类car、卡车类truck和轮船类boat,定义这些类并定义一个虚函数用来显示各类信息。 5.2定义一个shape抽象类,派生出Rectangle类和Circle类,计算各派生类对象的面积Area( )。 5.5某学校对教师每月工资的计算公式如下:固定工资+课时补贴。教授的固定工资为5000元,每个课时补贴50元;副教授的固定工资为3000元,每个课时补贴30元;讲师的固定工资为2000元,每个课时补贴20元。给出教师抽象类及主函数,补充编写程序求若干教师的月工资。 #include using namespace std; class Teacher{ protected: double salary; int workhours; public: Teacher(int wh=0){workhours=wh;} virtual void cal_salary()=0; void print(){cout<cal_salary(); prof.print(); Vice_Prof vice_prof(250); pt=&vice_prof; pt->cal_salary(); vice_prof.print(); Lecture lecture(100); pt=&lecture; pt->cal_salary(); lecture.print (); return 0; }

实验6多态性与虚函数

[实验目的] 1、了解多态性的概念; 2、了解虚函数的用途及使用方法; 3、了解纯虚函数和抽象类的概念和用法。 [实验要求] 给出以下各实验内容的源程序代码,并把编译、运行过程中出现的问题以及解决方法填入实验报告中,按时上交。 [实验学时] 2学时。 [实验内容] 1、写一个程序,定义抽象基类Shape,由它派生出3个派生类:Circle(圆形)、Square(正方形)、Rectangle(矩形)。利用指针、虚函数printArea()分别输出以上三者的面积,3个图形的数据在定义对象时给定。 [源程序] #include using namespace std; class Shape { public: virtual float area()const=0; virtual void display()const=0; }; class Circle:public Shape { public: Circle(double a):r(a){} virtual float area()const{return 3.14*r*r;} virtual void display()const { cout<<"圆面积"<

class Rectangle:public Shape { public: Rectangle(double a,double b):l(a),w(b){} virtual float area()const{return l*w;} virtual void display()const { cout<<"矩形面积"<display(); m=m+p[i]->area(); }

实验三虚函数与多态纯虚函数(完全版)

实验三虚函数与多态、纯虚函数 一.实验目的 1. 在掌握继承与派生关系的基础上,进一步理解虚函数与多态性的关系,实现运行时的多态。 2. 学会定义和使用纯虚函数 二、实验内容 1.范例:了解"单接口,多方法"的概念。现有称为figure的基类,存放了各二维对象(三角形、矩形和圆形三个类)的各维数据,set_dim()设置数据,是标准成员函数。 show_area()为虚函数,因为计算各对象的面积的方法是不同的。 【程序】 #include < iostream > using namespace std; class figure{ protected: double x,y;

public: void set_dim(double i,double j=0) { x=i; y=j; } virtual void show_area() { cout<<"No area computation defined for this class.\n"; } }; class triangle:public figure{ public: void show_area() { cout<<"Triangle with height "<< x<<" and base "<< y<<" has an area of "<< x*0.5*y<< endl; } }; class square:public figure{ public:

void show_area() { cout<<"Square with dimensions "<< x<<" and "<< y<<" has an area of "<< x*y<< endl; } }; class circle:public figure{ public: void show_area() { cout<<"Circle with radius "<< x<<" has an area of "<<3.14159*x*x<< endl; } }; int main(){ figure *p; triangle t; square s;

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< #define PI 3.1415926; using namespace std; class Shape // 抽象类的定义 { public: virtual double GetArea()= 0; //纯虚函数

实验六 多态性和虚函数

实验六多态性和虚函数 一、实验目的 1、了解多态性的概念。 2、了解虚函数的作用及其使用方法。 3、了解静态关联和动态关联的概念和用法。 4、了解纯虚函数和抽象类的概念和用法。 二、实验要求 1、分析程序运行结果,掌握虚函数的使用。 程序一: #include class ONE { public: virtual void f(){cout<<"l"<f(); } 程序二: #include class Base { public: virtual void fn() { cout <<"In Base Class\n";} }; class SubClass :public Base { public: virtual void fn(){ cout <<"In Sub Class\n"; } }; void main()

{ Base bc,*p; SubClass sc; p=&bc; p->fn(); p=≻ p->fn(); } 2、实现一个类A,在A中有两个私有的整型变量a和b,定义构造函数对a和b进行初始化,并实现成员函数geta()取得a的值和getb()取b的值。实现类B从A继承,覆盖geta(),使其返回a的2倍。主函数中声明类B对象,调用类B中的geta()并将结果输出。 3、声明抽象基类Shape,由它派生出3个派生类:Cirle(圆形)、Rectangle(矩形)、Triangle (三角形),用一个函数printArea分别输出以上三者的面积,3个图形的数据在定义对象是给定。

实验五 虚函数与多态性

实验三继承与多态性 一、实验目的与要求 1、掌握继承、基类和派生类的概念。 2、掌握初始化基类成员的方法。 3、掌握派生类对基类的继承。 4、学习虚函数和纯虚函数的定义与使用方式。 5、理解抽象类的概念,学习如何用指针指向其他的派生类,实现多态性。 6、掌握抽象类的定义与使用方式,并注意指针的用法。 7、学习如何使用虚函数、纯虚函数、抽象类和实现类的多态性。 二、实验设备与平台 实验设备要求每个学生一台电脑,其中运行环境为VC++ 6.0系统或新版。 三、实验内容与步骤 实验:编写一个人员信息管理系统。这个系统的功能是:交互式地实现校园人员信息的录入与显示。 分析: 学校里,主要有四类人员:大学本科学生、教师、研究生和助教。 大学本科生每周有固定的学时数。教师除了固定的学时数外,还有每周的教学时数。研究生除了固定的学时数外,每周还可以自由做一定的研究。助教除了上课外,还要做研究和一定的教学工作。 人员的基本信息包括姓名、编号、性别、身份证号、总学时数、以及每周固定学时数。各个人员之间的关系:people类派生出student类和teacher类,student类派生出graduate 类,graduate类和teacher类派生出TA类。 以下给出部分程序: #include using namespace std; class people { public: //添加程序 private: }; class student: virtual public people {

public: //添加程序 private: }; class teacher: virtual public people { public: //添加程序 private: }; class graduate: virtual public student { public: //添加程序 private: }; class TA: public graduate, public teacher { public: //添加程序 private: }; void main() { //添加程序 }

c多态性与虚函数习题

1.概念填空题 1.1 C++支持两种多态性,分别是静态和动态。 1.2在编译时就确定的函数调用称为静态联编,它通过使用重载函数实现。 1.3在运行时才确定的函数调用称为动态联编,它通过虚函数来实现。1.4虚函数的声明方法是在函数原型前加上关键字 virtual 。在基类中含有虚函数,在派生类中的函数没有显式写出virtual关键字,系统依据以下规则判断派生类的这个函数是否是虚函数:该函数是否和基类的虚函数参数个数相同 /同名;是否与基类的虚函数相应类型相同;是否与基类的虚函数返回值类型相同。如果满足上述3个条件,派生类的函数就是虚函数。并且该函数覆盖基类的虚函数。 1.5当通过指针或应用使用虚函数时,C++会在与对象关联的派生类中正确的选择重定义的函数。实现了运行时多态。而通过对象使用虚函数时,不能实现运行时多态。 1.6 纯虚函数是一种特别的虚函数,它没有函数的函数体部分,也没有为函数的功能提供实现的代码,它的实现版本必须由派生类给出,因此纯虚函数不能是。拥有纯虚函数的类就是抽象类,这种类不能建立对象。如果纯虚函数没有被重载,则派生类将继承此纯虚函数,即该派生类也是纯虚函数。 1.7 类的构造函数不可以(可以/不可以)是虚函数,类的析构函数可以(可以/不可以)是虚函数。当类中存在动态内存分配时经常将类的析构函数声明成虚函数。 2.简答题 2.1在C++中,能否声明虚构造函数?为什么?能否声明虚析构函数?为什么? 不可以声明纯构造函数,可以声明纯析构函数,因为C++中构造函数不能够被继承,而唇函数具有强继承性 2.2 什么是抽象类?抽象类有何作用?可以声明抽象类的对象吗?为什么? 一个类中至少有一个纯纯虚函数 抽象类中的纯虚函数可以在抽象类中定义,也可以是从它的抽象类中继承下来重订于定义 可以声明抽象类的指针和引用 2.3多态性和虚函数有何作用? 2.4是否使用了虚函数就能实现运行时的多态性?怎样才能实现运行时的多态性? 2.5为什么析构函数总是要求说明为虚函数? 3.选择题 3.1在C++中,要实现动态联编,必须使用( D )调用虚函数。 A.类名 B.派生类指针 C.对象名 D.基类指针 3.2下列函数中,不能说明为虚函数的是( )。 A.私有成员函数 B.公有成员函数 C.构造函数 D.析构函数 3.3在派生类中,重载一个虚函数时,要求函数名、参数的个数、参数的类型、参数的顺序和函数的返回值( )。 A.相同 B.不同 C.相容 D.部分相同 3.4当一个类的某个函数被说明为virtual时,该函数在该类的所有派生类中()。 A.都是虚函数

多态性与虚函数练习

多态性与虚函数练习 1.设计一个评选优秀教师和学生的程序,其类结构如图1所示。当输入一系列教师或学生的记录后,将优秀学生及教师的姓名列出。 图1 类结构 假设运行结果如下所示:#include #include using namespace std; class Base { public: void getname(){ cout<<"姓名:";cin>>name;} void printname() { cout<>num;} int isgood() { return num>90?1:0;} private: int num; }; class Teacher:public Base { public: void getnum() { cout<< "每年发表论文数:";cin>>num;} int isgood(){ return num>3?1:0;} 类Base void getname() void printname() 纯虚函数int ifgood()=0 string name 类student void getnum() int num 纯虚函数ifgood实现:若考试成绩大于90分,则返回1 类teachere void getnum() int num 纯虚函数ifgood实现:若一年发表论文大于3篇,则返回1

实验8_继承和多态

实验08:继承与多态 实验学时:6 实验类型:验证、设计 实验要求:必修 一、实验目的 1.理解继承的概念,了解面向对象设计中继承和多态的合理性; 2.掌握派生类的构造与析构; 3.掌握在对象中使用类层次和继承思想进行设计、实现和测试; 4.区别运行时的多态性的实现,理解重载与同名覆盖的差异; 5.理解虚函数与多态性; 6.实现运行时多态性的程序设计。 二、实验内容 1.Difine a class called PartFileledArrayWMax that is a derived class of the class PartFilledArray. The class PartFilledArrayWMax has one additional member variable named max_value that holds the maximum value stored in the array. Define a member accessor function named get_max that returns the maximum value stored in the array. Redefine the member function add_value and define two constructors, one of which has an int argument for the maximum number of entries in the array. Also define a copy constructor, an overloaded assignment operator, and a destructor. (A real class would have more member functions, but these will do for an exercise.) 2.某公司雇员(employee)包括经理(Manager)、技术人员(Technician)和销售员(Saleman)。开发部经理(developermanager)既是经理也是技术人员,销售部经理(salesmanager)既是经理也是销售员。 以employee类为虚基类,派生出manager、technician和saleman类,再进一步派生出developermanager和salesmanager类。 Employee类的属性包括姓名、职工号、工资级别、月薪(实发基本工资加业绩工资);操作包括月薪计算函数pay(),该函数要求输入请假天数,扣除应扣工

多态性和虚函数 实验报告

淮海工学院计算机科学系实验报告书 课程名:《 C++程序设计(二)》 题目:多态性和虚函数 班级: 学号: 姓名:

1、实验内容或题目 (1)声明二维坐标类作为基类派生圆的类,把派生类圆作为基类,派生圆柱体类。其中,基类二维坐标类有成员数据:x、y坐标值;有成员函数:构造函数实现对基类成员数据的初始化、输出的成员函数,要求输出坐标位置。派生类圆类有新增成员数据:半径(R);有成员函数:构造函数实现对成员数据的初始化、计算圆面积的成员函数、输出半径的成员函数。派生圆柱体类新增数据有高(H);新增成员函数有:构造函数、计算圆柱体体积的函数和输出所有成员的函数。请完成程序代码的编写、调试。 (2)教材393页7-8题。 (3)教材416页1、4、5题。 2、实验目的与要求 (1)理解继承与派生的概念 (2)掌握通过继承派生出一个新的类的方法 (3)了解多态性的概念 (4)了解虚函数的作用与使用方法 3、实验步骤与源程序 ⑴实验步骤 先定义一个基类point,及其成员函数,然后以public的继承方式定义子类circle,再定义一个派生类cylinder,最后在main主函数中定义类对象,调用函数实现其功能。 先定义一个基类A及其重载的构造函数,然后以Public派生出子类B,再定义其构造函数,最后在main主函数中定义类对象,调用成员函数实现其功能。 ⑵源代码 1.#include class Point { public: Point(float=0,float=0); void setPoint(float,float); float getX() const {return x;}

c++多态性与虚函数习题答案

多态性与虚函数 1.概念填空题 1.1 C++支持两种多态性,分别是编译时和运行时。 1.2在编译时就确定的函数调用称为静态联编,它通过使用函数重载,模板等实现。 1.3在运行时才确定的函数调用称为动态联编,它通过虚函数来实现。 1.4虚函数的声明方法是在函数原型前加上关键字virtual。在基类中含有虚函数,在派生类中的函数没有显式写出virtual关键字,系统依据以下规则判断派生类的这个函数是否是虚函数:该函数是否和基类的虚函数同名;是否与基类的虚函数参数个数相同、类型;是否与基类的虚函数相同返回类型。如果满足上述3个条件,派生类的函数就是虚函数。并且该函数覆盖基类的虚函数。 1.5 纯虚函数是一种特别的虚函数,它没有函数的函数体部分,也没有为函数的功能提供实现的代码,它的实现版本必须由派生类给出,因此纯虚函数不能是友元函数。拥有纯虚函数的类就是抽象类类,这种类不能实例化。如果纯虚函数没有被重载,则派生类将继承此纯虚函数,即该派生类也是抽象。 3.选择题 3.1在C++中,要实现动态联编,必须使用(D)调用虚函数。 A.类名 B.派生类指针 C.对象名 D.基类指针 3.2下列函数中,不能说明为虚函数的是(C)。 A.私有成员函数 B.公有成员函数 C.构造函数 D.析构函数 3.3在派生类中,重载一个虚函数时,要求函数名、参数的个数、参数的类型、参数的顺序和函数的返回值(A)。 A.相同 B.不同 C.相容 D.部分相同 3.4当一个类的某个函数被说明为virtual时,该函数在该类的所有派生类中(A)。 A.都是虚函数 B.只有被重新说明时才是虚函数 C.只有被重新说明为virtual时才是虚函数 D.都不是虚函数 3.5(C)是一个在基类中说明的虚函数,它在该基类中没有定义,但要求任何派生类都必须定义自己的版本。 A.虚析构函数B.虚构造函数 C.纯虚函数D.静态成员函数 3.6 以下基类中的成员函数,哪个表示纯虚函数(C)。 A.virtual void vf(int);B.void vf(int)=0; C.virtual void vf( )=0;D.virtual void vf(int){ } 3.7下列描述中,(D)是抽象类的特性。 A.可以说明虚函数 B.可以进行构造函数重载 C.可以定义友元函数 D.不能定义其对象 3.8类B是类A的公有派生类,类A和类B中都定义了虚函数func( ),p是一个指向类A对象的指针,则p->A::func( )将(A)。

多态与虚函数 练习题

多态与虚函数练习 选择题 单选题 1.要实现动态多态性,可以通过()调用虚函数。 A. 基类指针 B. 成员名限定 C. 对象名 D. 派生类名 2.以下()成员函数表示纯虚函数。 A. virtual int vf(int); B. void vf(int)=0; C. virtual void vf()=0; D. virtual void vf(int) { };

3.关于纯虚函数和抽象类的描述中,错误的是()。 A. 纯虚函数是一种特殊的虚函数,它没有具体的实现 B. 抽象类是指具有纯虚函数的类 C. 一个基类中说明有纯虚函数,该基类派生类一定不再是抽象类 D. 抽象类只能作为基类来使用,其纯虚函数的实现由派生类给出 4.下面4个选项中,()是用来声明虚函数的。 A. virtual B. public C. using D. false 5.下列关于动态联编的描述中,错误的是()。 A. 动态联编是以虚函数为基础 B. 动态联编是运行时确定所调用的函数代码的 C. 动态联编调用函数操作是指向对象的指针或对象引用 D. 动态联编是在编译时确定操作函数的

6.关于虚函数的描述中,()是正确的。 A. 虚函数是一个静态成员函数 B. 虚函数是一个非成员函数 C. 虚函数既可以在函数说明时定义,也可以在函数实现时定义 D. 派生类的虚函数与基类中对应的虚函数具有相同的参数个数和类型 7.下面关于友元的描述中,错误的是()。 A. 友元函数可以访问该类的私有数据成员 B. 一个类的友元类中的成员函数都是这个类的友元函数 C. 友元可以提高程序的运行效率 D. 类与类之间的友元关系可以继承 8.下面描述中,正确的是()。 A. virtual可以用来声明虚函数 B. 含有纯虚函数的类是不可以用来创建对象的,因为它是虚基类

实验四 类的多态性与虚函数

实验四类的多态性与虚函数 (实验课时:2 实验性质:设计) 实验名称: 类的多态性与虚函数 实验目的: (1)了解多态性的概念; (2)了解虚函数的作用和使用方法; (3)了解纯虚函数和抽象类的概念和用法。 实验设备:(1)硬件:个人微机(配置不低于:CPU为P4,主频1.6G,内存256MB,硬盘40GB); (2)软件:操作系统为WindowsXP(或2000、server2003等),工具软件为Visual C++6.0。 实验内容: 事先编好程序,上机调试和运行程序,分析结果。 (1)编程:定义抽象基类Shape(图形)。 Shape类有一个公共的纯虚函数Area,用于派生类求图形的面积。 (2)由Shape派生出3个派生类:Circle(圆形)、Rectangle(矩形)、Triangle(三角形),用一个函数PrintArea分别输出以上三者的面积,3个图形的数据在定义对象时给定。 在主程序中测试这些类的PrintArea函数。 (3)在类Circle基础上再派生一个圆柱体Cylinder,重载Area函数求圆柱体的体积。然后,编写主程序采用静态多态性和动态多态性分别测试Area函数。 (4)在类Circle中定义一个析构函数,让它输出一行信息“This is Cicle Destruction。”,在类Cylinder中定义一个析构函数,让它输出一行信息“This is Cylinder Destruction。”。 在主程序中用指向基类Circle类的指针动态生成Cylinder类对象,然后用delete 语句释放该对象,测试看看运行结果如何。再修改Circle的析构函数为虚函数,测试看看运行结果如何。 (5)运行上述程序,并分析结果。 实验要求: (1)掌握多态性的概念; (2)掌握虚函数的作用和使用方法; (3)掌握纯虚函数和抽象类的概念和用法; (4)程序格式规范,程序运行正确; (5)认真书写实验报告,如实填写各项实验内容。 实验步骤: (1)启动Visual C++6.0开发环境; (2)创建一个项目; (3)建立C++源程序文件; (4)编辑C++源程序文件内容; (5)建立并运行可执行程序,并分析结果; (6)关闭工作空间。 思考题:虚函数的作用是什么?、

c++多态性相关习题

多态性 10.2 典型例题分析与解答 例题1:指出下列对定义重载函数的要求中,哪些是错误的提法。 A.要求参数的个数不同。 B.要求参数中至少有一个类型不同。 C.求函数的返回值不同。 D. 要求参数的个数相同时,参数类型不同。答案: C 例题3:下面关于友元的描述中,错误的是()。 A. 友元函数可以访问该类的私有数据成员 B. 一个类的友元类中的成员函数都是这个类的友元函数 C. 友元可以提高程序的运行效率 D. 类与类之间的友元关系可以继承 答案:D 1

例题4:下述静态成员的特性中,()是错误的。 A. 静态成员函数不能利用this指针 B. 静态数据成员要在类体外进行初始化 C. 引用静态数据成员时,要在静态数据成员名前加<类名>和作用域运算符 D. 静态数据成员不是所有对象所共有的 答案:D 例题5:关于虚函数的描述中,()是正确的。 A. 虚函数是一个静态成员函数 B. 虚函数是一个非成员函数 C. 虚函数既可以在函数说明时定义,也可以在函数实现时定义 D. 派生类的虚函数与基类中对应的虚函数具有相同的参数个数和类型 参考答案:D 2

例题11:分析下列程序的输出结果。 #include class A{ public: A() { cout<<"A's cons."<

实验6多态性与虚函数

序号 实验6多态性与虚函数 [实验目的] ---------------------- 1、了解多态性的概念; 2、了解虚函数的用途及使用方法; 3、了解纯虚函数和抽象类的概念和用法。 [实验要求] 给出以下各实验内容的源程序代码,并把编译、运行过程中出现的问题 以及解决方法填入实验报告中,按时上交。 [实验学时] 2学时。 [实验内容] 1、写一个程序,定义抽象基类Shape由它派生出3个派生类:Circle(圆形)、Square正方形)、Rectangle矩形)。利用指针、虚函数printArea()分别输出以上三者的面积,3个图形的数据在定义对象时给定。 [源程序] #in clude using n amespace std; class Shape { public: virtual float area()c on st=0; virtual void display()c on st=0; }; class Circle:public Shape { public: Circle(double a):r(a){} virtual float area()co nst{return 3.14*r*r;} virtual void display()c onst { cout?"圆面积"<

class Recta ngle:public Shape { public: Rectangle(double a,double b):l(a),w(b){} virtual float area()c on st{return l*w;} virtual void display()c onst { cout?"矩形面积"<display(); m=m+p[i]->area(); {

第5章 多态性和虚函数

《面向对象程序设计》习题 班级:学号:姓名:成绩:____________ 第5章多态性和虚函数 一、选择题(共26分,每题2分) 1.关于虚函数的描述中,()是正确的。 A.虚函数是一个静态成员函数 B.虚函数是一个非成员函数 C.虚函数既可以在函数说明时定义,也可以在函数实现时定义 D.派生类的虚函数与基类中对应的虚函数具有相同的参数个数和类型2.编译时的多态性可以通过使用()获得。 A.虚函数和指针 B.重载函数和析构函数 C.虚函数和对象 D.虚函数和引用3.下面是类Shape的定义 Class Shape { public: Virtual void Draw()=0; }; 下列关于Shape类的描述中,正确的是()。 A.类Shape是虚基类 B.类Shape是抽象类类 C.类Shape中的Draw函数声明有误 D.语句“Shape s”能够建立Shape的一个对象 4. 下面4个选项中,()是用来声明虚函数的。 A.virtual B.public C.using D.false 5. 关于纯虚函数和抽象类的描述中,错误的是()。 A.纯虚函数是一种特殊的虚函数,它没有具体的实现 B.抽象类是指具体纯虚函数的类 C.一个基类中说明有纯虚函数,该基类派生类一定不再是抽象类 D.抽象类只能作为基类来使用,其纯虚函数的实现由派生类给出 6. 下列描述中,()是抽象类的特征。 A.可以说明虚函数B.可以进行构造函数重载 C.可以定义友元函数D.不能说明其对象 7. 以下()成员函数表示虚函数。 A.virtual intvf(int); B.void vf(int)=0; C.virtual void vf()=0; D.virtual void vf(int) { }; 8. 如果一个类至少有一个纯虚函数,那么就称该类为()。 A.派生类B.虚函数C.抽象类D.以上都不对 9. 要实现动态联编,必须通过()调用虚函数。 A.对象指针B.成员名限定C.对象名D.派生类名 10. 下面描述中,正确的是()。

实验八 虚函数及应用

实验八虚函数及应用 一、实验目的 1.理解虚函数与运行时(动态)多态性之间的关系,掌握虚函数的定义及应用; 2.理解纯虚函数与抽象类的概念,掌握抽象类的定义及应用; 3.理解虚析构函数的概念及作用。 二、实验学时 课内实验:2课时课外练习:2课时 三本实验涉及的新知识 ㈠虚函数与动态多态性 在C++中,如果将基类与派生类的同名成员函数定义为虚函数,就可以定义一个基类指针,当基类指针指向基类对象时访问基类的成员函数,当基类指针指向派生类对象时访问派生类的成员函数,实现在运行时根据基类指针所指向的对象动态调用成员函数,实现动态多态性。换句话说,虚函数与派生类相结合,使C++能支持运行时(动态)多态性,实现在基类中定义派生类所拥有的通用“接口”,而在派生类中定义具体的实现方法,即“一个接口,多种方法”。 ㈡虚函数的定义 1.在基类中定义 在定义函数的前面加上“virtual ”。即: virtual 返回类型函数名(参数表) { …… } 2.在派生类中定义 函数的返回类型、函数名、参数的个数、参数类型及顺序必须与基类中的原型完全相同。 3.说明: ⑴在派生类中定义虚函数时,可用“virtual”也可不用“virtual”(最好都使用)。 ⑵虚函数在派生类中重新定义时,其原型必须与基类中相同。 ⑶必须用基类指针访问虚函数才能实现运行时(动态)多态性;当用普通成员函数的调用方法(即用圆点运算符)调用虚函数时,为静态调用; ⑷虚函数在自身类中必须声明为成员函数(不能为友元函数或静态成员函数),但在另一个类中可以声明为友元函数。 ⑸虚函数可以公有继承多次,其虚函数的特性不变。 ⑹构造函数不能定义为虚函数,但析构函数可以定义为虚函数。 ⑺虚函数与重载函数的关系 ①普通函数重载是通过参数类型或参数的个数不同实现的;重载一个虚函数时,其函数原型(返回类型、参数个数、类型及顺序)完全相同。 ②当重载的虚函数只有返回类型不同时,系统将给出错误信息;如果定义的虚函数只有函数名相同,而参数个数或类型不同时,则为普通函数重载。 ㈢纯虚函数与抽象类 1.纯虚函数定义 格式:

相关文档