Social Icons

banner image

virtual base class..

Virtual Functions (I)
Member function preceded by keyword ‘virtual’ in base class
and overridden in derived class
If object of base class invokes virtual function, then copy of
base class is invoked and if derived class object invokes it,
then copy of derived class is invoked.
Virtual functions are declared to specify late binding.
When base class pointer points at derived class object, c++
determines which copy to be called depending upon the type
of the object at run time
They are resolved at run time not at compile time
Virtual Functions (II)
General rules while defining virtual function:
Must be member of some class
Accessed using object pointers
Can be friend of another class
Prototype of base class and derived class virtual function must
be identical
No need to use keyword ‘virtual’ in definition if its is defined
outside the class
Can not be a static member

EXAMPLES:
EX 1:
#include <iostream.h>

class B
{
public:
virtual void display()
{ cout<<"Content of base class.\n"; }
};

class D : public B
{
public:
       void display()
{ cout<<"Content of derived class.\n"; }
};

int main()
{
B *b;
D d;
// b->display();

b = &d;    /* Address of object d in pointer variable */
b->display();
return 0;

}
exp 2:

#include <iostream.h>
class B
{
public:
 virtual void display()      /* Virtual function */
{ cout<<"Content of base class.\n"; }
};

class D1 : public B
{
public:
void display()
{ cout<<"Content of first derived class.\n"; }
};

class D2 : public B
{
public:
void display()
{ cout<<"Content of second derived class.\n"; }
};

int main()
{
B *b;
D1 d1;
D2 d2;

/* b->display();  // You cannot use this code here because the function of base class is virtual. */

b = &d1;
b->display();   /* calls display() of class derived D1 */
b = &d2;
b->display();   /* calls display() of class derived D2 */


}
exp 3:
#include<iostream.h>
#include<conio.h>
class student
{            protected: int rn;
                public:
                       void getrn(int a)
                       {
                            rn=a;
                       }
                       void showrn()
                       {
                            cout<<"rollno="<<rn<<"\n";
                       }
};
class test:virtual public student
{
      protected:
      float part1,part2;
      public:
             void getmarks(float a,float b)
             {
                  part1=a;
                  part2=b;
                  }
                  void showmarks()
                  {
                       cout<<"marks 1"<<part1<<"marks2"<<part2;
                  }
};
class sports: virtual public student
{
      protected:
      float score;
      public:
             void getscore(float a)
             {
                  score=a;
                  }
                  void showscore()
                  {
                       cout<<"sports="<<score;
                  }
                  };
class result:public test,public sports
                  {
                        float total;
                        public:
                               void display()
                               {
                                    total=part1+part2+score;
                                    showrn();
                                    showmarks();
                                    showscore();
                                    cout<<"total="<<total;
                                    }
                               };
      int main()
      {
          result s;
          s.getrn(10);
          s.getmarks(40,30);
          s.getscore(20);
          s.display();
          getch();
          return 0;
          }









virtual base class.. virtual base class.. Reviewed by Shobhit Goel on May 05, 2015 Rating: 5

No comments:

Airtel Hackaton 2017

Powered by Blogger.