Inheritance in Object Oriented Programming Language

Q. What is the use of making a method private inside a class? What is the application of inheritance in OOP?

 

(b) What are the difference forms of inheritance give an example of each.

 

 Ans. (a) a member function of a class is accessed by the objects of that class using the dot operator. A member function of a class can call any other member function of its even class irrespective of its privilege and this situation is called nesting of member function. Here the calling function can be defined in private part because we have to access this function within the class. The method for calling member functions of one's is illustrated in the following program.

A member function contacting another member function.

 include < iostream.h >

Class Number Pairs

{

 Private:

Int num 1, num 2;

            Int max ()

            {   If (num 1>num 2)

                Return num 1;

                Else

                Return num 2;

          }

     Public:

         Void read ()

        {

           Cout <<"enter first no";

           Cin>> num 1;

            Cout <<"enter second no";

           Cin > num 2;

      }

        Void show max ()

      {

         Cout <<"max=" << max ();

      }

 };

Void main ()

{

   Number pairs n1;

   N1.read ()

   N1.show max ();

}

Single inheritance occurs when a derived class inherits some or all of the traits from the base class when a derived class having only one base class. The phenomenon is called single inheritance. Example

Class B

 {

    Int a;

    Public:

     Int b;

     Void get. ab ()

     Int get. a (void);

     Void show. a (void); 

 };

    Class D: public B

{

   Int c;

   Public:

   Void maul (void);

   Void display (void);

};

   Void B: get. ab (void)

{

   A=5; b=10;

}

  Int B: get. A ()

{

   Return a;

}

   Void B: show. A ()

{

   Cout <<"a=" <

}

   Void D: maul ()

{

   C = b get. A ();

}

   Void D: display ()

{

  Cout <<"a =" <

  Cout << "B=" <

  Cout <<"c=" <

}

Void main ()

{

    D d;

    D .get. ab ();

    D. maul ();

    D. show. a ();

    D .display ();

    D. b=20;

    d.mul ();

    d. display ();

}

 (B) Multiple inheritances: A derived class with several base classes is called multiple inheritance.  

  Example

Class M

{

Protected :

Void get. m (Int i)

};

Class N

{

Protected :

Int n;

Public :

Void get. n (Int i)

};

Class P : public M, public N

{

Public :

Void display (void);

};

Void M :: get. m (Int x)

{ m = x;}

Void N ::  get. n (Int x)

{ n =y; }

Void P :: display (void)

{

Cout << "m=" << m << "\n";

Cout << "n=" << n << "\m";

Cout << "m*n = " << m*n;

}

Void main 0

{

P p;

P . get. m (10);

 P . get. m (20);

P . display();

}

Hierarchical inheritance: When traits of one class may be inherited by more than one, this process is known as hierarchical inheritance. This is used when two or more class want to reuse the contents of a main class, example

Class A

{

Int a, b;

Public:

Void A ()

{ a = 0; b =0;}

};

Class B : public A 

{ = };

Class C : public A

{ = };

Class D : public A

{ = };

Then all the members or function of class A can be called from class B, C or class D.

Multilevel inheritance: When one class inherit the other hand and the other third class inherit second class and so on.

Example

Class A

{

Int a, b;

Public :

Void get data ()

{

Cin >> a >> b;

}

Class B : public A

{ =};

Class C : public A

{ = };

Hybrid inheritance: It is a mixture of hierarchical, multiple and multilevel inheritance.

   Related Questions in Programming Languages

©TutorsGlobe All rights reserved 2022-2023.