Lecture: Polymorphism

The derived class virtual function with the same name as the function parameter and a base class without virtual virtual function automatically becomes:

1.1. Here need to add virtual function in the base class, the same parameters of the same name of the derived class virtual function was generated automatically.
2.1. If the base class virtual keyword is not added, the addition of the derived class virtual function can not be generated.
3.1. If the base class and the derived class are not added to the virtual keyword, the function call depends on the current pointer to the object is a base class object or a derived class object. (I did not use polymorphism)

If the base class pointer to a derived object:

We will first consider the base class function calling the function pointer, if the base class virtual function is a function, which will consider the function calls the same point in the derived class object.

Lecture Exercise:

A: looks like polymorphism

Topic links: http://cxsjsx.openjudge.cn/hw202006/A/

#include <iostream>
using namespace std;
class B { 
    private: 
        int nBVal; 
    public: 
        void Print() 
        { cout << "nBVal="<< nBVal << endl; } 
        void Fun() 
        {cout << "B::Fun" << endl; } 
        B ( int n ) { nBVal = n;} 
};
class D :public B{
    private:
        int nDVal;
    public:
        D( int n):B(3*n){nDVal=n;}
        void Fun()
        {cout<<"D::Fun"<<endl;}
        void Print()
        {
            B::Print();
            cout<<"nDVal="<<nDVal<<endl;
        }
};
int main() { 
    B * pb; D * pd; 
    D d(4); d.Fun(); //D::Fun
    pb = new B(2); pd = new D(8); 
    pb -> Fun(); pd->Fun(); //B::Fun,D::Fun
    pb->Print (); pd->Print (); //nBVal=2 nBVal=24,nDVal=8
    pb = & d; pb->Fun(); //B::Fun
    pb->Print(); //nBVal=12
    return 0;
}

B:Fun和Do

Topic links: http://cxsjsx.openjudge.cn/hw202006/B/

#include <iostream> 
using namespace std;
class A { 
    private: 
    int nVal; 
    public: 
    void Fun() 
    { cout << "A::Fun" << endl; }; 
    void Do() 
    { cout << "A::Do" << endl; } 
}; 
class B:public A { 
    public: 
    virtual void Do() //B相对与C是基类,使得C中Do成为虚函数
    { cout << "B::Do" << endl;} 
}; 
class C:public B { 
    public: 
    void Do( ) 
    { cout <<"C::Do"<<endl; } 
    void Fun() 
    { cout << "C::Fun" << endl; } 
}; 
void Call(
    B& p
) //先调用p.Fun(),那么由于类B中没有该函数,只好找继承的A中的函数。之后调用调用Do使先考虑B自身的,但由于有virtual关键字,考虑到引用的是一个C类对象,于是调用C类虚函数。
{ 
    p.Fun(); p.Do(); 
} 
int main() { 
    C c; 
    Call( c); 
    return 0;
}

C: What the hell delete

Topic links: http://cxsjsx.openjudge.cn/hw202006/C/

#include <iostream> 
using namespace std;
class A 
{ 
public:
    A() { }
    virtual ~A(){ cout<<"destructor A"<<endl;}//这是虚析构函数的一个例子,基类析构函数前➕virtual,能保证析构一个派生类对象时,先调用派生类析构函数,再调用基类析构函数。
}; 
class B:public A { 
    public: 
    ~B() { cout << "destructor B" << endl; } 
}; 
int main() 
{ 
    A * pa; //如果这里是B*指针,就会自动调用派生类析构函数再调用基类析构函数,无虚析构函数的烦恼。
    pa = new B; 
    delete pa; 
    return 0;
}

D: how is Fun and Do

Topic links: http://cxsjsx.openjudge.cn/hw202006/D/

#include <iostream>
using namespace std;
class A {
    private:
    int nVal;
    public:
    void Fun()
    { cout << "A::Fun" << endl; };
    virtual void Do()
    { cout << "A::Do" << endl; }
};
class B:public A {
    public:
    virtual void Do()
    { cout << "B::Do" << endl;}
};
class C:public B {
    public:
    void Do( )
    { cout <<"C::Do"<<endl; }
    void Fun()
    { cout << "C::Fun" << endl; }
};
void Call(
    A* p
) 
{
    p->Fun(); p->Do();
}
int main() {
    Call( new A()); //A::Fun A::Do
    Call( new C());//A::Fun C::Do 首先考虑Fun和Do能否在基类函数中输出,如果有virtual关键字,再去找它所指向相应对象派生类的函数!
    return 0;
}

Guess you like

Origin www.cnblogs.com/wwk510/p/12465127.html