0% found this document useful (0 votes)
2 views3 pages

Abstract Classes (C++ Only) - IBM Documentation

An abstract class in C++ is a base class that contains at least one pure virtual function, which is declared using a pure specifier. You cannot instantiate an abstract class or use it as a parameter type, but you can declare pointers and references to it. Derived classes must override all pure virtual functions to avoid being abstract themselves.

Uploaded by

Naveen Karyana
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views3 pages

Abstract Classes (C++ Only) - IBM Documentation

An abstract class in C++ is a base class that contains at least one pure virtual function, which is declared using a pure specifier. You cannot instantiate an abstract class or use it as a parameter type, but you can declare pointers and references to it. Derived classes must override all pure virtual functions to avoid being abstract themselves.

Uploaded by

Naveen Karyana
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Abstract classes (C++ only)

Last Updated: 2021-04-08

An abstract class is a class that is designed to be specifically used as a base class. An


abstract class contains at least one pure virtual function. You declare a pure virtual function
by using a pure specifier (= 0) in the declaration of a virtual member function in the class
declaration.

The following is an example of an abstract class:

class AB {
public:
virtual void f() = 0;
};

Function AB::f is a pure virtual function. A function declaration cannot have both a pure
specifier and a definition. For example, the compiler will not allow the following:

struct A {
virtual void g() { } = 0;
};

You cannot use an abstract class as a parameter type, a function return type, or the type of
an explicit conversion, nor can you declare an object of an abstract class. You can, however,
declare pointers and references to an abstract class. The following example demonstrates
this:

https://www.ibm.com/docs/en/zos/2.4.0?topic=only-abstract-classes-c 25/05/25, 12 11 PM
Page 1 of 3
:
struct A {
virtual void f() = 0;
};

struct B : A {
virtual void f() { }
};

// Error:
// Class A is an abstract class
// A g();

// Error:
// Class A is an abstract class
// void h(A);
A& i(A&);

int main() {

// Error:
// Class A is an abstract class
// A a;

A* pa;
B b;

// Error:
// Class A is an abstract class
// static_cast<A>(b);
}

Class A is an abstract class. The compiler would not allow the function declarations A g()
or void h(A), declaration of object a, nor the static cast of b to type A.
Virtual member functions are inherited. A class derived from an abstract base class will also
be abstract unless you override each pure virtual function in the derived class.

For example:

class AB {
public:
virtual void f() = 0;
};

class D2 : public AB {
void g();
};

int main() {
D2 d;
}

https://www.ibm.com/docs/en/zos/2.4.0?topic=only-abstract-classes-c 25/05/25, 12 11 PM
Page 2 of 3
:
The compiler will not allow the declaration of object d because D2 is an abstract class; it
inherited the pure virtual function f()from AB. The compiler will allow the declaration of
object d if you define function D2::f(), as this overrides the inherited pure virtual function
AB::f(). Function AB::f() needs to be overridden if you want to avoid the abstraction of
D2.

Note that you can derive an abstract class from a nonabstract class, and you can override a
non-pure virtual function with a pure virtual function.

You can call member functions from a constructor or destructor of an abstract class.
However, the results of calling (directly or indirectly) a pure virtual function from its
constructor are undefined. The following example demonstrates this:

struct A {
A() {
direct();
indirect();
}
virtual void direct() = 0;
virtual void indirect() { direct(); }
};

The default constructor of A calls the pure virtual function direct() both directly and
indirectly (through indirect()).

Parent topic:
Inheritance (C++ only)

Related reference
Virtual functions (C++ only)
Virtual function access (C++ only)

https://www.ibm.com/docs/en/zos/2.4.0?topic=only-abstract-classes-c 25/05/25, 12 11 PM
Page 3 of 3
:

You might also like