1

I'm sure I'm not the first one to ask this question, but I can not find any answer for it.

The thing I want is a base class from which multiple classes are inherited. The inherited classes all have some functions (with implementation) and variables in common and have some variables and functions (different functions for every derived class) of there own. The common members I would like to combine in the base class. The thing is the base class should really just be a base class and I don't want any instances to be made of the base class. How should I do this?

If the above description is not clear, maybe this makes it more clear: let's say I want a base class mammals and derived class such as human, ape, blue whale, etc. I want to create instances of the human, ape, blue whale, etc. but not of mammals.

I have read somewhere you could put the constructor as private, but I need a public constructor of the derived classes

5
  • 3
    make the base class constructors protected Commented Jan 25, 2020 at 9:19
  • 3
    Does this answer your question? Virtual/pure virtual explained Commented Jan 25, 2020 at 9:19
  • 1
    Maybe making base class virtual
    – exodehm
    Commented Jan 25, 2020 at 9:20
  • 1
    @user3733164 What does it mean to make a class virtual? Commented Jan 25, 2020 at 9:23
  • 1
    @DanielLangr, sorry, I wanted to say to make an abstract class, like it's said above. Defining pure virtuals method to get that.
    – exodehm
    Commented Jan 25, 2020 at 9:33

3 Answers 3

3

Making the base class abstract is your solution.

If you don't want to make any base class method pure virtual then you can make the destructor pure virtual

#include <iostream>
using namespace std;

class IMammal //I for Interface
{
    public:
        virtual ~IMammal() = 0; //Makes the class abstract, Pure virtual destructor
};

IMammal::~IMammal() //Its necessary or ld will complain
{
    cout << "In ~IMammal" << endl;
}

class Ape : IMammal
{

};

int main()
{
//    IMammal m; // error: cannot declare variable ‘m’ to be of abstract type ‘IMammal’
    Ape a;
}
6
  • Will the implementation of IMammal::~IMammal() be called when Ape is destructed?
    – C. Binair
    Commented Jan 25, 2020 at 9:40
  • Yes it is called, you can copy paste that code and see yourself, its printing In ~IMammal
    – Sumit
    Commented Jan 25, 2020 at 9:43
  • So do I understand it correctly, that a pure virtual function can still have an implementation in the base class? The only required thing is to put =0 in the header?
    – C. Binair
    Commented Jan 25, 2020 at 9:45
  • 2
    Yes !! Its little known :) but the implementation must be outside the class !!
    – Sumit
    Commented Jan 25, 2020 at 9:47
  • Okay that's great @Sumit. Could you maybe elaborate on the difference between this and using a protected constructor? At this point I think both would work equally well for my case.
    – C. Binair
    Commented Jan 25, 2020 at 9:49
2

Since every method is implemented in the base class, using a protected constructor will allow you to add extra member variables to the derived class without being able to construct the base. To make it even more obvious, I'd probably put the base class in a different namespace, so the caller gets a strong hint that they shouldn't even try to create one.

namespace detail
{

class Mammal
{
public:
    void layEggs() { /*implementation*/ }

protected:
    //Stop anyone creating a Mammal
    //Can still be accessed by derived classes,
    //and anyone that Mammal has declared a friend
    Mammal(int age) : age_(age);

    int age_;
};
}

class Dog : public detail::Mammal
{
public:
    //Dog is still allowed to access Mammal constructor
    Dog(int age, const std::string& name) : 
        Mammal(age), name_(name) 
    {} 

protected:
    std::string name_;
};

Edit: I originally answered without understand that every method was implemented in the base class

You can do this by making the class an abstract class. You do this by creating at least one function a pure virtual function.

class Mammal
{
public:
    void layEggs()
    {
        //implemented in base
    }

    virtual std:string speak() = 0 //pure virtual, so class cannot be instantiated
};

class Dog : public Mammal
{
public:
    virtual std::string speak()
    {
        return "woof";
    }
};

Because Mammal contains a pure virtual function, a variable cannot be created from it. However, Dog has implemented all of the functions, so a variable can be created.

Mammal m; //will result in compiler error, because what would m.speak() do?
Dog d; //is allowed, because we know what d.speak() should do
6
  • I don't have a function which will be called for every derived class but with a different implementation. So if I would do something like this I would need to make a dummy function and give it a dummy implementation in every derived class. This will lead to a messy code in my opinion. But it might indeed be a solution for others!
    – C. Binair
    Commented Jan 25, 2020 at 9:44
  • In that case I would probably take another look at the design - why wouldn't you let people create a Mammal? It might still be a sensible idea, as I haven't seen the code. I'll add a section on protected constructors, which I think is a better way to go in your case.
    – Korosia
    Commented Jan 25, 2020 at 9:46
  • So my code is not really about mammals, but if I try to keep up with the analogue. Let's say I have some pets, but I don't want somebody to have a mammal as pet. I only want them to have the more defined animal so to say.
    – C. Binair
    Commented Jan 25, 2020 at 9:52
  • @C.Binair Is there anything different about each derived class, or they just empty classes? In which case you could make them a typedef.
    – Korosia
    Commented Jan 25, 2020 at 9:54
  • The derived classes are different. They mostly have more member variables. I will generally have the same functions as in the base class but then some extra stuff is done for the extra variables. Lets say for example that I would do void Derived::initialize(int a, int b, int c, int d){Base::initialize(a,b); cc = c; dd = d}
    – C. Binair
    Commented Jan 25, 2020 at 9:59
2

I think the suggestion of Alan Birtles might indeed work. Check this question What are practical uses of a protected constructor?

All functions I would have in the base class have an implementation in the base class. Otherwise using a virtual function would indeed be an option as well. I checked this website https://www.geeksforgeeks.org/pure-virtual-functions-and-abstract-classes/ maybe this is useful for others.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.