Skip to main content

INHERITENCE

C++ IN INHERITENCE 

                      In C++, inheritance is a process in which one object acquires all the properties and behaviors of its parent object automatically. In such way, you can reuse, extend or modify the attributes and behaviors which are defined in other class.

               In C++, the class which inherits the members of another class is called derived class and the class whose members are inherited is called base class. The derived class is the specialized class for the base class.


ADVANTAGE OF C++ INHERITANCE;

                      CODE REUSABILITY: Now you can reuse the members of your parent class. So, there is no need to define the member again. So less code is required in the class.

TYPES OF INHERTANCE;

C++ supports five types of inheritance:

  • Single inheritance
  • Multiple inheritance
  • Hierarchical inheritance
  • Multilevel inheritance
  • Hybrid inheritance

C++ Inheritance



DERIVED CLASSES;

                    A Derived class is defined as the class derived from the base class.


The Syntax of Derived class:


  1. class derived_class_name :: visibility-mode base_class_name  
  2. {  
  3.     // body of the derived class. 
  4. }


Where,

derived_class_name: It is the name of the derived class.

visibility mode: The visibility mode specifies whether the features of the base class are publicly inherited or privately inherited. It can be public or private.

base_class_name: It is the name of the base class.

  • When the base class is privately inherited by the derived class, public members of the base class becomes the private members of the derived class. Therefore, the public members of the base class are not accessible by the objects of the derived class only by the member functions of the derived class.
  • When the base class is publicly inherited by the derived class, public members of the base class also become the public members of the derived class. Therefore, the public members of the base class are accessible by the objects of the derived class as well as by the member functions of the base class.

Note:

  • In C++, the default mode of visibility is private.
  • The private members of the base class are never inherited.

C++ SINGLE INHERITANCE;

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


C++ Inheritance

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

C++ SINGLE LEVEL INHERITANCE EXAMPLE:

 INHERITING FIELDS  

When one class inherits another class, it is known as single level inheritance. Let's see the example of single level inheritance which inherits the fields only.

  1. #include <iostream>  
  2. using namespace std;  
  3.  class Account {  
  4.    public:  
  5.    float salary = 60000;   
  6.  };  
  7.    class Programmer: public Account {  
  8.    public:  
  9.    float bonus = 5000;    
  10.    };       
  11. int main(void) {  
  12.      Programmer p1;  
  13.      cout<<"Salary: "<<p1.salary<<endl;    
  14.      cout<<"Bonus: "<<p1.bonus<<endl;    
  15.     return 0;  
  16. }  

Output:

Salary: 60000
Bonus: 5000
In the above example, Employee is the base class and Programmer is the derived class.


HOW TO MAKE A PRIVATE MEMBER INHERITANCE;

The private member is not inheritable. If we modify the visibility mode by making it public, but this takes away the advantage of data hiding.

C++ introduces a third visibility modifier, i.e., protected. The member which is declared as protected will be accessible to all the member functions within the class as well as the class immediately derived from it.


Visibility modes can be classified into three categories:


C++ Inheritance

  • Public: When the member is declared as public, it is accessible to all the functions of the program.
  • Private: When the member is declared as private, it is accessible within the class only.
  • Protected: When the member is declared as protected, it is accessible within its own class as well as the class immediately derived from it.

VISIBILITY OF INHERITANCE MAMBERS;


Base class visibilityDerived class visibility
PublicPrivateProtected
PrivateNot InheritedNot InheritedNot Inherited
ProtectedProtectedPrivateProtected
PublicPublicPrivateProtected

C++ MULTILEVEL INHERITANCE;

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


C++ Inheritance

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. }  

Output:

Eating...
Barking...
Weeping...


C++ MULTIPLE INHERITANCE;

Multiple inheritance is the process of deriving a new class that inherits the attributes from two or more classes.


C++ Inheritance

Syntax of the Derived class:

  1. class D : visibility B-1, visibility B-2, ?  
  2. {  
  3.     // Body of the class;  
  4. }   

Let's see a simple example of multiple inheritance.

  1. #include <iostream>  
  2. using namespace std;  
  3. class A  
  4. {  
  5.     protected:  
  6.      int a;  
  7.     public:  
  8.     void get_a(int n)  
  9.     {  
  10.         a = n;  
  11.     }  
  12. };  
  13.   
  14. class B  
  15. {  
  16.     protected:  
  17.     int b;  
  18.     public:  
  19.     void get_b(int n)  
  20.     {  
  21.         b = n;  
  22.     }  
  23. };  
  24. class C : public A,public B  
  25. {  
  26.    public:  
  27.     void display()  
  28.     {  
  29.         std::cout << "The value of a is : " <<a<< std::endl;  
  30.         std::cout << "The value of b is : " <<b<< std::endl;  
  31.         cout<<"Addition of a and b is : "<<a+b;  
  32.     }  
  33. };  
  34. int main()  
  35. {  
  36.    C c;  
  37.    c.get_a(10);  
  38.    c.get_b(20);  
  39.    c.display();  
  40.   
  41.     return 0;  
  42. }  

Output:

The value of a is : 10
The value of b is : 20
Addition of a and b is : 30

In the above example, class 'C' inherits two base classes 'A' and 'B' in a public mode.


C++ HYBIRD INHERITANCE;

Hybrid inheritance is a combination of more than one type of inheritance.


C++ Inheritance

