Browse DevX
Sign up for e-mail newsletters from DevX

Tip of the Day
Language: Design Patterns
Expertise: Beginner
Sep 7, 1999



Building the Right Environment to Support AI, Machine Learning and Deep Learning

C++ abstract classes

What are Abstract Classes? Please explain with an example.

An abtract class has at least one pure virtual member function. A pure virtual member function is a non-implemented function that serves as a means of enforcing a common interface, or services, in derived classes.

For example, an abstract class File can define abstract operations such as Open and Read, which must be overridden in classed derived from it.

class File
void virtual Open() = 0; //pure virtual
void virtual Read() = 0; //pure virtual
This abstract class ensures that every class derived from it supports the Read and Open services:

class TextFile: public File
void Open() {/*..*/}
void Read() {/*..*/}
Note that you cannot create an instance of an abstract class but you can create pointers to abstract classes.

Unfortunately, many people, as well as primer books, confuse the terms "abstract class" and "abstract data type". It's important to realize that an abstract data type is anything but an abstract class: it has no base class, and no virtual member functions. For example, std::string and std::vector are abstract data types, while class File above is an abstract class.

DevX Pro
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date