abstract/virtual

Can someone please give me definitions and simples examples of abstract and virtual functions?  What would make me want to declare a function as abstract?  virtual?  Can a function be declared both and why would I want to do that?
Thanks
omomAsked:
Who is Participating?
 
nietodConnect With a Mentor Commented:
"abstract" is not a term usually applied to functions--I've nver heard it used that way.   it is applied to classes--data types.   An abstract class is one with at least one pure virtual function.   but that is getting ahead of myself.

>> Can a function be declared both and
>> why would I want to do that?
It can be declared virtual or even pure virtual, but not abstract.

continues
0
 
nietodCommented:
This stiff matters when a class is used in a polymorphic maner, that is, when an object of a derived class is access via a reference or pointer to a base class type.  If a derived class overrides a base class version of a function, then when the object is used polymorphically, the compiler will choose to use the version of the function associated with the pointer or reference type, not the derived class type.  For example

class Base
{
public:
   void F()
   {
      cout << "Base version" << endl;
   }
};

class Derived : piublic Base
{
public:
   void F()
   {
      cout << "Deived version" << endl;
   }
};

int main()
{
  Derived D;
  Base &B = D;
  B.F();  // outputs "base version";
  return 0;
}

See in this case, the F function is invoked using a reference whose type is a Base class object, so the compiler invokes the Base::F function, not the Derived::F() function despite the fact that the object is actually a Derived class object.

continues
0
 
nietodCommented:
but if a function is declared virtual, then the compiler will determine which function to call based on the actual type of the object that is being used.  Consider.

class Base
{
public:
   virtual void F()
   {
      cout << "Base version" << endl;
   }
};

class Derived : piublic Base
{
public:
   void F()
   {
      cout << "Deived version" << endl;
   }
};

int main()
{
  Derived D;
  Base &B = D;
  B.F();  // outputs "Derived  version";
  return 0;
}

See, even though the function was invoked through a reference to a Base class, object, the actual object is a Derived object, so the Derived::F() gets called in this case

continues
0
The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

 
nietodCommented:
now a virtual function can be declared as pure.  If a class contains one or more pure virutal functions, then the class is abstract.  Objects of an abstract class cannot be created, attempts to create objects of abstract classes will cause compiler errors.  If a class contains a pure viruytal function, then it is abstract, but derived classes can override pure virtual functions with non-pure versuons.  once all the pure virtual functiosn have been overriden, then a class concrete again, that is, it is not abstract and objects of the class can be created again.  for example

This stiff matters when a class is used in a polymorphic maner, that is, when an object of a derived class is access via a reference or pointer to a base class type.  If a derived class overrides a base class version of a function, then when the object is used polymorphically, the compiler will choose to use the version of the function associated with the pointer or reference type, not the derived class type.  For example

class Base
{
public:
   void F() = 0;  // Made pure virtual.
};

class Derived : piublic Base
{
public:
   void F()
   {
      cout << "Deived version" << endl;
   }
};

int main()
{
  Base B; // ERROR.  won't compile.
  Derived D;  // FINE compiles okay.
  return 0;
}

So you can see declaring a funciton as pure virtual forces derived classes to redefine virutal functions. This is used in cases where the base class needs to define an intefrace--a type of behavior for the derived classes, but where the base class can't impliment the detail--can't write the function.  By declaring the function as pure virtual the base class forces the derived class to define the function.

All of this stuff is best convered in a book dedicated to advanced C++ programming.  I would recommned you get a good C++ text book.

Let me know if you have any questions.
0
 
omomAuthor Commented:
2 questions:

1. A pure virtual function _must_ be overridden?  or can the derived class choose to ignore the function?

2. The way to declare a pure virtual function is to set it equal to 0? (as in your example)

0
 
nietodCommented:
>> 1. A pure virtual function _must_ be overridden?
If it is not overrident in a particular derived class, then that class is still abstract.  That is okay, but you can't create instances of that class.   Abstract classes are not actually useful, except as serving as the base for derived concrete classes (and for writting polymorphic code that works with derived concrete classes.)  So the intention is that ever pure virtual function will be overriden at some point.  however, it might not be an in immediate derived class, for example

class Base
{
public:
   virtual void F1() = 0;
   virtual void F1() = 0;
};

class D1 : public Base
{
public:
   void F1() {}
};

class D2 : public D1
{
public:
   void F2() {}
};

In this case the D1 class overrides 1 of the 2 pure virutal functions.  So D1 is still an abstact class.  but D2 overrides the remaining one and thus becomes a concrete class.  Note that virtual functions don't have to be defined in absolute base classes.   D1 or D2 could add new virutal functions to the interface.  These new functions could be pure virtual too, thus making them abstract classes until the new functions were overridden.

>> 2. The way to declare a pure virtual function is to
>> set it equal to 0? (as in your example)
The _syntax is to add " = 0" at the end of the function declaration/definition  That is syntax only. the "set it equal to 0" is not the semantics of this.  Nothing is being set to 0.  At least nothing tangable to your code.  The designers wanted a way to specify that a function was pure and did not want to add a new keyword ("pure") to the language and for better or worse this is what they came up with.  

Note that you can still have a definiton for the pure virtual funciton, like

class Base
{
public:
   virtual void F() { cout << "some code"; } = 0;
}'

In this case the pure virtual function has an implimentation.  In order to create a concrete class, some
dervied class will evnetually need to override this function, so it seems useless.  BUT it is not uncommon for an overriden virtual function to call the base class version fo the function in order to do some of the work.  So this is usefull for that sort of design.  
0
 
omomAuthor Commented:
Thanks!

For future readers of this question, I think in that last example, the 2nd function should be name F2:

class Base
{
public:
  virtual void F1() = 0;
  virtual void F2() = 0;
};
0
 
nietodCommented:
yes, sorry for the confussion--though it doesn't sound like there was any.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.