Let's see a simple example:

  1. #include <iostream>  
  2. using namespace std;  
  3. class A  
  4. {  
  5.     protected:  
  6.     int a;  
  7.     public:  
  8.     void get_a()  
  9.     {  
  10.        std::cout << "Enter the value of 'a' : " << std::endl;  
  11.        cin>>a;  
  12.     }  
  13. };  
  14.   
  15. class B : public A   
  16. {  
  17.     protected:  
  18.     int b;  
  19.     public:  
  20.     void get_b()  
  21.     {  
  22.         std::cout << "Enter the value of 'b' : " << std::endl;  
  23.        cin>>b;  
  24.     }  
  25. };  
  26. class C   
  27. {  
  28.     protected:  
  29.     int c;  
  30.     public:  
  31.     void get_c()  
  32.     {  
  33.         std::cout << "Enter the value of c is : " << std::endl;  
  34.         cin>>c;  
  35.     }  
  36. };  
  37.   
  38. class D : public B, public C  
  39. {  
  40.     protected:  
  41.     int d;  
  42.     public:  
  43.     void mul()  
  44.     {  
  45.          get_a();  
  46.          get_b();  
  47.          get_c();  
  48.          std::cout << "Multiplication of a,b,c is : " <<a*b*c<< std::endl;  
  49.     }  
  50. };  
  51. int main()  
  52. {  
  53.     D d;  
  54.     d.mul();  
  55.     return 0;  
  56. }  

Output:

Enter the value of 'a' :
10              
Enter the value of 'b' :    
20      
Enter the value of c is :   
30  
Multiplication of a,b,c is : 6000


C++ HIERARCHICAL INHERITANCE;

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



C++ Inheritance


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. }   

Let's see a simple example:

  1. #include <iostream>  
  2. using namespace std;  
  3. class Shape                 // Declaration of base class.  
  4. {  
  5.     public:  
  6.     int a;  
  7.     int b;  
  8.     void get_data(int n,int m)  
  9.     {  
  10.         a= n;  
  11.         b = m;  
  12.     }  
  13. };  
  14. class Rectangle : public Shape  // inheriting Shape class  
  15. {  
  16.     public:  
  17.     int rect_area()  
  18.     {  
  19.         int result = a*b;  
  20.         return result;  
  21.     }  
  22. };  
  23. class Triangle : public Shape    // inheriting Shape class  
  24. {  
  25.     public:  
  26.     int triangle_area()  
  27.     {  
  28.         float result = 0.5*a*b;  
  29.         return result;  
  30.     }  
  31. };  
  32. int main()  
  33. {  
  34.     Rectangle r;  
  35.     Triangle t;  
  36.     int length,breadth,base,height;  
  37.     std::cout << "Enter the length and breadth of a rectangle: " << std::endl;  
  38.     cin>>length>>breadth;  
  39.     r.get_data(length,breadth);  
  40.     int m = r.rect_area();  
  41.     std::cout << "Area of the rectangle is : " <<m<< std::endl;  
  42.     std::cout << "Enter the base and height of the triangle: " << std::endl;  
  43.     cin>>base>>height;  
  44.     t.get_data(base,height);  
  45.     float n = t.triangle_area();  
  46.     std::cout <<"Area of the triangle is : "  << n<<std::endl;  
  47.     return 0;  
  48. }  

Output:

Enter the length and breadth of a rectangle:
23  
20  
Area of the rectangle is : 460          
Enter the base and height of the triangle:  
2   
5
Area of the triangle is : 5 

*****************************************


Comments

Popular posts from this blog

NUMBER SYSTEM CONVERSION

NUMBER BASE CONVERSION                     In our previous section, we learned different types of number systems such as binary, decimal, octal, and hexadecimal. In this part of the tutorial, we will learn how we can change a number from one number system to another number system. As, we have four types of number systems so each one can be converted into the remaining three systems. There are the following conversions possible in Number System Binary to other Number Systems. Decimal to other Number Systems. Octal to other Number Systems. Hexadecimal to other Number Systems.                                                                                                                                                                                                                                     1.BINARY NUMBER SYSTEM CONVERSION  Number System with base value 2 is termed as Binary number system. It uses 2 digits i.e. 0 and 1 for the creation of numbers. The numbers formed using these

DJANGO

                                                                     TABLE OF CONTENT: What is Django? why use of Django Framework? Features of Django How To   Setup   Django at Glance Advantages and Disadvantages of Django WHAT IS DJANGO? Django is just an open-source web framework that supports high-level python programming. It speeds up the development of web applications that are being built on Python Language. Django helps in “Rapid Development, Pragmatic djangoProjectCarriedand Clean Design”. This is deployed on a web server that helps developers produce a web front-end that is secure, feature-rich, fast, and scalable. Starting from scratch, which involves designing the backend, APIs, javascript, and sitemaps, is a more effective way to create a web app than using the Django web framework. Web developers can concentrate on developing a specific application using the Django web platform, which provides more flexibility than any other web development tool. WHY USE OF DJANGO FRAMEWO

HEXADECIMAL NUMBER SYSTEM CONVERSION

 HEXADECIMAL NUMBER SYSTEM CONVERSION           1.HEXADECIMAL NUMBER CONVERSION Number System with base value 16 is termed as Hexadecimal Number System. It uses 16 digits for the creation of its numbers. Digits from 0-9 are taken like the digits in the decimal number system but the digits from 10-15 are  represented as A-F i.e. 10 is represented as A, 11 as B, 12 as C, 13 as D, 14 as E, and 15 as F. Hexadecimal Numbers are useful for handling memory address locations. * HEXADECIMAL NUMBER SYSTEM TO BINARY NUMBER SYSTEM; Hex numbers are represented in base 16, but the binary numbers are of base 2. Hence, to convert a hexadecimal number to a binary number, the base of that number is to be changed. Follow the steps given below: Step 1:  Convert the Hex symbols into its equivalent decimal values. Step 2:  Write each digit of the Hexadecimal number separately. Step 3:  Convert each digit into an equivalent group of four binary digits. Step 4:  Combine these groups to form the whole binary n