#include <iostream>
class Base {
private:
std::string hello{ "hello world!" };
public:
Base() = default;
virtual ~Base() = default;
const std::string &getHello() const {
return hello;
}
void setHello(const std::string &hello) {
Base::hello = hello;
}
};
class Derived : public Base {
public:
Derived() = default;
~Derived() override = default;
};
int main(int argc, const char *argv[]) {
Derived d;
std::cout << d.getHello() << std::endl;
}
Base and Derived all use the default constructor and destructor, I explicitly declare them and marked as default. But in fact, if you don't explicitly declare them, the code can still work very well.
My confusion is whether I need to explicitly declare them. I heard two different arguments, some people think that whether or not you use them, you should declare them, others consider that if you need one, you declare it, otherwise you don't.
So what is the good practice?