C ++ virtual functions pure virtual virtual base class (Virtual)

C ++ virtual functions pure virtual virtual base class (Virtual)
Introduction : the need to understand the difference between the three, the three necessary conditions that must be mastered polymorphic:

  1. inherit
  2. Overload
  3. Parent pointer to the subclass object.

Pure virtual function virtual base class virtual function between the three types

1.虚函数是用于多态中virtual修饰父类函数,确保父类指针调用子类对象时,运行子类函数的。
2.纯虚函数是用来定义接口的,也就是基类中定义一个纯虚函数,基类不用实现,让子类来实现。
3.虚基类是用来在多继承中,比如菱形继承中,如果两个父类继承自同一个类,就只实例化一个父类

① virtual function
first is not polymorphic (only inherited) general ways:

class A  
{  
public:  
    void printf(){  
        cout<<"printf A"<<endl;  
    }  
};  
class B : public A  
{  
public:  
    void printf(){  
        cout<<"printf B"<<endl;  
    }  
};  
int main(int argc, const char * argv[])  
{  
    A *a = new A();  
    a->printf();  
    B *b = new B();  
    b->printf();  
    return 0;  
}  

result:

printf A
printf B

This is not the early polymorphic code Disadvantages: code redundancy
Here is a multi-state but does not refer to virtual keyword:

int main(int argc, const char * argv[])  
{  
    A *a = new B();  
    a->printf();  
    return 0;  
}  

result:

printf A

Because the class definition are the same, so I can not write out, when the pointer to the parent child class object, if you do not use virtual, call the parent class method or when a parent calls his method, do not call the subclass overridden method, so it did not realize the multi-state action, let us try to join in the parent class to see the virtual keyword:

class A  
{  
public:  
virtual void printf(){  
        cout<<"printf A"<<endl;  
    }  
};  
class B : public A  
{  
public:  
//子类也可以不使用virtual,直接写为void printf()
    virtual void printf(){  
        cout<<"printf B"<<endl;  
    }  
};  
int main(int argc, const char * argv[])  
{  
    A *a = new B();  
    a->printf();  
    return 0;  
}  

result

printf B

is added to the virtual parent class, subclass code try plus virtual (conveniently implemented inherited subclass polymorphism), may not be added, or the main function of the parent class object pointer to the child, and finally print the result subclasses override method, so confirmed virtual function is a virtual function of the polymorphic modification of the parent class rewritten, to ensure runtime calls the parent pointer subclass subclass object function.
② pure virtual function
pure virtual function is the abstract interface, using pure virtual functions can not be instantiated, defines a pure virtual function without writing implement pure virtual functions, implemented by subclasses, see the following codes:

class A  
{  
public:  
    virtual void printf() =0;  
};  
void A::printf()//纯虚函数可以不写实现  
{  
    cout<<"printf A"<<endl;  
}  
class B : public A  
{  
public:  
    void printf(){  
        cout<<"printf B"<<endl;  
    }  
};  
int main(int argc, const char * argv[])  
{  
    A *a =new A();//编译出错,纯虚函数的类不能实例化  
    a->printf();  
    return 0;  
}  

virtual void printf () = 0; This virtual function is written, I wrote the following virtual functions to achieve void A :: printf (), in fact, write do not write does not matter, wrote can not act, then I was main try it functions pure virtual function instance of the class, the results being given directly, is not a pure virtual function described instantiated.

int main(int argc, const char * argv[])  
{  
    A *a =newB();//这里使用了多态  
    a->printf();  
    return 0;  
}  

result:

printf B

The main function of a pointing object subclass, the results can be printed out correctly for subclasses. Thus pure virtual function is described as a polymorphic and services, its role is to define an interface, allow subclasses to achieve.
③ virtual base class
virtual base class is c ++ something unique, because c ++ has multiple inheritance, but also related to the definition of virtual keyword.

First is that the multiple inheritance, if Grandpa class (parent class was temporarily parent class is tentatively scheduled for grandpa class), the parent class inherited from my grandfather class. If the Sun class inherits from more than one parent class (sounds a bit weird), so if you do not use the virtual base class, Grandpa will instantiate multiple class object (getting more and more bizarre), the compiler will complain that there is ambiguity. If the parent class inherited from the virtual base class, you can solve the problem of more than one parent class does not instantiate multiple Grandpa, is only a grandfather.

class Grandfather{  
public:  
    int flag;  
    Grandfather(){  
        flag = 1;  
    }  
};  
class Father1:public Grandfather{  
public:  
    Father1(){  
        flag = 2;  
    }  
};  
class Father2:public Grandfather{  
public:  
    Father2(){  
        flag = 3;  
    }  
};  
class Son:public Father1,public Father2{  
};  
int main(int argc, const char * argv[])  
{  
    Son *son = new Son();  
    cout<<son->flag<<endl;//这里编译错误,flag访问不明确,因为两个父类中都有flag变量,歧义  
    return 0;  
}  

If no virtual base class inherits from more than one parent class with a grandfather, will be ambiguous, in order not to ambiguity, the code can be changed (palliative):

cout<<son->Father1::flag<<endl;
cout<<son->Father2::flag<<endl;

If the parent class inherits from virtual base classes is different:

class Grandfather{  
public:  
    int flag;  
    Grandfather(){  
        flag = 1;  
        cout<<"Grandfather flag = "<<flag <<endl;  
    }  
};  
class Father1:virtual public Grandfather{  
public:  
    Father1(){  
        flag = 2;  
        cout<<"Father1 flag = "<<flag<<endl;  
    }  
};  
class Father2:virtual public Grandfather{  
public:  
    Father2(){  
        flag = 3;  
        cout<<"Father2 flag = "<<flag<<endl;  
    }  
};  
class Son:public Father1,public Father2{  
};  
int main(int argc, const char * argv[])  
{  
    Son *son = new Son();  
    cout<<son->flag<<endl;  
    return 0;  
}  

result:

Grandfather flag = 1

Father1 flag = 2

Father2 flag = 3

3

Now, you can run, class Father2: virtual public Grandfather, virtual base class that inherits the wording, the object is only a grandfather, grandfather class variable is instantiated only once, and that is why the final print out 3 it? See order of construction can be seen, now the grandfather class configuration, the first configuration and then inherit parent class, and finally inherits the second parent class inheritance, so the last flag is maintained at the modified value of the second parent Lane.

C ++ virtual functions pure virtual virtual base class (Virtual)In general, virtual function is a pure virtual function in order polymorphic services, virtual base class is instantiated only once for the base class exists

Guess you like

Origin blog.51cto.com/14233078/2451973