Core C++

Inheritance

Simple Inheritance


Example 1

#include <iostream>

// The base class has a single member variable
class CBase
{
public:
    double mdBase;
};

// Derived inherits the base class variable
class CDerived : public CBase
{
public:
    double mdDerived;
};

int main()
{
    using namespace std;

    CDerived qMyObject;

    // The derived object has both base and derived members
    qMyObject.mdBase    = 1.0;
    qMyObject.mdDerived = 2.0;

    cout << "Base = " << qMyObject.mdBase << endl;
    cout << "Derived = " << qMyObject.mdDerived << endl;

    return 0;
}


Output:



Example 2

#include <iostream>

// This base class has a single member function
class CBase
{
public:
    void PrintBase() {
        std::cout << "Base" << std::endl;
    }
};

// Derived inherits the base class function
class CDerived : public CBase
{
public:
    void PrintDerived() {
        std::cout << "Derived" << std::endl;
    }
};

int main()
{
    using namespace std;

    CDerived qMyObject;

    // The derived object can call both functions
    qMyObject.PrintBase();
    qMyObject.PrintDerived();

    return 0;
}


Output:



Constructors and Destructors


Example 1

#include <iostream>

// Base class
class CBase
{
public:
    CBase() {
        std::cout << "Base Constructor" << std::endl;
    }
    ~CBase() {
        std::cout << "Base Destructor" << std::endl;
    }
};

// First derived class
class CDerived1 : public CBase
{
public:
    CDerived1() {
        std::cout << "Derived1 Constructor" << std::endl;
    }
    ~CDerived1() {
        std::cout << "Derived1 Destructor" << std::endl;
    }
};

// Second derived class inheriting the first
class CDerived2 : public CDerived1
{
public:
    CDerived2() {
        std::cout << "Derived2 Constructor" << std::endl;
    }
    ~CDerived2() {
        std::cout << "Derived2 Destructor" << std::endl;
    }
};

int main()
{
    using namespace std;

    // Instantiation calls the constructors from base to most derived.
    CDerived2 qMyObject;

    // The destructors are called in reverse order upon exit
    return 0;
}


Output:



Example 2

#include <iostream>

// Base class
class CBase
{
public:
    CBase() {
        std::cout << "Default Base" << std::endl;
    }
    CBase(int i) {
        std::cout << "Base with value = " << i << std::endl;
    }
};

// Derived class
class CDerived : public CBase
{
public:
    // The base constructor is called with the argument 3
    CDerived() : CBase(3) {
        std::cout << "Default Derived" << std::endl;
    }
    // The base default constructor called since none is specified
    CDerived(int i) {
        std::cout << "Derived  with value = " << i << std::endl;
    }
};

int main()
{
    using namespace std;
    // Illustration of default and int argument constructors
    CDerived qDefaultObject;
    CDerived qIntArgObject(2);

    return 0;
}


Output:



Multiple Inheritance


Example 1

#include <iostream>

// First base class
class CBase1
{
public:
    CBase1() {
        std::cout << "Base1" << std::endl;
    }
};

// Second base class
class CBase2
{
public:
    CBase2() {
        std::cout << "Base2" << std::endl;
    }
};

// Derived class inherits both base classes
class CDerived : public CBase1, public CBase2
{
public:
    CDerived() {
        std::cout << "Derived" << std::endl;
    }
};

int main()
{
    using namespace std;

    // A derived object which inherits both bases
    CDerived qMyObject;

    return 0;
}


Output:



Example 2

#include <iostream>

// Base class
class CBase
{
public:
    CBase() {
        std::cout << "Base" << std::endl;
    }
};

// First derived class with virtual inheritance
class CDerived1 : virtual public CBase
{
public:
    CDerived1() {
        std::cout << "Derived1" << std::endl;
    }
};

// Second derived class with virtual inheritance
class CDerived2 : virtual public CBase
{
public:
    CDerived2() {
        std::cout << "Derived2" << std::endl;
    }
};

// Derived class inheriting both derived classes
class CDerived3 : public CDerived1, public CDerived2
{
public:
    CDerived3() {
        std::cout << "Derived3" << std::endl;
    }
};

int main()
{
    using namespace std;

    // Instantiate all three derived classes
    CDerived1 qMyObject1;
    CDerived2 qMyObject2;
    // Although both derived inherited classes inherit the
    // base, the inheritance is virtual. So, the third
    // derived class only inherits the base once instead
    // of twice.
    CDerived3 qMyObject3;

    return 0;
}


Output:



 

© 2007–2024 XoaX.net LLC. All rights reserved.