0

I am learning C++ right now and I have a probably very basic question.

If I have the following in a .h file:

class Top{
    Top();
    virtual ~Top() = 0;
}

class Bottom: public Top{
    public:
       Bottom(char core);
       virtual ~Bottom();
    public:
       char getCore() const;
    private:
       char core;  
}

Would the implementation of Bottom look like the following?

Top::Top(): Top() {}

//Bottom
Bottom::Top(char core){
    core = core;
}

char Bottom::getCore(){
    return core;
}

Or

Top::Top(): Top() {}

//Bottom
Bottom::Bottom(char core){
    core = core;
}

char Bottom::getCore(){
    return core;
}
10
  • What is virtual ~Operator();? Commented May 11, 2014 at 23:56
  • It is my understanding that a method following a tilde is a deconstructor. Commented May 11, 2014 at 23:56
  • 1
    You are shadowing member property when you declare a method with the same name as it... I am not sure what do you expect by writing Bottom::Bottom(char core){core = core;}. Also if you have a question where you give two options, why won't you try it yourself. It's not like an hour of coding that would go in vain. Commented May 11, 2014 at 23:59
  • 2
    The Top constructors are bad: Delegate construction of top using the standard constructor to the standard constructor? That's a loop. Commented May 12, 2014 at 0:01
  • 1
    @MikeDeSimone: If there is no implementation (even pure virtual functions can have an implementation) for a base-class destructor, there's no way to define the derived-class destructor. Commented May 12, 2014 at 0:15

1 Answer 1

2

There are a few problems with your code.

class Top{
    Top();
    virtual ~Top() = 0;
}

class access control defaults to private. This means that this class has a private constructor and thus cannot be constructed, nor can subclasses be constructed. A fixed version would probably look like:

class Top {
protected:
    Top();
    virtual ~Top() = 0;
}

The virtual destructor means you can't create a Top, but you could still create a subclass.

And as @Deduplicator pointed out, you still need a definition for Top::~Top() because subclasses' destructors will call it:

Top::~Top() {}

The declaration of class Bottom looks fine, though you don't need to repeat public:.

And one minor problem with both class definitions: you forgot the semicolon after the closing brace.

As for the implementation:

Top::Top(): Top() {}

The third Top is incorrect. That syntax is used to pass things to a superclass's constructor, or delegate to another constructor (of which you have none). Top is not its own superclass. The correct code is simply:

Top::Top() {}

Moving on...

Bottom::Bottom(char core){
    core = core;
}

This has a problem because the parameter core hides the class member core. They need to be different names (though there is no requirement that the parameter name in a class declaration match the one in the method definition). I usually fix this by adding a trailing underscore to the parameter name:

Bottom::Bottom(char core_) {
    core = core_;
}

Finally:

char Bottom::getCore(){
    return core;
}

This is wrong because you declared it as a const method but this definition is not const. Fixed:

char Bottom::getCore() const {
    return core;
}

BTW, you can have const and non-const versions of a method. Which one runs depends on if this is const or not.

One last thing: you forgot to define Bottom::~Bottom(). Add this:

Bottom::~Bottom() {
}

I've got a fixed version of this code, plus some token test code, posted online.

Sign up to request clarification or add additional context in comments.

8 Comments

The third Top is incorrect: Delegating or superclass.
core = core: One could use thisinstead.
Top::~Top() is missing too.
Was fixing all that, and writing a test version to catch anything else, while you were commenting. :P
As for using this: Technically yes, but it should be discouraged because humans reading the code might think core refers to the class member if they forgot that it's a parameter.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.