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




Thursday, February 7, 2013

Single Inheritance – C++.




 Example :  Queue using Single Inheritance.


#include <iostream.h>
#include<stdio.h>
#include <process.h>
#define MAX 20
  class Queue
      {
           protected: int front,rear;
              int arr[MAX];
              int cap;
              int item;
           public:
              Queue(int x)
                   {
                        cap=x;
                        front=-1;
                        rear=-1;
                   }
              void push()
                   {
                        if(rear==-1 && front==-1)
                            {
                                front=rear=0;
                            }
                        cout<<"\n\tEnter The element: ";
                        cin>>item;
                        arr[rear++]=item;
                   }

              void pop()
                   {
                        cout<<"\n\tPopped element is :"<<arr[front];
                        front++;
                   }
              void display()
                   {
                        int i=front;
                        cout<<"\n\tElements in the Queue : ";
                        while(i<rear)
                            {
                                cout<<" "<<arr[i];
                                i++;
                            }
                   }
      };

  class My_Queue: public Queue
      {
           public:
              My_Queue(int x):Queue(x)
                   {
                   }
              int push_check()
                   {
                        if(rear>=cap)
                            {
                                cout<<"\n\tQueue in Overflow Condition!\n";
                                return 0;
                            }
                        else
                            {
                                return 1;
                            }
                   }

              int pop_check()
                   {
                        if(front==rear)
                            {
                                cout<<"\n\tQueue in Underflow Condition!\n";
                                return 0;
                            }
                        else
                            return 1;
                   }
      };

  void main()
      {
           int c,flag;
           cout<<"\n\tEnter Queue Size : ";
           cin>>c;
           My_Queue Q(c);
           c=0;
           while(c!=4)
              {
                   cout<<"\n\t1.Push";
                   cout<<"\n\t2.Pop";
                   cout<<"\n\t3.Display";
                   cout<<"\n\t4.Exit";
                   cout<<"\n\tEnter a Choice : ";
                   cin>>c;
                   switch(c)
                        {
                            case 1:    flag=Q.push_check();
                                          if(flag==1)
                                              {
                                                  Q.push();
                                              }
                                          break;
                            case 2:    flag=Q.pop_check();
                                          if(flag==1)
                                              {
                                                  Q.pop();
                                              }
                                          break;
                            case 3:    flag=Q.pop_check();
                                          if(flag==1)
                                              {
                                                  Q.display();
                                              }
                                          break;
                            case 4:  exit(1);
                            default: cout<<"INVALID CHOICE"<<endl;
                        }

              }
      }


Example – Using Parameterize Constructor

#include <iostream.h>


  class Product
      {
           protected:
              char Pname[20];
              int Pcode;
           public:
              Product(char N[], int P)
                   {
                        strcpy(Pname,N);
                        Pcode=P;
                   }
              void Show()
                   {
                        cout<<"\n\tProduct Name    : "<<Pname;
                        cout<<"\n\tProduct code    : "<<Pcode;
                   }
      };

  class Sales:private Product
      {
           protected:
              float Quantity,Price;
              public:
              Sales(float Q,float p,char N[],int P):Product(N,P)
                   {
                        Quantity=Q;
                        Price=p;
                   }
              void Calculate()
                   {
                        float Sale;
                        Sale=Quantity*Price;
                        Show();
                        cout<<"\n\tRate of product : "<<Price;
                        cout<<"\n\tQuantity        : "<<Quantity;
                        cout<<"\n\tTotal Sale      : "<<Sale;
                   }
      };

  void main()
      {
           float Qt,pr;
           int pc;
           char Nm[50];
           cout<<"\n\tProduct Name?  ";
           cin.getline(Nm,50);
           cout<<"\n\tProduct Code?  ";
           cin>>pc;
           cout<<"\n\tProduct Price? ";
           cin>>pr;
           cout<<"\n\tQuantity Sold? ";
           cin>>Qt;
           Sales S(Qt,pr,Nm,pc);
           S.Calculate();
      }



Example – Using Parameterize Constructor


#include<iostream.h>
#include<string.h>
  class Employee
      {
           protected:
              char  Ename[20];
              float Basic;
           public:
              Employee()
                   {}
              Employee(char nm[],float b)
                   {
                        strcpy(Ename,nm);
                        Basic=b;
                   }
              void Display()
                   {
                        cout<<"\n\tEmployee Name : "<<Ename;
                        cout<<"\n\tBasic Salary : "<<Basic;
                   }
      };
  class Salary:public Employee
      {
           private:
              int Days;
              float Sal;
           public:
              Salary()
                   {}
              Salary(char n[],float b,int d):Employee(n,b)
                   {
                        Days=d;
                   }
              void Cal()
                   {
                        Sal=((Basic/30)*Days);
                        cout<<"\n\tDays : "<<Days;
                        cout<<"\n\tSalary : "<<Sal;
                   }
      };
  void main()
      {
           char n[20];
           int d;
           float b;
           cout<<"\n\tEnter Employee Name : ";
           cin.getline(n,20);
           cout<<"\n\tEnter Days Worked : ";
           cin>>d;
           cout<<"\n\tEnter Basic Salary : ";
           cin>>b;
           Salary S(n,b,d);
           S.Display();
           S.Cal();
      }