Virtual functions and pure virtual functions

Source: Internet
Author: User

Original article link

Functions of pure virtual functions

In many cases, virtual functions cannot be meaningful in the base class, but declared as pure virtual functions. Its implementation is left toDerived class.

1. First, emphasize a concept
Defining a function as a virtual function does not mean that the function is not implemented. It is defined as a virtual function to allow the base class pointer to call this function of the subclass.
Defining a function as a pure virtual function means that the function is not implemented. It is defined to implement an interface and act as a specification, which inherits this specification. Class Program The member must implement this function.

2. instantiate a class:
Classes with pure virtual functions cannot generate class objects. If they do not have pure virtual functions, they can. For example:
Class ca
{
Public:
Virtual void fun () = 0; // The Fun function is a pure virtual function.
Virtual void fun1 ();
};

Class CB
{
Public:
Virtual void fun ();
Virtual void fun1 ();
};

// Implementation of Ca and CB classes
...

Void main ()
{
Ca a; // not allowed, because the class CA has pure virtual functions
Cb B; // yes, because CB class does not have pure virtual functions
...
}

3. Use of virtual functions in the middle of polymorphism:
Polymorphism is generally implemented by pointing to the base class pointer.

4. One thing you must understand is to use the parent class pointer to call the subclass at runtime:
For example, a function is as follows:
Void animal: fun1 (animal * maybedog_maybehorse)
{
Maybedog_maybehorse-> born ();}
The maybedog_maybehorse parameter does not know whether it is a dog class or a horse class at the time of compilation. Therefore, it is set to an animal class. The function is used only when it is determined by the runtime. That is to say, the parent class pointer is used to determine who the runtime is directed to through the virtual function.5. Use virtual functions
# Include <iostream. h>

Class animal
{
Public:
Animal ();
~ Animal ();
Void fun1 (animal * maybedog_maybehorse );
Virtual void born ();
};

Void animal: fun1 (animal * maybedog_maybehorse)
{
Maybedog_maybehorse-> born ();
} Animal: Animal (){}
Animal ::~ Animal (){}
Void animal: Born ()
{
Cout <"animal ";
}
/// // Horse
Class horse: Public animal
{
Public:
Horse ();
~ Horse ();
Virtual void born ();
};

Horse: Horse (){}
Horse ::~ Horse (){}
Void horse: Born (){
Cout <"horse ";
}
/// // Main
Void main ()
{
Animal;
Horse B;
A. fun1 (& B );
}

// Output: Horse

6. No virtual functions
# Include <iostream. h>
Class animal
{
Public:
Animal ();
~ Animal ();
Void fun1 (animal * maybedog_maybehorse );
Void born ();
};

Void animal: fun1 (animal * maybedog_maybehorse)
{
Maybedog_maybehorse-> born ();
}

Animal: Animal (){}

Animal ::~ Animal (){}
Void animal: Born ()
{
Cout <"animal ";
}
/// // Horse
Class horse: Public animal
{
Public:
Horse ();
~ Horse ();
Void born ();
};

horse: Horse () {}< br> horse ::~ Horse () {}< br> void horse: Born () {
cout <"horse ";
}< BR >//// // main
void main ()
{< br> animal a;
horse B;
. fun1 (& B);
}< br> // output: Animal

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.