虚函数是指在基类中使用了vitual申明,并且在一个或多个派生类中被重新定义的函数。-> 每个派生类可以拥有自己的虚函数定义。
- C++根据指针指向对象的类型来决定调用虚函数的哪个定义,这种定义实在运行时作出的。
- 当虚函数在派生类中重新定义时,关键字virtual 不需要重复。(重复也不是错误)
;虚函数的继承: 无论经过多少层的继承函数,都是虚函数。
- 包含虚函数的类被称为多态类。
;虚函数+继承,使C++支持运行多态.(Polymorphism)
#include <iostream>
using namespace std;
class base{
public:
virtual void who(){ //申明虚函数
cout<<"Base\n";
}
};
class derived1:public base{
public:
void who(){ //重新定义 derived1 中的who()
cout<<"derived1 class\n";
}
};
class derived2:public base{
public:
void who(){ //重新定义 derived2 中的who()
cout<<"derived2 class\n";
}
};
int main(){
base base_obj;
base *p;
derived1 derived1_obj;
derived2 derived2_obj;
p=&base_obj;
p->who();
p=&derived1_obj;
p->who();
p=&derived2_obj;
p->who();
return 0;
}
输出结果:
Base
derived1 class
derived2 class
如果在上述代码中删除virtual,将输出
Base
Base
Base
在类base中, 函数who()被申明为虚函数,说明该函数可以在派生类中重新定义。
因此,derived1,derived2中的 who()都重新被定义了。
由于who()被声明为虚函数,在运行时,C++根据p所指向的对象类型决定who()的那个定义将被调用.
同时,由于基类的指针可以指向任何派生类的对象。
重载(Overloading)一般的函数和重新定义虚函数(Overriding,覆盖/重写)是有差异的.
Overloading is a method that allows defining multiple member functions with the same name but different signatures.The compiler will pick the correct function based on the signature.
Overriding is a method that allows the derived class to redefine the behavior of member functions which the derived class inherits from the base class.The signatures of both base class member function and derived class member function are the same; however, the implementation and behavior will differ.
- 重载函数必须在参数的类型和数量上不同,覆盖函数(重新定义虚函数)在参数的类型而后数量上必须相同。
为了理解多态(一种接口,多种方法)的强大内涵,参考如下代码.
#include <iostream>
using namespace std;
//基类figure:保存图形的尺寸,并且可以用于计算其面积show_area().
class figure{
protected:
double x,y;
public:
void set_dim(double i,double j){
x=i;
y=j;
}
virtual void show_area(){ //采用虚函数,可以在派生类中覆盖(Overloading)面积计算.
cout<<"No area computation defined for this class.\n";
}
};
class triangle:public figure{
public:
void show_area(){
cout<<"Triangle with base "<<x;
cout<<" and height "<<y;
cout<<" has an area of "<<x*y*0.5<<".\n";
}
};
class rectangle:public figure{
public:
void show_area(){
cout<<"Rectangle with dimensions ";
cout<<x<<","<<y;
cout<<" has an area of ";
cout<<x*y<<".\n";
}
};
int main(){
figure *p; //声明基类的指针
triangle tri_obj; //声明派生类对象
rectangle rect_obj;
p=&tri_obj;
p->set_dim(6.0,8.0);
p->show_area(); //将显示三角形面积 24
p=&rect_obj;
p->set_dim(10.0,3.0);
p->show_area(); //将显示长方形面积 30
return 0;
}
Q1. What is the difference between non-virtual and virtual functions?
A 1. The behavior of a non-virtual function is known at compile time while the behavior of a virtual function is not known until the run time.
Q2. What is a pure virtual function?
A2. "A pure virtual function is a function declared in a base class that has no definition relative to the base."
Q3. What is an abstract base class?
A3. It is a class that has one or more pure virtual functions.
Q4. What is pure virtual function? or what is abstract class?
A4. When you define only function prototype in a base class without implementation and do the complete implementation in derived class. This base class is called abstract class and client won't able to instantiate an object using this base class.
You can make a pure virtual function or abstract class this way..
分享到:
相关推荐
Linux运维-运维系统服务04-Shell脚本d6-Shell函数与正则表达式-10函数中return作用1.mp4
return this 马克-to-win Java视频this指针的详细介绍
下面是一个简单的示例,演示如何将函数作为参数传递给其他函数: ```python def add(a, b): return a + b def subtract(a, b): return a - b def apply(func, a, b): return func(a, b) print(apply(add, 2, 3)) # ...
C++中按值返回和返回值优化的程序代码
python-return函数用法 - 免费 让你看懂!
动态动态虚拟滚动 React组件可用于在任何页面上实现虚拟滚动,而无需担心动态项目的高度。 您可以在这里使用库: 安装 npm install react - dynamic - virtual - scroll ... return ( < div className = "L
Inside the Java Virtual Machine Bill Venners $39.95 0-07-913248-0 Inside the Java Virtual Machine Acknowledgments Introduction Part One: Java's Architecture 1 Introduction to Java's Architecture Why ...
Lab2-Return-To-Libc,包含代码、实验说明、相关论文。
这是一个电脑鼠走迷宫(大迷宫)中原路返回的程序,驱动也做过一定的调整,可用于测试
使用时,将虚拟函数附加到猫鼬查询的结果中。 。 用法 const mongooseLeanVirtuals = require ( 'mongoose-lean-virtuals' ) ; // Example schema const userSchema = new mongoose . Schema ( { name : String } )...
A novel packet format with non-return-to-zero (NRZ) label and duobinary carrier-suppressed return-to-zero (DCS-RZ) payload is proposed for optical packet switching networks. NRZ label is followed by ...
Bypassing non-executable-stack during exploitation using return-to-libc.pdf
复旦大学_软件安全_SEED labs_2-Return_to_libc实验 是从雪城大学SEED labs上找的实验 资源包括:原始文件夹、攻击代码、实验报告详细版
22-函数-return.rar
Return to a healthy L-term growth trend.pdf
函数参数和返回值 2-1 函数参数 ...函数的值只能通过return语句返回主调函数。 return语句的一般形式为: return 表达式; //形式1 return (表达式); //形式2 return; //形式3 2-2 函数的返回值 return语句的作
使用函数 generate_password_hash 存储密码的哈希值 将用户名添加到数据库 检查用户名是否已存在于数据库中,如下所示: result = db.execute(...) 如果不是结果: return apology("...") db.execute("INSERT INTO ...
第5章 函数与模块 Python基础编程与实践教学课件第5章--函数与模块全文共31页,当前为第1页。 CONTENTS 目 录 5.1 函数的定义和调用 01 5.2 ...在函数体中可以使用return [表达式] 来结束函数,有选择性地返回一个值
- FIX: The TFlexPanel.FindControlAtPoint method maked virtual to realize RealTime-capability when on mouse cursor moving the flex-object search not occurs. - FIX: After deleting the selected points ...
#include int Add(int a,int b){ int z; z=a+b; return z; } int main(){ int num1=10; int num2=12; int c=Add(num1,num2); printf("%d\n",c); return 0; }