Solved

abstract/virtual

Posted on 2001-07-23
8
326 Views
Last Modified: 2013-11-15
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
0
Comment
Question by:omom
  • 6
  • 2
8 Comments
 
LVL 22

Accepted Solution

by:
nietod earned 150 total points
Comment Utility
"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
 
LVL 22

Expert Comment

by:nietod
Comment Utility
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
 
LVL 22

Expert Comment

by:nietod
Comment Utility
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
 
LVL 22

Expert Comment

by:nietod
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:omom
Comment Utility
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
 
LVL 22

Expert Comment

by:nietod
Comment Utility
>> 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
 

Author Comment

by:omom
Comment Utility
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
 
LVL 22

Expert Comment

by:nietod
Comment Utility
yes, sorry for the confussion--though it doesn't sound like there was any.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Replication has always been one of those technologies that people run scared from. The main reason is usually cost. When you think of replication, your mind drifts to solutions that replicate from one disk frame to another using block level technolo…
VM backups can be lost due to a number of reasons: accidental backup deletion, backup file corruption, disk failure, lost or stolen hardware, malicious attack, or due to some other undesired and unpredicted event. Thus, having more than one copy of …
This tutorial will show how to configure a single USB drive with a separate folder for each day of the week. This will allow each of the backups to be kept separate preventing the previous day’s backup from being overwritten. The USB drive must be s…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now