Thursday, February 14, 2013

Multiple Inheritance – C++.




 Example :  Queue using Single Inheritance.

/*Multiple Inheritance : class result derived from both Base Class - class Stud and class Marks.*/

  #include <iostream.h>

  class Stud
      {
           protected:
              int rn;
              char Sname[20];
           public:
              void GetData();
              void DispData();
      };
  void Stud::GetData()
      {
           cout<<"\n\tRoll No : ";
           cin>>rn;
           cin.ignore();
           cout<<"\n\tStudent Name : ";
           cin.getline(Sname,20);
      }
  void Stud::DispData()
      {
           cout<<"\n\t* * Marksheet * *";
           cout<<"\n\n\tRoll No. : "<<rn;
           cout<<"\n\tStudent Name : "<<Sname;
      }
  class Marks
      {
           protected:
              int Mrk[5];
           public:
              void GetMarks();
              void  DispMarks();
      };
  void Marks::GetMarks()
      {
           cout<<"\n\tEnter Marks for five Subject : \n";
           for(int i=0;i<5;i++)
              {
                   cout<<"\t";
                   cin>>Mrk[i];
              }
      }

  void Marks::DispMarks()
      {
           cout<<"\n\tMarks Obtained : ";
           for(int i=0;i<5;i++)
              {
                   cout<<Mrk[i]<<" ";
              }
      }
  class Result:public Stud,public Marks
      {
              int Total;
              float Avg;
           public:
           Result()
              {
                   Total=0;
                   Avg=0.0;
              }
              void ShowResult();
      };
  void Result::ShowResult()
      {
           for(int i=0;i<5;i++)
              {
                   Total=Total+Mrk[i];
              }
           Avg=Total/5.0;
           cout<<"Total : "<<Total<<" Average : "<<Avg<<"\n";
      }

  int main()
      {
           Result R;
           R.GetData();
           R.GetMarks();
           R.DispData();
           R.DispData();
           R.DispMarks();
           R.ShowResult();
           return 0;
      }


Example – Parameter Passed through Parameterize Constructor


/*Multiple Inheritance with Parameterize Constructor and
with Private Base class*/

#include <iostream.h>

  class ArrayA
      {
           protected:
              int *Ar;
              int N;
           public:
              ArrayA(int nn,int a[])
                   {
                        N=nn;
                        Ar=new int[N];
                        for(int i=0;i<N;i++)
                            {
                                Ar[i]=a[i];
                            }
                   }
              void DispArr();
      };
  void ArrayA::DispArr()
      {
           int i;
           for(i=0;i<N;i++)
              {
                   cout<<Ar[i]<<" ";
              }
      }
  class ArrayB
      {
           protected:
              int *Br;
              int size;
           public:
              ArrayB(int nn,int a[])
                   {
                        size=nn;
                        Br=new int[size];
                        for(int i=0;i<size;i++)
                            {
                                Br[i]=a[i];
                            }
                   }
              void PrintArr();
      };
  void ArrayB::PrintArr()
      {
           int i;
           for(i=0;i<size;i++)
              {
                   cout<<Br[i]<<" ";
              }
      }
  class Merge:public ArrayA,private ArrayB
      {
           int *Cr;
           int n;
           public:
              Merge(int n1,int a[],int n2,int b[]):ArrayA(n1,a),ArrayB(n2,b)
                   {
                        n=n1+n2;
                        Cr=new int[n];
                   }
              void ArrayMerge();
              void Disp();
      };
  void Merge::ArrayMerge()
      {
           int i;

           for(i=0;i<N+size;i++)
              {
                   if(i<N)
                        {
                            Cr[i]=Ar[i];
                        }
                   else
                        {
                            Cr[i]=Br[i-N];
                        }
              }
      }
  void Merge::Disp()
      {
           cout<<"\n\tSecond Array\n\t";
           PrintArr();
           cout<<"\n\tMerged Array\n\t";
           for(int i=0;i<N+size;i++)
              {
                   cout<<Cr[i]<<" ";
              }
      }
  int main()
      {
           int i,n,n1,*A,*B;
           cout<<"\n\tFirst Array Size? ";
           cin>>n;
           A=new int[n];
           cout<<"\n\tEnter "<<n<<" Nos. for First Array\n";
           for(i=0;i<n;i++)
              {
                   cout<<"\t";
                   cin>>A[i];
              }
           cout<<"\n\tSecond Array Size? ";
           cin>>n1;
           B=new int[n1];
           cout<<"\n\tEnter "<<n<<" Nos. for First         Array\n";
           for(i=0;i<n1;i++)
              {
                   cout<<"\t";
                   cin>>B[i];
              }
           Merge M(n,A,n1,B);
           cout<<"\n\tFirst Array\n\t";
           M.DispArr();
           M.ArrayMerge();
           M.Disp();
           return 0;
      }



