0

I am wondering whether it makes sense to have a class without constructor if the derived classes have one. If not, is there a way of doing what I want differently?

My case is the following: I want the users to only use the derived classes, but those have data members and methods in common.

EDIT: as default constructors exist for classes, it means that the user can always create an instance of Instrument directly, then how can I do what I want?

class instrument{
    public:
        double get_price();
        std::string get_udl();

    private:
        double m_price;
        std::string m_udl;

};

class stock : public instrument{
    public:
        double get_dividend();

    private:
        double m_dividend;
};

class option : public instrument{
    public:
        double get_strike();
    private:
        double m_strike;
};
astudentofmaths
  • 1,122
  • 2
  • 19
  • 33

4 Answers4

5

I want the users to only use the derived classes, but those have data members and methods in common

Have your default (or any other) c'tor, but declare it with protected access. Now only derived classes may invoke it. Effectively making it usable only when inherited.

class instrument{
    public:
        double get_price();
        std::string get_udl();

    protected:
        instrument() = default;

    private:
        double m_price;
        std::string m_udl;

};

This also avoids the overhead associated with making a class abstract. If you don't need polymorphism, you shouldn't use it just to make a class usable only as a base class.

StoryTeller - Unslander Monica
  • 165,132
  • 21
  • 377
  • 458
  • Can you please elaborate your last thought about abstract class and polymorphism? It’s actually possible using polymorphism with your solution. – Germán Jan 03 '18 at 08:35
  • @Germán - The last sentence isn't to say one cannot make `instrument` abstract. It's to say that one shouldn't make it abstract *just* so it cannot be instantiated on its own. Polymorphism brings cost. And I don't think this simple use case warrants it. – StoryTeller - Unslander Monica Jan 03 '18 at 08:37
  • I agree with you about the cost and using it only when it’s necessary. And I clearly prefer your solution over the abstract class one. But I understand from your comment that abstract class brings more overhead than this solution. And that is what I don’t see. – Germán Jan 03 '18 at 08:57
  • @Germán - An abstract class means run time polymorphism support must be added (recall that a `vptr` and `vtable` are only created when the first virtual function is decalred). The cost I'm speaking of is in object size. – StoryTeller - Unslander Monica Jan 03 '18 at 08:59
  • I see now. Thanks! – Germán Jan 03 '18 at 09:44
1

Have thought of Abstract class ?

An abstract class is a class that is declared abstract—it may or may not include abstract methods. Abstract classes cannot be instantiated, but they can be subclassed.

Isn't this what you are looking for ?

Devidas
  • 2,479
  • 9
  • 24
  • I believe this is the answer and [here](https://stackoverflow.com/questions/9756893/how-to-implement-interfaces-in-c) is a post with an example of implementation. – bboll Jan 03 '18 at 05:39
  • 2
    I'm pretty sure that quote is Java documentation. C++ doesn't have an abstract keyword. – Zebrafish Jan 03 '18 at 05:41
  • @Zebrafish Good catch, maybe the answer should be edited with just a loose definition of the concept. – bboll Jan 03 '18 at 05:49
  • 1
    You can make a class abstract by using the following syntax for any member methods C++: `virtual void foobar(void) = 0;` But note that this will force you to implement this function in every single class deriving from your abstract base class. – corsel Jan 03 '18 at 05:49
  • (downvote) This is cut and pasted from Java documentation. – Galik Jan 03 '18 at 06:02
  • Good catch , I have studied Java . But I checked similar things exist in C++ So I posted. You have C++ definition Please edit my message. – Devidas Jan 03 '18 at 07:08
-1

When there's no default constructor defined, the compiler will automatically generate one for you, doing nothing for members of basic types and calls the default constructor for members of struct/class types.

So the conclusion is, it makes no sense to have a class without any constructor because whenever you define an instance of that class you have to call at least one (more may be called with deligate construction).

iBug
  • 35,554
  • 7
  • 89
  • 134
-2

Yes! Definitely it make sense. Because when user create the instance of derived class, first of all parent class's constructor will be called from within the derived class constructor. Even without you explicitly call it. And parent class's default constructor will be called even if you didn't create one. By default

public constructor with no parameter is present in every class
if it has no overridden constructor.

And yes all the public members of parent class will be present in derived class after creating derived class instance.

Abdur Rahman
  • 894
  • 1
  • 11
  • 27