3

Main

static void Main(string[] args)
    {
        string name = "Me";
        int height = 130;
        double weight = 65.5;
        BMI patient1 = new BMI();
        BMI patient2 = new BMI(name,height,weight);

        Console.WriteLine(patient2.Get_height.ToString() + Environment.NewLine + patient1.Get_height.ToString() );
        Console.ReadLine();
    }

Base Class

class BMI
{ 
    //memberVariables
    private string newName;
    private int newHeight;
    private double newWeight;

    //default constructor
    public BMI(){}

    //overloaded constructor
    public BMI(string name, int height, double weight)
    {
        newName = name;
        newHeight = height;
        newWeight = weight;
    }

    //poperties
    public string Get_Name
    {
        get { return newName; }
        set { newName = value;}
    }

    public int Get_height 
    {
        get { return newHeight; }
        set { newHeight = value; } 
    }

    public double Get_weight 
    {
        get { return newWeight; }
        set { newWeight = value; }
    }
}

derived class

class Health : BMI
{
    private int newSize;
    public Health(int Size):base()
    {
        newSize = Size;
    }
}

How do i pass in the base parameters from the overloaded constructor in the BMI base class into the Derived Class? Any time i try to pass them into the base parameters i get invalid expression error. Or do i just have to pass them into a Health object in the main with a ? eg

class Health : BMI
{
    private int newSize;

     public Health(int Size, string Name, int Height, double Weight)
    {
        newSize = Size;
        base.Get_Name = Name
        base.Get_weight = Weight;
        base.Get_height = Height;
    }
}
  • I would recommend renaming your properties to just `Height`, `Name`, and `Weight`. Having them prefixed by `Get` makes it look like you're calling a method or accessing a read-only property. – D Stanley May 19 '15 at 15:27

3 Answers3

4

Constructors aren't inherited, so yes you need to create a new constructor for the base class, but you can call the base constructor with the proper parameters:

 public Health(int size, string name, int height, double weight)
    : base(name, height, weight)
{
    newSize = size;
}
D Stanley
  • 149,601
  • 11
  • 178
  • 240
1

Like this:

class Health : BMI
{
    private int newSize;

    public Health(int Size, string Name, int Height, double Weight)
        : base(Name, Height, Weight)
    {
        newSize = Size;
    }
}
David L
  • 32,885
  • 8
  • 62
  • 93
aydjay
  • 858
  • 11
  • 25
0

Why can't you call the base class constructor passing the parameter like

public Health(int Size, string Name, int Height, double Weight)  
    : base(Name, Height, Weight)
{
    newSize = Size;
}
David L
  • 32,885
  • 8
  • 62
  • 93
Rahul
  • 76,197
  • 13
  • 71
  • 125