8 Replies - 1087 Views - Last Post: 09 January 2010 - 01:12 PM Rate Topic: -----

#1 toggle   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 36
  • Joined: 14-August 09

Question about virtual functions (pure virtual)

Posted 09 January 2010 - 10:20 AM

Hello,

I am learning virtual functions and have come across something like this:

virtual bool keeper(string word)=0;


May I ask, what does the =0 bit do? I have never seen it used before and unsure how to utilize it.

My cpp code looks like this so far. However it only works if the =0 has been removed. If I leave =0 on at compile time I get errors.

bool ReadWords::keeper(string word){
  cout << "working" << endl;
  return true;
 }




Thank you

Is This A Good Question/Topic? 0
  • +

Replies To: Question about virtual functions (pure virtual)

#2 Dogstopper   User is offline

  • The Ninjaducky
  • member icon

Reputation: 2965
  • View blog
  • Posts: 11,222
  • Joined: 15-July 08

Re: Question about virtual functions (pure virtual)

Posted 09 January 2010 - 10:25 AM

The virtual keyword tells the compiler that a only a subclass can implement it. From a Java standpoint, that means it is an abstract function, or one that is not implemented in this class, but instead, it is implemented in a subclass.

The =0 makes that virtual function essentially be null until it is overridden in a subclass.
Was This Post Helpful? 0
  • +
  • -

#3 KYA   User is offline

  • Wubba lubba dub dub!
  • member icon

Reputation: 3202
  • View blog
  • Posts: 19,234
  • Joined: 14-September 07

Re: Question about virtual functions (pure virtual)

Posted 09 January 2010 - 10:26 AM

=0 makes it pure virtual. This means that it will not have an implementation of its own, but rather derived classes must implement.
Was This Post Helpful? 0
  • +
  • -

#4 athlon32   User is offline

  • D.I.C Regular
  • member icon

Reputation: 117
  • View blog
  • Posts: 363
  • Joined: 20-August 08

Re: Question about virtual functions (pure virtual)

Posted 09 January 2010 - 10:27 AM

View Posttoggle, on 9 Jan, 2010 - 09:20 AM, said:

Hello,

I am learning virtual functions and have come across something like this:

virtual bool keeper(string word)=0;


May I ask, what does the =0 bit do? I have never seen it used before and unsure how to utilize it.

My cpp code looks like this so far. However it only works if the =0 has been removed. If I leave =0 on at compile time I get errors.

bool ReadWords::keeper(string word){
  cout << "working" << endl;
  return true;
 }




Thank you


adding and =0; to a virtual function makes it a 'pure' virtual function. Classes with pure virtual functions in not be instantiated, and class that inherit from classes with pure virtual function must provide implementations or else they suffer the same problem. It's a good way to make abstract interfaces.

Hope i helped :D
Was This Post Helpful? 0
  • +
  • -

#5 toggle   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 36
  • Joined: 14-August 09

Re: Question about virtual functions (pure virtual)

Posted 09 January 2010 - 10:32 AM

Ahhh i see, it all makes sense now.

Thanks for the quick replies!
Was This Post Helpful? 0
  • +
  • -

#6 toggle   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 36
  • Joined: 14-August 09

Re: Question about virtual functions (pure virtual)

Posted 09 January 2010 - 11:36 AM

Sorry for the double post but this has kind of broken my code.

You mention you can not instantialise a class which has a pure virtual function inside it. The problem is my class needs to be instantialised.

Can I instantialise using the pure virtual class? as "technically" it is part of that code and has all those functions and calls within it (Heirarchy). The constructor of my main class which has the virtual function inside it needs to be run to start reading in a file.

I apologies if that is really confusing.
Was This Post Helpful? 0
  • +
  • -

#7 athlon32   User is offline

  • D.I.C Regular
  • member icon

Reputation: 117
  • View blog
  • Posts: 363
  • Joined: 20-August 08

Re: Question about virtual functions (pure virtual)

Posted 09 January 2010 - 12:40 PM

You can instantiate classes that have virtual function in them, just not 'pure' virtual functions.

This works:
class some_class {
	public:
		virtual void somefunc();
};



If the class must be instantiated, it can't have a pure virtual function. Pure virtual functions are used in classes that are abstract (that shouldn't exist one there own), to provide an interface that other classes must override and implement

Hope I helped :D
Was This Post Helpful? 0
  • +
  • -

#8 toggle   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 36
  • Joined: 14-August 09

Re: Question about virtual functions (pure virtual)

Posted 09 January 2010 - 01:00 PM

**Deleted**

I think I have found out my solution.

You were a great help.

Thank you

This post has been edited by toggle: 09 January 2010 - 01:10 PM

Was This Post Helpful? 0
  • +
  • -

#9 Bench   User is offline

  • D.I.C Lover
  • member icon

Reputation: 944
  • View blog
  • Posts: 2,464
  • Joined: 20-August 07

Re: Question about virtual functions (pure virtual)

Posted 09 January 2010 - 01:12 PM

You can use the constructor, you just can't create an object of an abstract class
#include <iostream>

class Base
{
    int num;
public:
    Base(int n) : num(n) 
    {
        std::cout << "num is: " << n << std::endl;
    }
    virtual void foo() = 0;
};

class Derived : public Base
{
public:
    Derived(int n) : Base(n) {}
    void foo() { std::cout << "Derived foo" << std::endl; }
};

int main()
{
    Derived d(5);
    Base& ref = d;
    ref.foo();
} 

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1