[Home]  [Edit this page]  [Recent Changes]  [Special Pages]  [Help
CppVirtual

(C++) virtual

Keyword helping you designing derived classes. A virtual function in the base class hints that this can be overloaded in the derived class. Or that it has to be defined in the derived class.

The C++ keyword virtual is not supported in C.

Example

  1. include <iostream>
class MyBaseClass { public: MyBaseClass() { ; } virtual void sayHello() const = 0; //MUST be defined in derived class }; class MyDerivedClass : public MyBaseClass { public: MyDerivedClass() { ; } void sayHello() const { std::cout << "Hello world" << std::endl; } }; int main() { //MyBaseClass myBaseClass; //Will give error below MyDerivedClass myDerivedClass; myDerivedClass.sayHello(); }


The line with the error gives the following error when uncommenting it:

cannot declare variable `myBaseClass' to be of type `MyBaseClass' 
because the following virtual functions are abstract: 
virtual void MyBaseClass::sayHello() const


Topic links

'virtual' links



last edited (November 18, 2006) by bilderbikkel, Number of views: 3386, Current Rev: 6 (Diff)

[Edit this page]  [Page history]  [What links here]  [Discuss this topic]  [Printer Friendly]  

Members

Username:

Password:


Register
Forgot Password?




Programmers Heaven - for .NET, Java, C/C++ and WEB Developers!
© 1996-2008 Community Networks Ltd. All rights reserved. Reproduction in whole or in part, in any form or medium without express written permission is prohibited. Violators of this policy may be subject to legal action. Please read Terms Of Use and Privacy Statement for more information. Development by Tore Nestenius at .NET Consultant - Synchron Data.