Solved

abstract/virtual

Posted on 2001-07-23
8
333 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 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
Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

 
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

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Migrating From One Mac To Another 2 98
C++ help/ Toy problem 19 70
How to add columns to a RAD Studio FMX grid at design time 1 59
sync two window 10 machine 7 52
VM backup deduplication is a method of reducing the amount of storage space needed to save VM backups. In most organizations, VMs contain many duplicate copies of data, such as VMs deployed from the same template, VMs with the same OS, or VMs that h…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
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.

732 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