ChatGPT解决这个技术问题 Extra ChatGPT

抽象方法和纯虚函数是一回事吗?

As far as I know, both abstract methods and pure virtual functions do NOT provide any functionality ... So can we say they're both the same thing ?

Also, suppose a class (not necessarily declared as abstract) contains a number of implemented methods (not abstract or virtual), but contains a pure virtual function. Is this class then abstract ?

What programming language is this question about?
Likely, but C#, Java and VB.net (amongst others) also implement similar things, so it could be any of those.

G
Gordon Gustafson

Yes, they are the same thing. In C++, an abstract method is just another way of describing the characteristics of a pure virtual function. Both just mean a method with no implementation provided that needs to be implemented in a sub-class before the class can actually be instantiated.

The situation with pure virtual functions and abstract classes in C++ is similar as they essentially mean exactly the same thing. Any abstract class must have at least 1 pure virtual function or else it could be instantiated and wouldn't be abstract. Likewise, any class with at least 1 pure virtual function must be abstract because it needs to be extended so that method can actually be implemented.

Therefore, a class is abstract if and only if it contains at least 1 pure virtual function/abstract method.

Later on, languages like Java and C# made things like this more explicit, allowing a special keyword to define a class abstract rather than the presence of a pure-virtual function. C++ lets you do the same things as these languages, but they're just a little more explicit about it. :D


You can't necessarily instantiate a class without pure virtuals, e.g. if it doesn't have an accessible constructor. Just being pedantic :-)
J
Jordão

You don't explicitly declare classes or methods as abstract in C++. The presence of pure virtual methods is what makes them abstract.


D
David Titarenco

Yes, abstract methods are the exact same thing as pure virtual functions; the terms are often used interchangeably. IMO, "Pure virtual function" is the C++ technically correct term which specifically denotes the fact that the function is set to 0:

class myClass {
public:
  virtual void vfunc() = 0; // pure specifier
};

An abstract class is defined by:

a class that is designed to be specifically used as a base class. An abstract class contains at least one pure virtual function.

So basically, an abstract class is an abstract class because it's designed to be a base class (some base classes by definition need to have implementable methods which will need to be pure virtual). These classes become abstract classes simply by how they are used and extended from. Unlike languages like Java, there is no abstract or interface keyword modifier so this is why we need a "verbal contract" to talk about abstract classes in C++.


L
Lightness Races in Orbit

In C++, a pure virtual member function leads to the enclosing type being an "abstract type".

Functions themselves cannot be abstract, though the term is frequently misused in this manner.


t
taskinoor

I would say yes, abstract methods and pure virtual functions are conceptually the same thing.

Also, suppose a class (not necessarily declared as abstract) contains a number of implemented methods (not abstract or virtual), but contains a pure virtual function. Is this class then abstract ?

A class with at least 1 pure virtual function is called an abstract class.