If derived class inherits the private members of a base class, then why not constructors?

前端 未结 4 1011
太阳男子
太阳男子 2021-01-03 12:36

I want to clear my understanding of this basic OOPS concept in c#. On most of the internet sites, I read that a derived class inherits the private members o

4条回答
  •  难免孤独
    2021-01-03 13:06

    in order to construct a Manager you need to construct the base class using any constructor in the base class, if there is only one (as in this case) you need to call it. that does not mean you must define a constructor with the same signature.

    you would also be allowed to do this:

    public Manager() : base(100000)
    {
    }
    

    or

    public Manager(string name, int salary) : base (salary)
    {
         // store name
    }
    

    During construction of you Manager you will allocate a new object on the heap. This object will claim enough memory so that it can store the variables defined in the base class (Employee) and concrete class (Manager).

提交回复
热议问题