Solved

abstract/virtual

Posted on 2001-07-23
8
331 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
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
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

 
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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Problem with SqlConnection 4 168
reclamation process error on TSM 6 65
C++ :Change value from  DisableCMD registry 4 59
How do i run a c++ file? 15 23
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…
Are you looking to recover an email message or a contact you just deleted mistakenly? Or you are searching for a contact that you erased from your MS Outlook ‘Contacts’ folder and now realized that it was important.
This tutorial will walk an individual through the process of installing the necessary services and then configuring a Windows Server 2012 system as an iSCSI target. To install the necessary roles, go to Server Manager, and select Add Roles and Featu…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

813 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

11 Experts available now in Live!

Get 1:1 Help Now