Inheritance 


Contents of this section:

  1. A simple program demonstrating inheritance
  2. Using protected members
  3. Protected inheritance
  4. Multiple inheritance
  5. Calling base class's constructor in derived class

1. A simple program demonstrating inheritance

    Code Listing: 

// File:    ~ftp/pub/class/cplusplus/Inheritance/SimpleInherit.cpp
// Purpose: A simple program showing inheritance

#include <iostream>
using namespace std;

class base {
  int i, j;
public:
  void set(int a, int b) { i = a; j = b; }
  void show() { cout << i << " " << j << "\n"; }
};

// inheritance
class derived : public base {
  int k;
public:
  derived(int x) { k = x; }
  void showk() { cout << k << "\n"; }
};

int main()
{
  derived ob(3);

  ob.set(1, 2); // access member of base
  ob.show();    // access member of base

  ob.showk();   // uses member of derived class

  return 0;
}

    Running session:

@2:32pm merc-hu[211]% CC -LANG:std SimpleInherit.cpp -o SimpleInherit
@2:33pm merc-hu[212]% SimpleInherit
1 2
3
@2:33pm merc-hu[213]%

2. Using protected members

Code listing:

// File:    ~ftp/pub/class/cplusplus/inheritance/ProtectedMember.cpp
// Purpose: Inherite protected members of the base class

#include <iostream>
using namespace std;

class base {
protected:
  int i, j; // private to base, but accessible to derived
public:
  void set(int a, int b) { i = a; j = b; }
  void show() { cout << i << " " << j << "\n"; }
};

class derived : public base {
  int k;
public:
  // derived may access base's i and j
  void setk() { k = i*j; }

  void showk() { cout << k << "\n"; }
};

int main()
{
  derived ob;

  ob.set(2, 3); // OK, known to derived
  ob.show();    // OK, known to derived

  ob.setk();
  ob.showk();

  return 0;
}

Running session:

@10:48am merc-hu[18]% CC -LANG:std ProtectedMember.cpp -o ProtectedMember
@10:49am merc-hu[19]% ProtectedMember
2 3
6
@10:50am merc-hu[20]%

3. Protected inheritance

    Code Listing: 

// File:    ~ftp/pub/class/cplusplus/Inheritance/ProtectedInheritance.cpp
// Purpose: using protected for inheritance of a base class

#include <iostream>
using namespace std;

class base {
  int i;
protected:
  int j;
public:
  int k;
  void seti(int a) { i = a; }
  int geti() { return i; }
};

// Inherit base as protected.
class derived : protected base {
public:
  void setj(int a) { j = a; } // j is protected here
  void setk(int a) { k = a; } // k is also protected
  int getj() { return j; }
  int getk() { return k; }
};

int main()
{
  derived ob;

  /* This next line is illegal because seti() is
     a protected member of derived, which makes it
     inaccessible outside of derived. */
//  ob.seti(10);

//  cout << ob.geti(); // illegal -- geti() is protected
//  ob.k = 10; // also illegal because k is protected

  // these next statements are OK
  ob.setk(10);
  cout << ob.getk() << ' ';
  ob.setj(12);
  cout << ob.getj() << ' ';

  return 0;
}

    Running session:

@10:59am merc-hu[25]% CC -LANG:std ProtectedInheritance.cpp -o ProtectedInheritance
@10:59am merc-hu[26]% ProtectedInheritance
10 12
@10:59am merc-hu[27]%

กก

4. Multiple inheritance 

    Code Listing: 

// File:   ~ftp/pub/class/cplusplus/Inheritance/MulInhert.cpp
// Purpose: An example of multiple base classes.

#include <iostream>
using namespace std;

class base1 {
protected:
  int x;
public:
  void showx() { cout << x << "\n"; }
};

class base2 {
protected:
  int y;
public:
  void showy() { cout << y << "\n"; }
};

// Inherit multiple base classes.
class derived: public base1, public base2 {
public:
  void set(int i, int j) { x = i; y = j; }
};

int main()
{
  derived ob;

  ob.set(10, 20); // provided by derived
  ob.showx();     // from base1
  ob.showy();     // from base2

  return 0;
}

    Running session:

@11:04am merc-hu[32]% CC -LANG:std MulInherit.cpp -o MulInherit
@11:05am merc-hu[33]% MulInherit
10
20
@11:05am merc-hu[34]%

กก

5. Calling base class's constructor in derived class 

    Code Listing: 

// File:    ~ftp/pub/class/cplusplus/Inheritance/BaseConst.cpp
// Purpose: demonstrate how to call base class's constructor

#include <iostream>
using namespace std;

class base1 {
protected:
  int i;
public:
  base1(int x) { i = x; cout << "Constructing base1\n"; }
  ~base1() { cout << "Destructing base2\n"; }
};

class base2 {
protected:
  int k;
public:
  base2(int x) { k = x; cout << "Constructing base2\n"; }
  ~base2() { cout << "Destructing base2\n"; }
};

class derived: public base1, public base2 {
  int j;
public:
  derived(int x, int y, int z): base1(y), base2(z)
    { j = x; cout << "Constructing derived\n"; }

  ~derived() { cout << "Destructing derived\n"; }
  void show() { cout << i << " " << j << " " << k << "\n"; }
};

int main()
{
  derived ob(3, 4, 5);

  ob.show();  // displays 4 3 5

  return 0;
}

 

    Running session:

@11:11am merc-hu[35]% CC -LANG:std BaseConst.cpp -o BaseConst
@11:12am merc-hu[36]% BaseConst
Constructing base1
Constructing base2
Constructing derived
4 3 5
Destructing derived
Destructing base2
Destructing base2
@11:12am merc-hu[37]%

กก



Last modified: Sunday, 25-Mar-2012 17:47:14 CST
Copyright 2000 Department of Computer Science, University of Regina.

 CS Dept Home Page

Teaching Material

C++ Index Page