-5

I have been told that if the decleration of a class member function starts with a virtual keyword and ends with =0, then this function would be a virtual function and the class it belongs to would be an abstract class (all member functions are virtual function, then would be an interface). But I also found that some classes are declared with const=0, but no virtual keyword.

So I want to know if the following two class are the same? If so, why some use virtual while others not. If not, then what are their differences?

class class1 {
   int function1() const = 0;
   int function2() const = 0;
   ...
};

class class2 {
    virtual int function1() const = 0;
    virtual int function2() const = 0;
};
user123
  • 231
  • 2
  • 12

2 Answers2

1

Pure virtual function, is a function that you want to override and use in derived classes. The syntax is:

return_type func_name (param_list) = 0;

This makes your function pure virtual and your class abstract. The class that contains such a function couldn't be instantiated and the function can't have a body.

const means that this function can not modify any other members of the class containing it.

Ziezi
  • 6,375
  • 3
  • 39
  • 49
  • Hi, @simplicis, I have fixed this question, could you please take a look at it? Thank you for your help! – user123 Jan 28 '16 at 04:30
  • As it is now, the aforementioned apply to `class2`. For the `class1`, I can't say anything because I haven't seen such syntax (`int func() = 0`, without `virtual` keyword) being used and I doubt that it is valid. – Ziezi Jan 28 '16 at 05:24
  • Hi, @simplicis, I added a picture where I found this syntax. Could you take a look? It is from a scientific computation lecture slide, I don't think he is wrong in wrting this. – user123 Jan 28 '16 at 05:46
0

= 0 means that the method is pure virtual.

This means that the class can't be instantiated and classes that inherit from that class have to provide an implementation for the method, otherwise those classes will be virtual too ( and can't be instantiated ).

Community
  • 1
  • 1
Hatted Rooster
  • 35,759
  • 6
  • 62
  • 122