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 Property Specifications Property

    Property Specifications: Users can specify assertions using the assert(expr) statements. An assert statement is used to check if the property specified by the expression expr is valid within a state. If expr evaluates to 0, this implies that it is not

  • Q : Define the term Method signature Define

    Define the term Method signature: This is the synonym for method header.

  • Q : Define Primitive type Primitive type :

    Primitive type: Java's eight standard non-class types are the primitive types as: Boolean, byte, char, float, int, double, long and short.

  • Q : Pointer in C What is pointer? What

    What is pointer? What significance of pointer in C programming languages?

  • Q : Explain Twos-complement notation

    Twos-complement notation: In twos-complement notation, the most noteworthy bit in an integer value is employed as the sign bit. A 1 bit points out a negative number, and a 0 bit points out a positive number. The positive number can be transformed to i

  • Q : Depths of categories-subcategories

    Normal 0 false false

  • Q : Define the reasons of Process Handle

    Define the reasons of Process Handle Table.

  • Q : Explain Counters Counter variables are

    Counter variables are commonly used in many computer applications for different purposes. Here is a typical example where a variable is used to measures the progress of some activity of interest:

    Q : What is Octal character constant Octal

    Octal character constant: It is a character constant in the form of \ddd, where each d is an octal digit. This might be employed for characters with a Unicode value in the range of 0 to 255.

  • Q : Define Continue statement Continue

    Continue statement: A statement which might only be employed within the body of a loop. In case of a while loop or do loop, control passes instantly to the loop's terminating test. In case of a for loop, the control passes to the post-body update expr

©TutorsGlobe All rights reserved 2022-2023.