Programming with C & C++

unit-5

1. EXPLAIN TYPES OF INHERITANCE

C++ Single Inheritance

Single inheritance is defined as the inheritance in which a derived class is inherited from the only one base class.





Where 'A' is the base class, and 'B' is the derived class.

Syntax:

Class A //base class

{

//code o class A

}

Class B: public A //derived class

{

//code of class B

}

Example

1.    #include <iostream>  

2.    using namespace std;  

3.     class Animal {  

4.       public:  

5.     void eat() {   

6.        cout<<"Eating..."<<endl;   

7.     }    

8.       };  

9.       class Dog: public Animal    

10.    {    

11.        public:  

12.      void bark(){  

13.     cout<<"Barking...";   

14.      }    

15.    };   

16. int main() 

17. {  

18.     Dog d1;  

19.     d1.eat();  

20.     d1.bark();  

21.     return 0;  

22. }  

 

C++ Multilevel Inheritance

Multilevel inheritance is a process of deriving a class from another derived class.




C++ Multi Level Inheritance Example

When one class inherits another class which is further inherited by another class, it is known as multi level inheritance in C++. Inheritance is transitive so the last derived class acquires all the members of all its base classes.

Let's see the example of multi level inheritance in C++.

1.     #include <iostream>  

2.     using namespace std;  

3.      class Animal {  

4.        public:  

5.      void eat() {   

6.         cout<<"Eating..."<<endl;   

7.      }    

8.        };  

9.        class Dog: public Animal   

10.     {    

11.         public:  

12.       void bark(){  

13.      cout<<"Barking..."<<endl;   

14.       }    

15.     };   

16.     class BabyDog: public Dog   

17.     {    

18.         public:  

19.       void weep() {  

20.      cout<<"Weeping...";   

21.       }    

22.     };   

23.  int main(void) {  

24.      BabyDog d1;  

25.      d1.eat();  

26.      d1.bark();  

27.       d1.weep();  

28.       return 0;  

29.  }  

Multiple Inheritance is the concept of the Inheritance in C++ that allows a child class to inherit properties or behaviour from multiple base classes. Therefore, we can say it is the process that enables a derived class to acquire member functions, properties, characteristics from more than one base class.

Diagram of the Multiple Inheritance


 


In the above diagram, there are two-parent classes: Base Class 1 and Base Class 2, whereas there is only one Child Class. The Child Class acquires all features from both Base class 1 and Base class 2. Therefore, we termed the type of Inheritance as Multiple Inheritance.

Syntax of the Multiple Inheritance

1.    class A  

2.     {  

3.     // code of class A  

4.     }  

5.     class B  

6.     {  

7.     // code of class B  

8.     }  

9.     class C: public A, public B (access modifier class_name)  

10.  {  

11.  // code of the derived class  

12.  }  

 

Example

1.    #include <iostream>  

2.    // create a base class1  

3.    class Base_class  

4.    {  

5.        // access specifier  

6.        public:   

7.        // It is a member function  

8.        void display()  

9.        {  

10.         cout << " It is the first function of the Base class " << endl;  

11.     }  

12. };  

13.   

14. // create a base class2  

15. class Base_class2  

16. {  

17.     // access specifier  

18.     public:   

19.     // It is a member function  

20.     void display2()  

21.     {  

22.         cout << " It is the second function of the Base class " << endl;  

23.     }  

24. };  

25.   

26. /* create a child_class to inherit features of Base_class and Base_class2 with access specifier. */  

27. class child_class: public Base_class, public Base_class2  

28. {  

29.       

30.     // access specifier  

31.     public:  

32.     void display3() // It is a member function of derive class  

33.     {  

34.         cout << " It is the function of the derived class " << endl;      

35.     }  

36.       

37. };  

38.   

39. int main ()  

40. {  

41.     // create an object for derived class  

42.     child_class ch;  

43.     ch.display(); // call member function of Base_class1  

44.     ch.display2(); // call member function of Base_class2  

45.     ch.display3(); // call member function of child_class  

46. }  

 

C++ Hierarchical Inheritance

Hierarchical inheritance is defined as the process of deriving more than one class from a base class.




Syntax of Hierarchical inheritance:

1.     class A  

2.     {  

3.         // body of the class A.  

4.     }    

5.     class B : public A   

6.     {  

7.         // body of class B.  

8.     }  

9.     class C : public A  

10.  {  

11.      // body of class C.  

12.  }   

13.  class D : public A  

14.  {  

15.      // body of class D.  

16.  }   

 

Example

// C++ program for Hierarchical Inheritance

#include<iostream>

class A //superclass A

{

public:

void show_A() {

                cout<<"class A"<<endl;

}

};

class B : public A //subclass B

{

public:

void show_B() {

                cout<<"class B"<<endl;

}

};

 

class C : public A //subclass C

{

public:

void show_C() {

                cout<<"class C"<<endl;

}

};

 

int main() {

B b; // b is object of class B

cout<<"calling from B: "<<endl;

b.show_B();

b.show_A();

               

C c; // c is object of class C

cout<<"calling from C: "<<endl;

c.show_C();

c.show_A();

return 0;

}

Hybrid inheritance in C++

Combining various types of inheritance like multiple, simple, and hierarchical inheritance is known as hybrid inheritance.

