Virtual members 虚函数
来源:互联网 发布:访客网络怎么设置 编辑:程序博客网 时间:2024/06/05 06:35
A member of a class that can be redefined in its derived classes is known as a virtual member. In order to declare a member of a class as virtual, we must precede its declaration with the keyword virtual:
Now the three classes (CPolygon, CRectangle and CTriangle) have all the same members:width,height,set_values() and area().
The member function area() has been declared as virtual in the base class because it is later redefined in each derived class. You can verify if you want that if you remove thisvirtual keyword from the declaration ofarea() withinCPolygon, and then you run the program the result will be0 for the three polygons instead of20,10 and 0. That is because instead of calling the correspondingarea() function for each object (CRectangle::area(),CTriangle::area() andCPolygon::area(), respectively),CPolygon::area() will be called in all cases since the calls are via a pointer whose type isCPolygon*.
Therefore, what the virtual keyword does is to allow a member of a derived class with the same name as one in the base class to be appropriately called from a pointer, and more precisely when the type of the pointer is a pointer to the base class but is pointing to an object of the derived class, as in the above example.
A class that declares or inherits a virtual function is called a polymorphic class.
Note that despite of its virtuality, we have also been able to declare an object of typeCPolygon and to call its ownarea() function, which always returns 0.
// virtual members#include <iostream>using namespace std;class CPolygon { protected: int width, height; public: void set_values (int a, int b) { width=a; height=b; } virtual int area () { return (0); } };class CRectangle: public CPolygon { public: int area () { return (width * height); } };class CTriangle: public CPolygon { public: int area () { return (width * height / 2); } };int main () { CRectangle rect; CTriangle trgl; CPolygon poly; CPolygon * ppoly1 = ▭ CPolygon * ppoly2 = &trgl; CPolygon * ppoly3 = &poly; ppoly1->set_values (4,5); ppoly2->set_values (4,5); ppoly3->set_values (4,5); cout << ppoly1->area() << endl; cout << ppoly2->area() << endl; cout << ppoly3->area() << endl; return 0;}
20100
Now the three classes (CPolygon, CRectangle and CTriangle) have all the same members:width,height,set_values() and area().
The member function area() has been declared as virtual in the base class because it is later redefined in each derived class. You can verify if you want that if you remove thisvirtual keyword from the declaration ofarea() withinCPolygon, and then you run the program the result will be0 for the three polygons instead of20,10 and 0. That is because instead of calling the correspondingarea() function for each object (CRectangle::area(),CTriangle::area() andCPolygon::area(), respectively),CPolygon::area() will be called in all cases since the calls are via a pointer whose type isCPolygon*.
Therefore, what the virtual keyword does is to allow a member of a derived class with the same name as one in the base class to be appropriately called from a pointer, and more precisely when the type of the pointer is a pointer to the base class but is pointing to an object of the derived class, as in the above example.
A class that declares or inherits a virtual function is called a polymorphic class.
Note that despite of its virtuality, we have also been able to declare an object of typeCPolygon and to call its ownarea() function, which always returns 0.
- Virtual members 虚函数
- 虚函数 (virtual)
- virtual 虚函数
- virtual虚函数
- virtual虚函数
- 关于virtual虚函数
- C#虚函数virtual
- virtual纯虚函数
- c++ (virtual)虚函数
- 虚函数virtual
- virtual 虚函数
- virtual虚函数
- 虚函数 virtual
- virtual虚函数
- C++ 虚函数virtual
- virtual虚函数
- virtual-虚函数
- 虚函数 virtual
- 数据说Digg的兴衰史 从辉煌到没落
- C++关于CFileDialog的更改路径问题及解决方法
- CreateProcess c
- perl的子程序sub返回值不改变的解决方法
- C# 线程的挂起与恢复
- Virtual members 虚函数
- double转换成NSString
- webservice发布方式
- Objective-C中一些 值得程序员注意的地方 学习笔记
- 如何用美剧真正提升你的英语水平
- Web前端:11个让你代码整洁的原则
- iOS annotation
- vbs sendkey
- mark