Thursday, March 28, 2013

Virtual Function – C++.




The functions which can be overridden within inheriting class is known as virtual functions, this also follows the polymorphism principle of object-oriented programming. A virtual function is a member function that is declared within a base class and redefined by a derived class. A virtual function should precede with keyword 'virtual'.

Pure Virtual function : Pure virtual function is a that implemented in the derived class. Pure virtual functions have no body in the base classes and have to overloaded in the derived classes.

The pure virual function in the base class define as :

virtual void show()=0;

Example  of Virtual Function:

#include <iostream.h>
  class Base
      {
           protected:
              int a;
           public:
                   Base(int a)
                        {
                            this->a=a;
                        }
                   void showData()
                        {
                            cout<<"\n\tValue in Base Class  : "<
                        }
      };
  class Derived:public Base
      {
           public:
              int b;
              Derived(int x,int y):Base(x)
                   {
                        b=y;
                   }
              void showData()
                   {
                        cout<<"\n\t Value-1 in Derived class : "<
                        cout<<"\n\t Value-2 in Derived class : "<
                   }
      };
  int main()
      {
           int n;
           Base *bptr;      //Base Pointer
           Base Bs(10);
           bptr=&Bs;     //Base Address
           bptr->showData();
           Derived Dr(10,20);
           bptr=&Dr;
           bptr->showData(); //showData() from Base class
           Derived *dptr;
           dptr = &Dr;
           dptr->showData(); //showData() from derived class
           ((Derived *)bptr)->b=500;
           ((Derived *)bptr)->showData();
           return 0;
      }

Example  of Pure Virtual Function:



