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 : State Null character State Null

    State Null character: The \u0000 character. The care must be taken not to confuse this with null reference.

  • Q : Define Stream class Stream class : An

    Stream class: An input stream class is one which delivers data from its source (frequently the file system as a series of bytes. Likewise, an output stream class will write byte-level data. The stream classes must be contrasted with the operation of r

  • Q : Explain Aggregation Aggregation : It is

    Aggregation: It is a relationship in which an object has one or more other subordinate objects as portion of its state. The subordinate objects usually have no self-governing existence separate from their containing object. Whenever the containing obj

  • Q : What is an Arithmetic expression

    Expression: It is a combination of operands and operators which generates a resultant value. Expressions contain a resulting type that affects the context in which they might be employed.

  • Q : Distinct features of Object oriented

    Q. What are the distinct features of Object oriented programming language?  

    Q : What is Modal What is Modal : A dialog

    What is Modal: A dialog is a modal when its parent application is blocked from additional activity until the dialog has finished.

  • Q : State the term XLL State the term XLL?

    State the term XLL? Answer: The specification of XML Link Language has XPointer or XLink.

  • Q : Basic programming help In C Language 1.

    1. In what ways are the initialization, repetition test, and update steps alike for a sentinel-controlled loop and an endfile-controlled loop? How are they different? 2. Rewrite the program segment that follows using a for loop: count = 0;

  • Q : Explain the way to write the opening

    Explain the way to write the opening tag of an XHTML element.

  • Q : Define Final variable Final variable :

    Final variable: A variable with final reserved word in its declaration. The final might not assigned to once it has been initialized. Initialization frequently occurs as a part of its declaration. Though, the initialization of an uninitialized final f

©TutorsGlobe All rights reserved 2022-2023.