日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

虚函数(Virtual Function)与 纯虚函数(Pure Virtual Function)

發布時間:2024/4/15 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 虚函数(Virtual Function)与 纯虚函数(Pure Virtual Function) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1>虛函數(Virtual Function)

1.1>Base Class

#ifndef Animal_h #define Animal_h #include <string> class Animal { protected:std::string m_strName;// We're making this constructor protected because// we don't want people creating Animal objects directly,// but we still want derived classes to be able to use it.Animal(std::string strName): m_strName(strName){}public:std::string GetName() { return m_strName; }virtual const char* Speak() { return "???"; }//virtual const char* Speak()=0; };#endif

1.2>Derived Class 1

#ifndef Cat_h #define Cat_h #include"Animal.h" class Cat: public Animal { public:Cat(std::string strName): Animal(strName){}virtual const char* Speak() { return "Meow"; } }; #endif

1.3>Derived Class 2

#ifndef Dog_h #define Dog_h #include"Animal.h" class Dog: public Animal { public:Dog(std::string strName) : Animal(strName){}virtual const char* Speak() { return "Woof"; } }; #endif

1.4>Main Class

#include<iostream> #include"Cat.h" #include"Dog.h" using namespace std; int main(){Cat cCat("cat");cout<<cCat.GetName()<<" says "<<cCat.Speak()<<endl;Dog cDog("dog");cout<<cDog.GetName()<<" says "<<cDog.Speak()<<endl;return 0; }

Result is:


運行結果正常。

1.5>Derived Class 3

#ifndef Cow_h #define Cow_h #include"Animal.h" class Cow: public Animal { public:Cow(std::string strName): Animal(strName){}// We forgot to redefine Speak//virtual const char* Speak(){ return "Moo";} }; #endif

在這里我們忘記定義Base Class Animal的Speak()函數了。

而Main函數改為下面的形式:

#include<iostream> #include"Cow.h" using namespace std; int main(){Cow cCow("cow");cout<<cCow.GetName()<<" says "<<cCow.Speak()<<endl;return 0; }

Result is:

What happened? We forgot to redefine Speak, so cCow.Speak() resolved to Animal.Speak(), which isn’t what we wanted.(我們忘記重新定義Speak函數了,所以cCow.Speak()解析為基類Animal的Speak()函數了,這不是我們所期望的。)

A better solution to this problem is to use a pure virtual function:
(解決這個問題的一個更好的方法是使用純虛函數)

2>純虛函數(Pure Virtual Function)

將Animal中的Speak()聲明改成如下純虛函數形式:

virtual const char* Speak()=0;

然后重新編譯1.5中的程序,出現如下錯誤:

1>d:\visual studio 2008\projects\cnn_finite_wordlength_emulator\purevirtualfunctiontest\purevirtualfunctiontest\main.cpp(5) : error C2259: 'Cow' : cannot instantiate abstract class 1> due to following members: 1> 'const char *Animal::Speak(void)' : is abstract 1> d:\visual studio 2008\projects\cnn_finite_wordlength_emulator\purevirtualfunctiontest\purevirtualfunctiontest\animal.h(19) : see declaration of 'Animal::Speak'

大致意思是:

error C2259 ‘Cow’:不能實例化抽象類? , 因為'const char *Animal::Speak(void)' 是抽象的,請查看'Animal::Speak'的聲明。

也就是說,Cow類只有提供Speak的函數實現才可以實例化實例化。

2.1>提供Speak的函數實現

#ifndef Cow_h #define Cow_h #include"Animal.h" class Cow: public Animal { public:Cow(std::string strName): Animal(strName){}// Cow provides a body for Speak().virtual const char* Speak(){ return "Moo";} }; #endif


再次編譯并運行,結果如下:

A pure virtual function is useful when we have a function that we want to put in the base class, but only the derived classes know what it should return. A pure virtual function makes it so the base class can not be instantiated, and the derived classes are forced to define these function before they can be instantiated. This helps ensure the derived classes do not forget to redefine functions that the base class was expecting them to.


URL:http://www.learncpp.com/cpp-tutorial/126-pure-virtual-functions-abstract-base-classes-and-interface-classes/






總結

以上是生活随笔為你收集整理的虚函数(Virtual Function)与 纯虚函数(Pure Virtual Function)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。