In simple inheritance, one class is derived from a single class which is its base. In multiple inheritances, a class is derived from two classes, where one of the parents is also a derived class. In hierarchical inheritance, more than one derived class is created from a single base class.

In hybrid inheritance, there is a combination of one or more inheritance types. For instance, the combination of single and hierarchical inheritance. Therefore, hybrid inheritance is also known as multipath inheritance.

Example



The diagram shows the hybrid inheritance that is a combination of single inheritance and multiple inheritance.

Single inheritance - Class B inherits class A. Thus an example of single inheritance.

Multiple inheritance - Class D is inherited from multiple classes( B and C shown above D). Thus an example of multiple inheritance.

Syntax code of the above example

1.     Class A  

2.     {  

3.     statement(s)  

4.     }:  

5.     Class B: public A  

6.     {  

7.     statement(s);  

8.     };  

9.     Class C  

10.  {  

11.  statement(s);  

12.  };  

13.  Class D: public B, public C  

14.  {  

15.  statement(s);  

16.  };  

 

#include <iostream>

 

using namespace std;

 

class Animal {

  public:

    Animal() {

      cout << "This is an Animal ";

    }

};

 

//single inheritance

class cat: public Animal {

  public:

   cat() {

    cout << "that is cat ";

  }

};

 

class pet {

  public:

    pet() {

      cout << "and pet";

    }

};

 

//Multiple Inheritance

class kitty: public cat, public pet {

  public:

    kitty() {

      cout << "\nName of the cat is kitty! \n";

  }

};

 

int main() {

  kitty mycat;

  return 0;

}


2. EXPLAIN ACCESS SPECIFIES

Access Specifiers

By now, you are quite familiar with the public keyword that appears in all of our class examples:



Example

#include <iostream>

class MyClass {   // The class

  public:         // Public access specifier

    int x;        // Public attribute (int variable)

};

int main() {

  MyClass myObj;  // Create an object of MyClass

  // Access attributes and set values

  myObj.x = 15;

// Print values

  cout << myObj.x;

  return 0;

}

The public keyword is an access specifier. Access specifiers define how the members (attributes and methods) of a class can be accessed. In the example above, the members are public - which means that they can be accessed and modified from outside the code.

However, what if we want members to be private and hidden from the outside world?

In C++, there are three access specifiers:

  • public - members are accessible from outside the class
  • private - members cannot be accessed (or viewed) from outside the class
  • protected - members cannot be accessed from outside the class, however, they can be accessed in inherited classes.  

In the following example, we demonstrate the differences between public and private members:

#include <iostream>

class MyClass {

  public:    // Public access specifier

    int x;   // Public attribute

  private:   // Private access specifier

    int y;   // Private attribute

};

int main() {

  MyClass myObj;

  myObj.x = 25;  // Allowed (x is public)

  myObj.y = 50;  // Not allowed (y is private)

  return 0;

}

 

3. What are the advantages and disadvantages of Inheritance in C++

The followings are the advantages and disadvantages of Inheritance in C++:



 

Advantages of Inheritance in C++

1.   Code Reusability – Inheritance in C++ allows you to create new classes that are based on existing classes, so you can reuse existing code and avoid rewriting the same functionality. This can save a lot of time and effort when you’re creating new programs.

2.   Organization – Inheritance in C++ helps you organize your code in a logical and structured way. By creating classes that inherit from other classes, you can create a hierarchy of related classes that makes it easy to understand how your code works and how different parts of your program are related.

3.   Polymorphism – Inheritance in C++ allows you to create objects that can take on different forms, depending on the class they inherit from. This is called polymorphism, and it can make your code more flexible and adaptable to changing requirements.

4.   Overriding – Inheritance in C++ allows you to override methods in a derived class, which means you can change the way a method works without changing the original code. This is a powerful way to customize existing code to suit your needs.

5.   Abstraction – Inheritance in C++ allows you to create abstract classes that define the basic behavior of a group of related classes, while leaving the implementation details to the derived classes. This is called abstraction, and it can make your code more modular and easier to understand.

Disadvantages of Inheritance in C++

1.   Complexity – Inheritance in C++ can make your code more complex, especially when you have multiple levels of inheritance or when you’re inheriting from classes that have a lot of methods and properties. This can make it harder to understand how your code works and can lead to bugs and other issues.

2.   Rigidity – Inheritance in C++ can make your code more rigid, as it can be difficult to change the way a class works once it has been inherited by other classes. This can make it hard to adapt your code to changing requirements and can limit its overall flexibility.

3.   Dependency – Inheritance in C++ can create dependencies between classes, which can make it harder to reuse your code in other programs or to make changes to your code without affecting other parts of your program.

4.   Performance – Inheritance in C++ can impact the performance of your code, as it requires additional memory and processing power to maintain the relationship between classes. This can slow down your program and make it less efficient.

5.   Violation of encapsulation – Inheritance allows the child class to inherit the parent class’s protected and public members, but not the private members. However, if the child class overrides or accesses the parent class’s protected members, it can potentially lead to unexpected changes, errors, and violate the encapsulation principle. This can result in bugs and make the code harder to maintain.

 

 


 


Comments

Popular posts from this blog

PROGRAMMING WITH C&C++