| 
Inheritance is an
  object-oriented programming principle. It is the ability  of a subclass to take  the characteristics from other class on
  which it's based on is known as Inheritance. . In inheritance the subclass is
  known as derived class or child class which derived from a parent class which
  also known as base class or Super class. In c++ we get are six different
  forms of inheritance. 
Objects those declared as public  share with all other classes. Where  Private Identifiers and member methods only
  accessed by the member of the class and inner classes. protected data and
  member methods meant for the derived classes 
   
The derived class
  can share the members of the base class as well as the new members of its
  own. Therefore, a derived class can use members of the base class, unless
  same members are redefined or declared as private in base class. The base
  class should be written before the derived class.  
1.          
  Single inheritance
  : Only one Base or Super Class 
2.          
  Multiple
  inheritance : A subclass can inherit several Base classes 
3.          
  Hierarchical
  inheritance : Many subclass with only one super class 
4.          
  Multi-Level
  inheritance : Derived classed derived from other derive classes 
5.          
  Hybrid inheritance
  : When more than two above mentioned type of inheritance used is known as
  Hybrid Inheritance. 
6.          
  Multi-path
  inheritance : Get some or all properties from two sources. 
Single Inheritance : In single
  inheritance classes have only
  one base class. B_Class is derived from A_Class.  | 
| 
Example : 
  class A_Class 
      { 
           protected: 
              int
  Num; 
      }; 
  class B_Class : public A_Class 
      { 
            private: 
              int
  Num1; 
      }; 
Multiple inheritance : A subclass can
  inherit several Base classes. C_Class is derived from both A_Class and
  B_Class. | 
| 
Example : 
  class A_Class 
      { 
           protected: 
              int
  Num; 
      }; 
  class B_Class 
      { 
           protected: 
              int
  Num1; 
      }; 
  class C_Class : public A_Class,B_Class 
      { 
            private: 
              int
  Num2; 
      }; 
Hierarchical inheritance : Many subclass
  with only one super class. Three classes, B_Class, C_Class and D_Class
  derived from base class A_Class.  | 
| 
Example: 
  class A_Class 
      { 
           protected: 
              int
  Num; 
      }; 
  class B_Class : public A_Class 
      { 
            private: 
              int
  Num1; 
      }; 
  class C_Class : public A_Class 
      { 
            private: 
              int
  Num2; 
      }; 
  class D_Class : public A_Class 
      { 
            private: 
              int
  Num3; 
      }; 
Multi-Level inheritance :
  Derived classes derived from other derive classes. C_Class derived from
  B_Class and which already derived from A_Class. | 
| 
Example: 
  class A_Class 
      { 
           protected: 
              int
  Num; 
      }; 
  class B_Class : public A_Class 
      { 
            protected: 
              int
  Num1; 
      }; 
  class C_Class : public B_Class 
      { 
            private: 
              int
  Num2; 
      }; 
Hybrid inheritance : When more than
  two above mentioned type of inheritance used is known as Hybrid Inheritance.
  Here applying both types, Multiple and Multilevel inheritance. Where C_Class
  derived from both A_Class and B_Class and whereas D_Class derived from
  C_Class. | 
| 
Example: 
  class A_Class 
      { 
           protected: 
              int
  Num; 
      }; 
  class B_Class  
      { 
            protected: 
              int
  Num1; 
      }; 
  class C_Class : public A_Class, B_Class 
      { 
            protected: 
              int
  Num2; 
      }; 
  class D_Class : public C_Class 
      { 
            private: 
              int
  Num3; 
      }; 
Multi-path inheritance : Share some or all
  properties from two sources. Multi-path inheritance is a type when a class is
  derived from more than one classes and those classes are derived  from same base class. | 
| 
Example: 
  class A_Class 
      { 
           protected: 
              int
  Num; 
      }; 
  class B_Class : public A_Class 
      { 
           protected: 
              int
  Num1; 
      }; 
  class C_Class : public A_Class 
      { 
            private: 
              int
  Num2; 
      }; 
  class D_Class : public B_Class, C_Class 
      { 
            private: 
              int
  Num3; 
      }; | 
