#Previous question #Index of C++ Questions #Next question

Virtual functions ensure that the code that manipulates objects of the base type can without change manipulate derived type objects as well. Virtual functions should however always be called using either a pointer or a reference. If we try to do so using an object a phenomenon called object slicing takes place.

class base
{
  public:
  virtual void funct1(void);
};
class derived : public derived
{
  public:
  virtual void funct1(void);
  virtual void funct2(void);
};
int main (int argc, char *argv[])
{
  base b;
  derived d;
  b = d;
  b.funct1(); //calls base::funct1()
  d.funct1(); //calls derived::funct1()
}

You have viewed 1 page out of 62. Your C++ learning is 0.00% complete. Login to check your learning progress.

Most popular topics (must visit)

Useful links for students