Pages

Showing posts with label OOS. Show all posts
Showing posts with label OOS. Show all posts

Monday, November 12, 2012

MCA OOS UNIT 4 & UNIT 5



Inheritance
C++ strongly supports the concepts of reusability.
Once a class has been written and tested, it can be adopted b other programmers to suit their requirements.
This is basically done by creating new classes reusing the properties of the existing once.
The mechanism of deriving a new class from an old one is called inheritance.
The old class is referred as base class and the new one is called the derived or subclass.
Hierarchical Inheritance
Multilevel Inheritance

Multiple Inheritance
Hybrid Inheritance

Single Inheritance



A class can also inherit properties from more than one class or from more than one level .
A derived class with only one base class is called single inheritance and one with several base classes is called multiple inheritances.
The traits of one class may be inherited by more than one class. This process is called as hierarchical inheritance.
The mechanism of deriving a class from another derived class is known as multilevel inheritance.




Syntax 

class  derived_ class-name : visibility-mode  base class_name
{
---------------//
---------------//members of derived class
--------------//
} ;

Single Inheritance Example :Single Inheritance is method in which a derived class has only one base class.

#include <iostream.h>
class Value 
  {
    protected:
      int val;
    public:
      void set_values (int a)
        { val=a;}
  };
class Cube: public Value 
  {
    public:
     int cube()
       { return (val*val*val); }
  };
int main () 
 {
   Cube cub;
   cub.set_values (5); //object of cube class inherit the 
                         function of Value class 
   cout << "The Cube of 5 is::" << cub.cube() << endl;
   return 0;
 }

Multiple Inheritance Example: Multiple Inheritance is a method by which a class is derived from more than one base class.

#include <iostream.h>
using namespace std;
class Square
  {
    protected:
     int l;
    public:
     void set_values (int x)
      { l=x;}
  };
class CShow 
  {
    public:
     void show(int i);
  };

void CShow::show (int i) 
 {
   cout << "The area of the square is::" << i << endl;
 }

class Area: public Square, public CShow
  {
   public:
    int area()
      { return (l *l); }
  };
int main () 
{
  Area r;
  r.set_values (5);
  r.show(r.area());
  return 0;
}


Hierarchical Inheritance Example: Hierarchical Inheritance is a method of inheritance where one or more derived classes is derived from common base class.

#include <iostream.h>
class Side
  {
    protected:
      int l;
    public:
      void set_values (int x)
        { l=x;}
  };
class Square: public Side
  {
    public:
      int sq()
      { return (l *l); }
  };
class Cube:public Side
  {
   public:
     int cub()
      { return (l *l*l); }
  };
int main () 
  {
    Square s;
    s.set_values (10);
    cout << "The square value is::" << s.sq() << endl;
    Cube c;
    c.set_values (20);
    cout << "The cube value is::" << c.cub() << endl;
    return 0;
  }


Multilevel Inheritance Example: Multilevel Inheritance is a method where a derived class is derived from another derived class.

#include <iostream.h>
class mm
  {
    protected:
      int rollno;
    public:
      void get_num(int a)
        { rollno = a; }
      void put_num()
        { cout << "Roll Number Is:\n"<< rollno << "\n"; }
  };
class marks : public mm
  {
    protected:
      int sub1;
      int sub2;
    public:
      void get_marks(int x,int y)
        {
           sub1 = x;
           sub2 = y;
        }
      void put_marks(void)
        {
          cout << "Subject 1:" << sub1 << "\n";
          cout << "Subject 2:" << sub2 << "\n";
        }
   };
class res : public marks
   {
     protected: 
       float tot;
     public:
       void disp(void)
          {
            tot = sub1+sub2;
            put_num();
            put_marks();
            cout << "Total:"<< tot;
          }
   };
int main()
   {
     res std1;
     std1.get_num(5);
     std1.get_marks(10,20);
     std1.disp();
     return 0;
  }

Hybrid Inheritance Example: Hybrid Inheritance is a method where one or more types of inheritance are combined together and used.
#include <iostream.h>
class mm
  {
    protected:
      int rollno;
    public:
      void get_num(int a)
       { rollno = a; }
      void put_num()
       { cout << "Roll Number Is:"<< rollno << "\n"; }
  };
class marks : public mm
  {
   protected:
     int sub1;
     int sub2;
   public:
     void get_marks(int x,int y)
       {
         sub1 = x;
         sub2 = y;
       }
     void put_marks(void)
       {
          cout << "Subject 1:" << sub1 << "\n";
          cout << "Subject 2:" << sub2 << "\n";
      }
  };

class extra 
  {
    protected:
      float e;
    public:
    void get_extra(float s)
      {e=s;}
    void put_extra(void)
      { cout << "Extra Score::" << e << "\n";}
  };

class res : public marks, public extra{
   protected: 
     float tot;
   public:
     void disp(void)
       {
         tot = sub1+sub2+e; 
         put_num();
         put_marks();
         put_extra();
         cout << "Total:"<< tot;
       }
 };

int main()
 {
   res std1;
   std1.get_num(10);
   std1.get_marks(10,20);
   std1.get_extra(33.12);
   std1.disp();
   return 0;
 }
When a base class is privately inherited by a derived class, public members of the
base class become private members of the derived class and therefore the public
member of the base class can only be accessed by the member functions of the derived class. They are inaccessible to the object of the derived class. 
A public member of a class can be accessed by its own objects using dot operator.
The result is that no member of the base class is accessible to the object of the derived class.

Visibility of Inherited members:



Followers