[Home]
[Edit this page]
[Recent Changes]
[Special Pages]
[Help]
CppVirtual
The C++ keyword virtual is not supported in C.
The line with the error gives the following error when uncommenting it:
[Edit this page] [Page history] [What links here] [Discuss this topic] [Printer Friendly]
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
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(); }
- include <iostream>
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
- #include
- iostream
- class
- public
- void
- std
- scope operator'::'
- cout
- stream out operator '<<'
- endl
- main
- return
'virtual' links
[Edit this page] [Page history] [What links here] [Discuss this topic] [Printer Friendly]