#include <iostream.h>

  class Base
      {
           public:
              //pure virtual Function (without Body)
              virtual void showData()=0;
      };
  class Derived_1:public Base
      {
           public:
              void showData()
                   {
                        cout <<"\n\tIn First Derived Class";
                   }
      };
  class Derived_2:public Base
      {
           public:
              void showData()
                   {
                        cout <<"\n\tIn Second Derived Class";
                   }
      };

  class Derived_3:public Base
      {
           public:
              void showData()
                   {
                        cout <<"\n\tIn Third Derived Class";
                   }
      };

  int main()
      {
            int i;
            Base *B[3];
            Derived_1 D1;
            Derived_2 D2;
            Derived_3 D3;
            B[0]=&D1;
            B[1]=&D2;
            B[2]=&D3;
            for(i=0;i<3 i="" span="">
              {
                   B[i]->showData();
              }
           return 0;
      }


Thursday, March 21, 2013

Virtual Base Class – C++.



In class hierarchies we have many common things and  the common code is implemented in a base class. Methods and variables and other objects accessed through multiple-inheritance. Base class have only one instance, there is no ambiguity when accessing these names. So,  virtual base classes offer a way to save space and avoid uncertainties in class hierarchies that use multiple inheritance.


Example :

#include <iostream.h>

  class Stud_det
      {
          protected:
             int Roll;
             char name[20];
          public:
             void getData();
             void showData();
      };
  void Stud_det::getData()
      {
          cout<<"\n\tRoll No? ";
          cin>>Roll;
          cout<<"\n\tStudent Name? ";
          cin>>name;
      }
  void Stud_det::showData()
      {
          cout<<"\n\n\n\t\tStudent Details";
          cout<<"\n\n\tRoll No:"<<Roll<<"  Name : "<<name;
      }
  class Exam:virtual public Stud_det
      {
          protected:
             int marks[5];
          public:
             void getMarks();
             void showMarks();
      };
  void Exam::getMarks()
      {
          char Sub[5][10]={"Eng","Maths","Phy","Chem","Comp"};
          cout<<"\n\tMarks For\n";
          for(int i=0;i<5;i++)
             {
                 cout<<"\n\t"<<Sub[i]<<"? ";
                 cin>>marks[i];
             }
      }
  void Exam::showMarks()
      {
          cout<<"\n\tMarks Obtained\n";
          char Sub[5][10]={"Eng","Maths","Phy","Chem","Comp"};
          for(int i=0;i<5;i++)
             {
                 cout<<"\n\t"<<Sub[i]<<"  :  "<<marks[i];
             }
      }
  class Extra_Curr:public virtual Stud_det
      {
          protected:
             int Point;
          public:
          void getPoint();
          void showPoint();
      };
  void Extra_Curr::getPoint()
      {
          cout<<"\n\tPoints For Extra Curricular Activites : ";
          cin>>Point;
      }
  void Extra_Curr::showPoint()
      {
          cout<<"\n\tPoints For Extra Curricular Activites : ";
          cout<<Point;
      }

  class Result:public Exam,public Extra_Curr
      {
          public:
             void showResult();
      };
  void Result::showResult()
      {
          int Tmarks=0;
          float Avg=0;
          for(int i=0;i<5;i++)
             {
                 Tmarks=Tmarks+marks[i];
             }
          Tmarks+=Point;
          Avg=Tmarks/5.0;
          cout<<"\n\tTotals : "<<Tmarks;
          cout<<"\n\tAggregrate : "<<Avg;
      }

  int main()
      {
          Result Rs;
          Rs.getData();
          Rs.getMarks();
          Rs.getPoint();
          Rs.showData();
          Rs.showMarks();
          Rs.showResult();
          return 0;
      }


Thursday, March 14, 2013

Multipath Inheritance – C++.



When a class is derived from two or more classes, which are derived from the same base class is know as multipath inheritance. Multipath inheritance can consists many types like single, multiple, multilevel and hierarchical etc.


Example : class Marks and Extra_curricular derived from Student class and class Result drived from both Marks and Extra_curricular class.

#include <iostream.h>

  class Student
      {
          protected:
             char sname[20];
             int rn;
          public:
             void getData()
                 {
                      cout<<"\n\tRoll No.? ";
                      cin>>rn;
                      cin.ignore();
                      cout<<"\n\tStudent Name? ";
                      cin.get(sname,20);
                 }
             void Disp()
                 {
                      cout<<"\n\n\t\t\t* * Marksheet * *\n";
                      cout<<"\n\n\t\tRoll : "<<rn<<"\tName : "<<sname;
                 }
      };
  class Marks: public Student
      {
          protected:
             int m[5],tot;
          public:
             Marks()
                 {
                      tot=0;
                 }
             void getMarks()
                 {
                      char sub[6][10]={"Eng","Maths","Phy","Chem" ,"Comp","Lang"};
                      for(int i=0;i<5;i++)
                         {
                              cout<<"\n\t\tMarks for "<<sub[i]<<" : ";
                              cin>>m[i];
                              tot=tot+m[i];
                         }
                 }
      };
  class Extra_curricular
      {
          protected:
             int score;
          public:
             void getScore()
                 {
                      cout<<"\n\tEnter Sports Score ( Between 1 - 10 ) : ";
                      cin>>score;
                 }
      };

  class Result:public Extra_curricular,public Marks
      {
          private:
             char grade;
             float avg;
          public:

             void calResult()
                 {
                      //getData();
                      if(score>=8)
                         {
                              tot=tot+60;
                         }
                      else if(score>=5)
                         {
                              tot=tot+45;
                         }
                      else if(score>=3)
                         {
                              tot=tot+30;
                         }
                      else
                         {
                              tot=tot+10;
                         }

                      avg=tot/5.0;
                      if(avg<35)
                         {
                              grade='F';
                         }
                      else if(avg<46)
                         {
                              grade='E';
                         }
                      else if(avg<60)
                         {
                              grade='D';
                         }
                      else if(avg<80)
                         {
                              grade='C';
                         }
                      else if(avg<90)
                         {
                              grade='B';
                         }
                      else
                         {
                              grade='A';
                         }

                 }
             void DispResult()
                 {
                      Disp();
                      char sub[6][10]={"Eng","Maths","Phy","Chem" ,"Comp","Lang"};
                      cout<<"\n\t\tSubject  Marks";
                      for(int i=0;i<5;i++)
                         {
                              cout<<"\n\t\t"<<sub[i]<<"\t  "<<m[i];
                         }
                      cout<<"\n\t\tSport Scoring : "<<score;
                      cout<<"\n\n\t\tTotal :"<<tot<<"  Average : "<<avg;
                      cout<<"\n\n\t\tGrade : "<<grade;
                      if(grade=='F')
                         {
                              cout<<"\t\t* * Not Promoted ";
                         }
                      else
                         {
                              cout<<"\t\t* * Promoted";
                         }
                 }
      };

  int main()
      {
          Result R;
          R.getData();
          R.getMarks();
          R.getScore();
          R.calResult();
          R.DispResult();
          return 0;
      }