A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://timsong-cpp.github.io/cppwp/n4140/class.abstract below:

[class.abstract]

10.4 Abstract classes [class.abstract]

The abstract class mechanism supports the notion of a general concept, such as a shape, of which only more concrete variants, such as circle and square, can actually be used. An abstract class can also be used to define an interface for which derived classes provide a variety of implementations.

An abstract class is a class that can be used only as a base class of some other class; no objects of an abstract class can be created except as subobjects of a class derived from it. A class is abstract if it has at least one pure virtual function. [ Note: Such a function might be inherited: see below.  — end note ] A virtual function is specified pure by using a pure-specifier ([class.mem]) in the function declaration in the class definition. A pure virtual function need be defined only if called with, or as if with ([class.dtor]), the qualified-id syntax ([expr.prim]). [ Example:

class point {  };
class shape {                     point center;
public:
  point where() { return center; }
  void move(point p) { center=p; draw(); }
  virtual void rotate(int) = 0;   virtual void draw() = 0;      };

 — end example ] [ Note: A function declaration cannot provide both a pure-specifier and a definition  — end note ] [ Example:

struct C {
  virtual void f() = 0 { };     };

 — end example ]

An abstract class shall not be used as a parameter type, as a function return type, or as the type of an explicit conversion. Pointers and references to an abstract class can be declared. [ Example:

shape x;                        shape* p;                       shape f();                      void g(shape);                  shape& h(shape&);               

 — end example ]

A class is abstract if it contains or inherits at least one pure virtual function for which the final overrider is pure virtual. [ Example:

class ab_circle : public shape {
  int radius;
public:
  void rotate(int) { }
  };

Since shape::draw() is a pure virtual function ab_circle::draw() is a pure virtual by default. The alternative declaration,

class circle : public shape {
  int radius;
public:
  void rotate(int) { }
  void draw();                  };

would make class circle nonabstract and a definition of circle::draw() must be provided.  — end example ]

Note: An abstract class can be derived from a class that is not abstract, and a pure virtual function may override a virtual function which is not pure.  — end note ]

Member functions can be called from a constructor (or destructor) of an abstract class; the effect of making a virtual call ([class.virtual]) to a pure virtual function directly or indirectly for the object being created (or destroyed) from such a constructor (or destructor) is undefined.


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4