Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

abstract/virtual

Posted on 2001-07-23
8
Medium Priority
?
338 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 2
8 Comments
 
LVL 22

Accepted Solution

by:
nietod earned 600 total points
ID: 6309076
"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
ID: 6309102
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
ID: 6309115
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
Learn Veeam advantages over legacy backup

Every day, more and more legacy backup customers switch to Veeam. Technologies designed for the client-server era cannot restore any IT service running in the hybrid cloud within seconds. Learn top Veeam advantages over legacy backup and get Veeam for the price of your renewal

 
LVL 22

Expert Comment

by:nietod
ID: 6309149
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
 

Author Comment

by:omom
ID: 6309722
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
ID: 6309831
>> 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
ID: 6309924
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
ID: 6309981
yes, sorry for the confussion--though it doesn't sound like there was any.
0

Featured Post

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Create your own, high-performance VM backup appliance by installing NAKIVO Backup & Replication directly onto a Synology NAS!
This article shows how to use a free utility called 'Parkdale' to easily test the performance and benchmark any Hard Drive(s) installed in your computer. We also look at RAM Disks and their speed comparisons.
To efficiently enable the rotation of USB drives for backups, storage pools need to be created. This way no matter which USB drive is installed, the backups will successfully write without any administrative intervention. Multiple USB devices need t…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

609 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