I just want make sure I’m doing and thinking right about inheritance and constructors if I have more than one sub class. My classes looks like this, Shape is the base class and the other derived classes
Shape<-----Shape2D<------Box
Is this code correct? it’s working, but I’m just wondering if this is the best way?
public Shape(int inputA, int inputB)
{
valueA = inputA;
valueB = inputB;
}
public Shape2D(int inputA, int inputB) : base(inputA, inputB)
{
}
public Box(int inputA, int inputB) : base(inputA, inputB)
{
}
Yes it seems correct to me.
If you don’t want to do any special initialization in your descendant classes, you can delegate the initialization task to the base class in the hierarchy. The members variables defined there, so it can handle any needed initialization.
If you need something special in the descendants, then you have to handle the special initialization there. But until you need those initializations exactly the same, I think this is the suggested way to do it.
Do you thought something this in your question?