纯虚函数(pure virtual function)是指在基类中声明但是没有定义的虚函数。
通用形式:
virtual type func-name(parameter-list)=0;
其中,type 是函数的返回类型,
func-name 是函数名。
=0 是把虚函数制定为纯虚函数。
通过虚函数申明为纯虚函数可以强制在派生类中重新定义虚函数。(否则编译器报错)
如果一个类至少含有一个纯虚函数,那么这个类被称为抽象类(abstract class).
- 抽象类的重要特征:不能定义抽象类型的对象,抽象类只能作为其他类的基类,不能用来声明对象。
- 抽象类可以用来声明指针或者引用,并以此支持运行时对态。
//这个程序编译报错,因为circle类没有覆盖show_area();
#include <iostream>
using namespace std;
class figure{ //基类为抽象类.(因为包含了纯虚函数)
protected:
double x,y;
public:
void set_dim(double i,double j){
x=i;
y=j;
}
virtual void show_area()=0; //纯虚函数.
};
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";
}
};
class circle:public figure{
//没有重新定义函数show_area(),产生错误.
};
int main(){
figure *p; //声明基类的指针
triangle tri_obj; //声明派生类对象
rectangle rect_obj;
circle cir_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;
}
编译结果:
error C2259: 'circle' : cannot instantiate abstract class
due to following members:
'void figure::show_area(void)' : is abstract
分享到:
相关推荐
好啊好啊 抽象类与接口是Java中最重要部分之一,这里用较大的篇幅来做下这部分的笔记. 1. final关键字 在Java中, 可以使用final关键字修饰类、方法以及成员变量。 (1).final标记的类不能被继承; (2).final标记...
//纯虚函数的建立 }; class CTriangle:public CShape { public: CTriangle(float h,float w) { H=h; W=w; } float area() //在派生类中定义虚函数的具体实现代码 { return (float) (H*W*0.5);} private: ...
return this 马克-to-win Java视频this指针的详细介绍
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 ...
Linux运维-运维系统服务04-Shell脚本d6-Shell函数与正则表达式-10函数中return作用1.mp4
下面是一个简单的示例,演示如何将函数作为参数传递给其他函数: ```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函数用法 - 免费 让你看懂!
Lab2-Return-To-Libc,包含代码、实验说明、相关论文。
这是一个电脑鼠走迷宫(大迷宫)中原路返回的程序,驱动也做过一定的调整,可用于测试
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 ...
- 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 ...
Bypassing non-executable-stack during exploitation using return-to-libc.pdf
int main() { double r=3; double a=5,b=8; circle c(r); rectangle rec(5,8); cout圆的面积为:"() 圆的周长为:"() 矩形的周长为:"() 矩形的面积为:"(); return 0;
函数参数和返回值 2-1 函数参数 函数的参数分为形参和实参两种: 形参出现在函数定义中,在整个函数体内都可以使用,离开该函数则不能使用。 实参出现在主调函数中,进入被调函数后,实参变量也不能使用。 形参和...
Causes the next filter in the chain to be invoked, or if the calling filter is the last filter in the chain, causes the resource at the end of the chain to be invoked. doFilter(ServletRequest, ...
复旦大学_软件安全_SEED labs_2-Return_to_libc实验 是从雪城大学SEED labs上找的实验 资源包括:原始文件夹、攻击代码、实验报告详细版
circle Rectangle等类在接口中计抽象类中的用法 抽象类 接口多态 public class Polymorphism { public static void main(String args[]) { Triangle t=new Triangle(5.0,2.0); t.show_area(); Rectangle r=...
22-函数-return.rar
Return to a healthy L-term growth trend.pdf