0

Suppose the base class has three constructors, one without parameter and one with parameters, and a copy constructor, as follows:

class Base {
  public:
    Base();    //default constructor
    Base(unsigned int width, unsigned int height);    //parametrized constructor
    Base(Base const & other);    //copy constructor
  private:
    int width_;
    int height_;  
}

Suppose a derived class is like this:

class Derived : public Base {
public:
  Derived();    //is this even needed?
  Derived(int width, int height);   //is this even needed?
  Derived(Derived const & other);   //is this even needed?
private:
  void additionalFunction(); //additional functionality not in base class
}

Is it even needed to declare (and define) the constructors for the derived class if it only require the base class's constructors? In another word, do I need to define the derived class constructors like this:

Derived::Derived(){
    Base();
}

And if so, is this the correct way to define the copy constructor:

 Derived::Derived(Base const & other){
     Base(other);
 }

Thanks

user173729
  • 367
  • 1
  • 2
  • 8
  • 1
    You attempt to created "derived class constructors" failed miserably, I'm afraid. All of your attempts to derive them, actually. You see, C++ is just too complicated. Any attempt to guess what the syntax would be, for something, will always end in tears. You need to ***learn*** what the right syntax is, from a good C++ textbook. Every C++ textbook should explain these fundamental concepts, is there something specific in yours', that's unclear? – Sam Varshavchik Sep 11 '22 at 21:50

0 Answers0