Thursday, January 31, 2013
Inheritance - C++
Thursday, January 24, 2013
Template Class & Class Template – C++.
Template class help us to use generic
classes and functions. Template allows a function or class to work on many
different data types without being rewritten for each one. Instead of writing
multiple overloaded function for the same purpose but with different data types
we can use template class and do the same with a single function. 
Class template
Class template : A class template same as
regular class, except it is prefixed by the keyword template. Using the class
templates, we can implemented a class can have members with template
parameters.
Example I :
#include <iostream.h>
template<class
t1>
class
A
       {
              t1 a;
              public:
              A(t1
x)
                   {
                        a=x;
                   }
              void display()
                   {
                        cout<<a<<endl;
                   }
       };
int
main()
       {
              A <int>o1(4);
              A <float>o2(8.7);
              A <char>o3('F');
              o1.display();
              o2.display();
              o3.display();
              return 0;
       }
OUTPUT
4
8.7
F
 Example II:
#include <iostream.h>
  template<class
T>
  class
TMCL
      {
           private:
              T x,y;
           public:
              TMCL()
                   {
                   }
              TMCL(T a,T b)
                   {
                        x=a;
                        y=b;
                   }
              void
display();
              void
sum();
      };
  template<class T>
  void TMCL<T>::display()
      {
           cout<<"\n\tX :
"<<x<<" & "<<"Y
: "<<y;
      }
  template<class T>
  void TMCL<T>::sum()
      {
           cout<<"\n\tSum =
"<<x+y;
      }
  void
main()
      {
           int
a,b;
           float
x,y;
           cout<<"\n\tEnter
two Integer No. : ";
           cin>>a>>b;
           cout<<"\n\tEnter two Real No. : ";
           cin>>x>>y;
           TMCL<int>T1(a,b);
           TMCL<float>T2(x,y);
           T1.sum();
           T2.sum();
      }
LisnkList Using Template Class
//linked list using template//
#include <iostream.h>
#include<process.h<
  template<class
t>
  class
list
      {
           private:
              t
data;
              list
*head,*next,*ptr;
           public:
              list()
                   {
                        head=NULL;
                   }
           void
input(t);
           void
Display();
        //   void
del 
      };
  template<class
t>
  void
list<t>::input(t n)
      {
           list
*ptr1;
           ptr1=new
list();
           ptr1->data=n;
           ptr1->next=NULL;
           if(head==NULL)
              {
                   ptr=head=ptr1;
              }
           else
              {
                   ptr->next=ptr1;
                   ptr=ptr1;
              }
      }
  template<class
t>
  void
list<t>::Display()
      {
           list
*ptr1;
           ptr1=head;
           cout<<"\n\tValues
: ";
           while(ptr1!=NULL)
              {
                   cout<<ptr1->data<<"
";
                   ptr1=ptr1->next;
              }
      }
  int SubMenu()
      {
           int c;
           cout<<"\n\t1.
Input";
           cout<<"\n\t2.
Display";
           cout<<"\n\t3. Exit";
           cout<<"\n\tChoice
? ";
           cin>>c;
           return
c;
      }
  void
main()
      {
           list<int>L;
           list<float>L1;
           int ch=0,c=0;
           int
n;
           float
n1;
           while(ch!=4)
              {
                   cout<<"\n\t1.
Integer Data";
                   cout<<"\n\t2.
Real Data";
                   cout<<"\n\t3.
Exit";
                   cout<<"\n\tChoice
? ";
                   cin>>ch;
                   switch(ch)
                        {
                            case
1:
                                c=0;
                                while(c!=3)
                                    {
                                          c=SubMenu();
                                          switch(c)
                                              {
                                                  case
1:
                                                       cout<<"\n\tEnter a No. : ";
                                                       cin>>n;
                                                       L.input(n);
                                                       break;
                                                  case
2:
                                                       L.Display();
                                                       break;
                                                  case
3:
                                                       break;
                                              }
                                    }
                                break;
                            case
2:
                                c=0;
                                while(c!=3)
                                    {
                                          c=SubMenu();
                                          switch(c)
                                              {
                                                  case
1:
                                                       cout<<"\n\tEnter
a No. : ";
                                                       cin>>n1;
                                                       L1.input(n1);
                                                       break;
                                                  case
2:
                                                       L1.Display();
                                                       break;
                                                  case
3:
                                                       break;
                                              }
                                    }
                                break;
                            case
3:
                                 exit(0);
                   }
           }
      }
Subscribe to:
Comments (Atom)






 
 
