virtual base class

来源:互联网 发布:php设计模式 编辑:程序博客网 时间:2024/05/18 11:11
#include <iostream>


using namespace std;


class base{
private:
public:

void show(){cout << "In base, show()\n";}

// virtual void show(){cout << "In base, show()\n";}

};


class father: virtual public base{
//class father:  public base{
private:
public:
//virtual void show(){cout << "In father, show()\n";}
};


class mother: virtual public base{
//class mother: public base{
private:
public:
//virtual void show(){cout << "In mother, show()\n";}
};


class child: public father, public mother{
private:
public:
//virtual void show(){cout << "In child, show()\n";}
};


int main()
{
        child ch;
        ch.show();
        return 0;

}

====================================================================================================================================

In base, show()

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

#include <iostream>


using namespace std;


class base{
private:
public:
void show(){cout << "In base, show()\n";}
};


class father: virtual public base{
//class father:  public base{
private:
public:
virtual void show(){cout << "In father, show()\n";}
};


class mother: virtual public base{
//class mother: public base{
private:
public:
virtual void show(){cout << "In mother, show()\n";}
};


class child: public father, public mother{
private:
public:
using base::show;
void show(int){
                cout << "In child, show()\n";
        }
};


int main()
{
        child ch;
        ch.show();
        return 0;
}

====================================================================================================================================

In base, show()

原创粉丝点击