Example –  A Simple Library Program



// class Issue derived from Member and Book class

#include<iostream.h>

  class book
      {
           protected:
           int  bcode[10];
           char bname[10][20];
           int  ncopy[10];
           int Nb;
           public:
              void GetBook()
                   {
                        cout<<"\n\tEnter Nos. of Book [Below 10] : ";
                        cin>>Nb;
                        for(int i=0;i<Nb;i++)
                            {
                                cout<<"\n\tEnter Book Code : ";
                                cin>>bcode[i];
                                cin.ignore();
                                cout<<"\n\tEnter Book Name : ";
                                cin.getline(bname[i],20);
                                cout<<"\n\tEnter No. of Copy : ";
                                cin>>ncopy[i];
                                cin.ignore();
                            }
                   }
              void dispBook()
                   {
                        cout<<"\n\t** Book List **";
                        cout<<"\n\t Book\tBook\t\tCopies";
                        cout<<"\n\t Code\tName";
                        cout<<"\n\t===============================";
                        for(int i=0;i<Nb;i++)
                            {
                                cout<<"\n\t "<<bcode[i]<<"\t"<<bname[i]<<"\t\t"<<ncopy[i];
                            }
                        cout<<"\n\t===============================";
                   }

      };
  class member:public book
      {
           protected:
              int mcode[10];
              char mname[10][20];
              int Nm;
           public:
              void GetMember()
                   {
                        cout<<"\n\tEnter Nos. of Member [Below 10] : ";
                        cin>>Nm;
                        for(int i=0;i<Nm;i++)
                            {
                                cout<<"\n\tEnter Member Code : ";
                                cin>>mcode[i];
                                cin.ignore(20,'\n');
                                cout<<"\n\tEnter Member Name : ";
                                cin.getline(mname[i],20);
                            }
                   }
              void dispMember()
                   {
                        cout<<"\n\t** Member List **";
                        cout<<"\n\tMember\tMember";
                        cout<<"\n\tCode\tName";
                        cout<<"\n\t======================";
                        for(int i=0;i<Nm;i++)
                            {
                                cout<<"\n\t"<<mcode[i]<<"\t"<<mname[i];
                            }
                        cout<<"\n\t======================";
                   }
      };
  class issue:public member
      {
           public:
              void BookIssue()
                   {
                        int Mcode;
                        int Bcode;
                        int j=0,i,flag=0;
                        cout<<"\n\tBook Code to be Isuued : ";
                        cin>>Bcode;
                        for(i=0;i<Nb;i++)
                            {
                                if(bcode[i]==Bcode)
                                    {
                                         flag++;
                                         j=i;
                                         if(ncopy[i]>1)
                                              {
                                                  flag++;
                                              }
                                         break;
                                    }
                            }
                        if(flag>=1)
                            {
                                if(ncopy[i]<1)
                                    {
                                         cout<<"\n\t*** No Copy is Available ";
                                    }
                                else
                                    {
                                         flag=0;
                                         cout<<"\n\tMember Code : ";
                                         cin>>Mcode;
                                         for(i=0;i<Nm;i++)
                                              {
                                                  if(mcode[i]==Mcode)
                                                       {
                                                            flag++;
                                                            break;
                                                       }
                                              }
                                         if(flag>=1)
                                              {
                                                  ncopy[i]--;
                                                  cout<<"\n\t* * Book : ";
                                                  cout<<bname[j];
                                                  cout<<" is Issued to : ";
                                                  cout<<mname[i]<<"  * *\n";
                                              }
                                         else
                                              {
                                                  cout<<"\n\t* * Not a Member * *";
                                              }
                                    }
                            }
                        else
                            {
                                cout<<"\n\t* * Book not in the List * *";
                            }
              }
      };

  int main()
      {
           issue i;
           i.GetBook();
           i.dispBook();
           i.GetMember();
           i.dispMember();
           i.BookIssue();
           i.dispBook();
           return 0;
      }




No comments: