Virtual function is abstract

Virtual function is abstract

We are getting an error message that says a function fromwithin a class C cannot allocate instanceof type C because one of the virtual functions(the one we wrote) is abstract. How can we make it non-abstract?

Before changing an abstract class to a concrete class, you first must understand why the creator of that class made it abstract in the first place. In general, this is a decision that is made during the object-oriented design process. When we speak of abstract in object-oriented terms, it more or less means the same thing as it does in philosophical terms. Something abstract is conceptual; thereforeit is intangible and can be realized only when applied to some thing tangible or concrete.

A classic example of an abstract class is Shape. The Shape may contain member functions to draw a circle, square or triangle, but it means nothing unless it can be applied to something tangible; therefore, the C++ compiler will prohibit any instatiation of objects of type Shape. In order to use the Shape class, you must derive a new class from it that implements the member functions (or methods) defined in the abstract class, thus making the derived class concrete. For example, you may derive a Table, House, and Boat class from the Shape abstract class. Provided you implement the functions in the base class that make it abstract, all your derived classes will be concrete classes. Then, you will be able to instantiate objects of those types. Hence you can have a square table, house, or boat.

Finally, what makes a class abstract in C++ is that at least one member function is declared as pure virtual. Pure virtual functions are virtual functions that are initialized to zero, and contain no implementation. Below is a code example:

// Abstract base class Shape//class Shape{// pure virtual functionspublic:	virtual void Round( ) =3D 0;	virtual void Square( ) =3D 0;	virtual void Triangle( ) =3D 0;};
To use an abstract class, you must derive a new class from it, and override all pure virtual functions by providing implementation. See code extract below:
// Concrete class Table inherits from abstract class shape//class Table : public Shape{// Functions implemented from abstract class shape.  Implementation for these functions will usually be// located in a methods file (.cpp file extension).public	void Round( );	void Square( );	void Triangle( );};

See also  How to Create and Deploy QR Codes Online: A Comprehensive Guide

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist