Thursday, February 28, 2013

Hierarchical Inheritance – C++.



Hierarchical Inheritance - A base class can have many derived classes.



Example – Hierarchical Inheritance, where Customer class and Vendor class using same database of base class Personal.

#include <iostream.h>

#define MAX 10
  class Personal    //Base Class
      {
          protected:
             int code[MAX];
             char name[MAX][20];
             int amt[MAX];
             int size;
          public:
             Personal(int x)
                 {
                      size=x;
                 }
             void getData();
             void showData();
      };
  void Personal::getData()
      {
          for(int i=0;i<size;i++)
             {
                 cout<<"\n\tCode? ";
                 cin>>code[i];
                 cin.ignore();
                 cout<<"\n\tName? ";
                 cin.get(name[i],20);
                 cout<<"\n\tAmount Due? ";
                 cin>>amt[i];
             }
      }
  void Personal::showData()
      {
          cout<<"\n\t Code\tName\tAmount";
          cout<<"\n\t==========================";
          for(int i=0;i<size;i++)
             {
                 cout<<"\n\t"<<code[i]<<"\t"<<name[i]<<"\t"<<amt[i];
             }
      }
  class Vendor:public Personal   //Derive Class
      {
          private:
             int amtPaid,cd;
          public:
             Vendor(int x):Personal(x){}
             void Payment()
                 {
                      int f=0;
                      cout<<"\n\tEnter Vendor Code : ";
                      cin>>cd;
                      for(int i=0;i<size;i++)
                         {
                              if(cd==code[i])
                                  {
                                      f=1;
                                      break;
                                  }
                         }
                      if(f==0)
                         {
                              cout<<"\n\t\t**No Such Vendor Presents **";
                         }
                      else if(f==1)
                         {
                              if(amt[i]<=0)
                                  {
                                      cout<<"\n\t No due amount for "<<name[i]<<"\n";
                                  }
                              else
                                  {
                                      cout<<"\n\tEnter Amount Paid : ";
                                      cin>>amtPaid;
                                      amt[i]=amt[i]-amtPaid;
                                      cout<<"\n\tRecord Updated ";
                                      showData();
                                  }

                         }
                 }
      };
  class Customer:public Personal   //Derive Class
      {
          private:
             int amtRecvd,cd;
          public:
             Customer(int x):Personal(x){}
             void Received()
                 {
                      int f=0;
                      cout<<"\n\tEnter Customer Code : ";
                      cin>>cd;
                      for(int i=0;i<size;i++)
                         {
                              if(cd==code[i])
                                  {
                                      f=1;
                                      break;
                                  }
                         }
                      if(f==0)
                         {
                              cout<<"\n\t\t**Invalid Customer Code **";
                         }
                      else if(f==1)
                         {
                              if(amt[i]<=0)
                                  {
                                      cout<<"\n\t No due amount for "<<name[i]<<"\n";
                                  }
                              else
                                  {
                                      cout<<"\n\tEnter Amount Received : ";
                                      cin>>amtRecvd;
                                      amt[i]=amt[i]-amtRecvd;
                                      cout<<"\n\tRecord Updated ";
                                      showData();
                                  }

                         }
                 }
      };

  int main()
      {
          int n;
          cout<<"\n\tEnter Nos of Records : ";
          cin>>n;
          Vendor V(n);
          V.getData();
          V.showData();
          V.Payment();
          Customer C(n);
          C.getData();
          C.showData();
          C.Received();
          return 0;
      }

1 comment:

Anonymous said...

Inheritance in C++
Thanks for this post