--%>

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 : What is an Instruction set Instruction

    Instruction set: The set of instructions which characterize a specific Central Processing Unit. The programs written in the instruction set of one type of CPU can’t usually be run on any other kind of CPU.

  • Q : Explain Java Virtual Machine Java

    Java Virtual Machine (JVM): It is an idealized machine whose instruction set comprises of bytecodes. Java program is compiled to an equal bytecode form and performed on an interpreter that implements the JVM.

  • Q : State Finalization Finalization :

    Finalization: Instantly before an object is garbage collected, its finalize method is called. This offers it the opportunity to free any resources it may be holding on to.

  • Q : What is Super class Super class: It is

    Super class: It is a class which is extended by one or more sub classes. All Java classes encompass the Object class as a super-class.

  • Q : Including CSS with the HTML Tag Explain

    Explain how to include the CSS within the HTML Tag?

  • Q : State the term XHTML State the term

    State the term XHTML?

  • Q : What is Unique Identifier What is

    What is Unique Identifier and how do I determine one?

  • Q : What is an Anonymous class Anonymous

    Anonymous class: It is a class formed without a class name. Such a class will be a sub class or an implementation of an interface, and is generally formed as an actual argument or returned as a method outcome. For example:

    Q : What is Stepwise refinement Stepwise

    Stepwise refinement: It is a divide and conquer approach to the programming, in which a complicated problem is recursively divided into smaller, sub-problems and more manageable. This approach to program design is frequently employed with structured p

  • Q : What is Native Mode Native Mode :

    Native Mode: Whenever all the domain controllers in a specified domain are executing Windows 2000 Server. This mode permits organizations to take benefit of new Active Directory features like Universal groups, nested group membership, and the inter-do

©TutorsGlobe All rights reserved 2022-2023.