Usage between C ++ virtual functions and pure virtual function of C ++ virtual functions and pure virtual function usage differs (rpm)

C ++ virtual functions and pure virtual function usage differs (rpm)

 

1. virtual functions and pure virtual function can be defined in the same class (class), the class containing pure virtual function is referred to as an abstract class (abstract class), but containing only class virtual function (class) can not be called an abstract class (abstract class).

  

2. virtual function can be used directly, can also subclasses (sub class) after overloaded call polymorphic form, and pure virtual function that must be implemented in a subclass of the function (sub class) can only be used in as pure virtual function in the base class (base class) only declared not defined.

 

3. pure virtual functions and virtual functions are in the subclass (sub class) are overloaded to be called polymorphic form.

 

4. A pure virtual functions and virtual functions normally present in the abstract base class (abstract base class -ABC) among inherited subclass overloaded object is to provide a unified interface.

 

The virtual function is defined in the form: virtual {method body}

  Defines a pure virtual function of the form: virtual {} = 0;

Can not have static identifiers in the definition of virtual functions and pure virtual functions, the reason is very simple, modified static function at compile time to ask pre-bind, but virtual functions is dynamic binding (run-time bind), and is two who qualified function life cycle (life recycle) are not the same.

 

6. virtual function must be implemented, if not achieved, the compiler will report an error, the error message is:

error LNK****: unresolved external symbol "public: virtual void __thiscall
ClassName::virtualFunctionName(void)"

7. For the virtual function, the parent class and subclass has its own version. When called by the multi-state mode dynamic binding.

 

8. subclass that implements pure virtual functions, the pure virtual function in the subclass to the programming virtual function, i.e., subclass subclass can override class grandchildren

The virtual function, when called by the multi-state mode dynamic binding.

9. The mechanism of C ++ virtual functions for implementing polymorphism (polymorphism) of. The core idea is to visit the derived class defined by the base class

function

10. polymorphism refer to the same or different object receives different messages have different objects to achieve the same operation when receiving the message. C ++ supports two polymorphisms: compile-time polymorphism, run-time polymorphism.
a compile-time polymorphism: overloaded the function
polymorphism b runtime: realized by virtual functions.

 

11. If a class contains a pure virtual function, then any attempt to instantiate this class statement will cause errors, because the abstract base class (ABC) can not be called directly. It must later be inherited by subclasses overload, the subclass method that calls upon request.

Copy the code
#include<iostream>
using namespace std;

class Virtualbase

{

public:

    virtual void Demon() = 0; //prue virtual function

    virtual void Base() { cout << "this is farther class" << endl; }

};

//sub class

class SubVirtual :public Virtualbase

{

public:

    void Demon() {
        cout << " this is SubVirtual!" << endl;
    }

        void Base() {
            cout << "this is subclass Base" << endl;
        }

    };


        void main()

        {

            Virtualbase* inst = new SubVirtual(); //multstate pointer

            inst->Demon();

            inst->Base();

            // inst = new Virtualbase();

            // inst->Base()
            system("pause");

            return;

        }
Copy the code

 

1. virtual functions and pure virtual function can be defined in the same class (class), the class containing pure virtual function is referred to as an abstract class (abstract class), but containing only class virtual function (class) can not be called an abstract class (abstract class).

  

2. virtual function can be used directly, can also subclasses (sub class) after overloaded call polymorphic form, and pure virtual function that must be implemented in a subclass of the function (sub class) can only be used in as pure virtual function in the base class (base class) only declared not defined.

 

3. pure virtual functions and virtual functions are in the subclass (sub class) are overloaded to be called polymorphic form.

 

4. A pure virtual functions and virtual functions normally present in the abstract base class (abstract base class -ABC) among inherited subclass overloaded object is to provide a unified interface.

 

The virtual function is defined in the form: virtual {method body}

  Defines a pure virtual function of the form: virtual {} = 0;

Can not have static identifiers in the definition of virtual functions and pure virtual functions, the reason is very simple, modified static function at compile time to ask pre-bind, but virtual functions is dynamic binding (run-time bind), and is two who qualified function life cycle (life recycle) are not the same.

 

6. virtual function must be implemented, if not achieved, the compiler will report an error, the error message is:

error LNK****: unresolved external symbol "public: virtual void __thiscall
ClassName::virtualFunctionName(void)"

7. For the virtual function, the parent class and subclass has its own version. When called by the multi-state mode dynamic binding.

 

8. subclass that implements pure virtual functions, the pure virtual function in the subclass to the programming virtual function, i.e., subclass subclass can override class grandchildren

The virtual function, when called by the multi-state mode dynamic binding.

9. The mechanism of C ++ virtual functions for implementing polymorphism (polymorphism) of. The core idea is to visit the derived class defined by the base class

function

10. polymorphism refer to the same or different object receives different messages have different objects to achieve the same operation when receiving the message. C ++ supports two polymorphisms: compile-time polymorphism, run-time polymorphism.
a compile-time polymorphism: overloaded the function
polymorphism b runtime: realized by virtual functions.

 

11. If a class contains a pure virtual function, then any attempt to instantiate this class statement will cause errors, because the abstract base class (ABC) can not be called directly. It must later be inherited by subclasses overload, the subclass method that calls upon request.

Copy the code
#include<iostream>
using namespace std;

class Virtualbase

{

public:

    virtual void Demon() = 0; //prue virtual function

    virtual void Base() { cout << "this is farther class" << endl; }

};

//sub class

class SubVirtual :public Virtualbase

{

public:

    void Demon() {
        cout << " this is SubVirtual!" << endl;
    }

        void Base() {
            cout << "this is subclass Base" << endl;
        }

    };


        void main()

        {

            Virtualbase* inst = new SubVirtual(); //multstate pointer

            inst->Demon();

            inst->Base();

            // inst = new Virtualbase();

            // inst->Base()
            system("pause");

            return;

        }
Copy the code

 

Guess you like

Origin www.cnblogs.com/tsh292278/p/10931493.html