Misslinska
Misslinska

Reputation: 92

compilation of derived class with no implementation of pure virtual function does not give any errors

I have read that a pure virtual functions are the virtual functions whose implementation must be provided by the derived class and I wanted to test it. Do do so I derived a class from an abstract class which has a pure virtual member function. For the educational purpose I did not implement this function in the derived class, just to see whether it breaks. To my surprise the code compiled with no problems.

I would like to ask you to help me to understand why my code does compile with no implementation of the pure virtual function in the derived class. I would be very happy to any hint that would help me to understand that.

├── AbstractClass.cpp
├── AbstractClass.h
├── cmake-build-debug
├── CMakeLists.txt
├── DerivedClass.cpp
├── DerivedClass.h
└── main.cpp

main.cpp

#include <iostream>
#include "DerivedClass.h"

int main() {
    std::cout << "Hello, World!" << std::endl;
    return 0;
}

AbstractClass.h

#ifndef TUTORIAL_CLASSES_ABSTRACTCLASS_H
#define TUTORIAL_CLASSES_ABSTRACTCLASS_H


class AbstractClass {
public:
    virtual int& pure_virtual_function(int) = 0;
    virtual int const_member_function() const = 0;
    virtual ~AbstractClass() {};

};


#endif //TUTORIAL_CLASSES_ABSTRACTCLASS_H

AbstractCLass.cpp

#include "AbstractClass.h"

DerivedClass.h

#ifndef TUTORIAL_CLASSES_DERIVEDCLASS_H
#define TUTORIAL_CLASSES_DERIVEDCLASS_H

#include "AbstractClass.h"

    class DerivedClass : public AbstractClass{
    public:
        DerivedClass() {};
    };


    #endif //TUTORIAL_CLASSES_DERIVEDCLASS_H

DerivedClass.cpp

#include "DerivedClass.h"

X555LJ:~/CLionProjects/tutorial_classes/cmake-build-debug$ make
[100%] Built target tutorial_classes

Upvotes: 0

Views: 180

Answers (1)

rafauu
rafauu

Reputation: 101

From cppreference abstract class:

An abstract class is a class that either defines or inherits at least one function for which the final overrider is pure virtual.

You haven't tried to create instance of it so it works as expected - DerivedClass is abstract as well.

Upvotes: 3

Related Questions