Sunday, January 13, 2013

Virtual Inheritance

Virtual inheritance:
                        Inheritance can be private (where even the public fields of the base class are treated as private), public (where the base class determines visibility), or virtual. With virtual inheritance there is only one copy of each object even if (because of multiple inheritance) the object appears more than once in the hierarchy. In the following example, derived1a and derived1b each contain a base object, butderived2 contains just one field.

#include <iostream>
using namespace std;

class base{
    public:
        int i;
};

class derived1a: virtual public base {
    public:
        int j;
};

class derived1b: virtual public base {
    public:
        int k;
};

class derived2: public derived1a, public derived1b{
    public:
        int product(){
            return i*j*k;
        }
};

int main()
{
    derived2 obj;
    obj.i = 10;
    obj.j = 3;
    obj.k = 5;
   
    cout << "\n product is " << obj.product() << '\n';
    return 0;
}

OUTPUT:

1 comment:

  1. Football predictions site with the best 100% accurate free football prediction william hill william hill 12bet 12bet 50Big Point Spread Nfl - Shootercasino

    ReplyDelete