--%>

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

  • Q : Define Delegation Delegation : The

    Delegation: The procedure by which an object passes on a message has received to a sub-ordinate object. When inheritance is not accessible in a programming language, then delegation is the most viable option for ignoring code duplication and promoting

  • Q : What is single user sysytem What is

    What is meant by the single users system?

  • Q : Explain Return statement Return

    Return statement: It is a statement employed to terminate the execution of the method. The method with void return type might only have return statements of the form as: return;

  • Q : Explain the way to enter element content

    Explain the way to enter element content.

  • Q : Define Binary search Binary search :

    Binary search: This is a search of sorted data, in which the middle place is examined first. The search continues with either the right or the left part of the data, therefore removing half the remaining search space. This procedure is repeated at eac

  • Q : Define Little-endian Little-endian : It

    Little-endian: It is a common difference among machines is the order in which they store up the individual bytes of multi-byte numerical data. The little-endian machine stores the lower-order bytes prior to the higher-order bytes.

  • Q : Define the term Graphical User Interface

    Graphical User Interface: A Graphical User Interface (abbreviated as GUI) is a part of a program which permits user interaction through graphical components, like menus, buttons, text areas, and so forth. Interaction frequently includes the usage of a

  • Q : What is High level programming language

    High level programming language: Languages like Ada, Java, C++, and so on which give programmers with features like control structures, classes, methods, packages, and so forth. Such features are mostly independent of any specific instruction set, and

  • Q : Define the term createWindow object

    Define the term createWindow object? Answer: reateWindow() physically makes the Window in memory  based upon the earlier registered WND

©TutorsGlobe All rights reserved 2022-2023.