Showing posts with label Inheritance. Show all posts
Showing posts with label Inheritance. Show all posts

Changing Access in Inheritance in C++


/*The following program illustrates the access declaration; notice how it uses access
declarations to restore j, seti(), and geti() to public status.*/
#include <iostream.h>
#include <conio.h>
class base
{
      int i; // private to base
      public:
      int j, k;
      void seti(int x)
      {
            i = x;
      }
      int geti()
      {
            return i;
      }
};
// Inherit base as private.
class derived: private base
{
      public:
      /* The next three statements override
      base's inheritance as private and restore j,
      seti(), and geti() to public access. */
      base::j; // make j public again - but not k
      base::seti; // make seti() public
      base::geti; // make geti() public
      // base::i; // illegal, you cannot elevate access
      int a; // public
};
int main()
{
      clrscr();
      derived ob;
      //ob.i = 10; // illegal because i is private in derived
      ob.j = 60; // legal because j is made public in derived
      //ob.k = 30; // illegal because k is private in derived
      ob.a = 70; // legal because a is public in derived
      ob.seti(80);
      cout << ob.geti() << " " << ob.j << " " << ob.a;
      getch();
      return 0;
}

Virtual Inheritance in C++


#include <iostream.h>
#include <conio.h>
class base
{
      public:
      virtual void vfunc()
      {
            cout << "This is base's vfunc().\n";
      }
};

class derived1 : public base
{
      public:
      void vfunc()
      {
            cout << "This is derived1's vfunc().\n";
      }
};

/* derived2 inherits virtual function vfunc()
from derived1. */

class derived2 : public derived1
{
      public:
// vfunc() is still virtual

            void vfunc()
            {
                  cout << "This is derived2's vfunc().\n";
            }
};

int main()
{
      clrscr();
      base *p, b;
      derived1 d1;
      derived2 d2;
      // point to base
      p = &b;
      p->vfunc(); // access base's vfunc()
      // point to derived1
      p = &d1;
      p->vfunc(); // access derived1's vfunc()
      // point to derived2
      p = &d2;
      p->vfunc(); // access derived2's vfunc()
      getch();
      return 0;
}

Ambiguity in Inheritance Explained in C++


#include <iostream.h>
#include <conio.h>
class base
{
      public:
      int i;
};
// derived1 inherits base.
class derived1 : public base
{
      public:
      int j;
};
// derived2 inherits base.
class derived2 : public base
{
      public:
      int k;
};
/* derived3 inherits both derived1 and derived2.
This means that there are two copies of base
in derived3! */
class derived3 : public derived1, public derived2
{
      public:
      int sum;
};
int main()
{
      clrscr();
      derived3 ob;
      ob.i = 10; // this is ambiguous, which i???
      ob.j = 20;
      ob.k = 30;
      // i ambiguous here, too
      ob.sum = ob.i + ob.j + ob.k;
      // also ambiguous, which i?
      cout << ob.i << " ";
      cout << ob.j << " " << ob.k << " ";
      cout << ob.sum;
      getch();
      return 0;
}
Custom Search