-2

I need help with a code I'm working on about classes and inheritance.

I created a simple Class of base information

class BaseStuff
    {
        public string ownersName;
        public int price;
        public string location;
        public int phone;
        public string space;
        public BaseStuff(string ownersName, int price, string location, int phone, string space)
        {
            this.ownersName = ownersName;
            this.price = price;
            this.location = location;
            this.phone = phone;
            this.space = space; 
        }
    }

After that I tried creating anotehr class that inherit information on the "BaseStuff" class, but with an add of new information that's special to that spacific class (floor number and has elevator).

 class Apartment : BaseStuff
    {
        public int floor;
        bool hasElevator;
        public Apartment(int floor, bool hasElevator, string ownersName, int price, string location, int phone, string space)
        {
            this.ownersName = ownersName;
            this.price = price;
            this.location = location;
            this.phone = phone;
            this.space = space;
            this.floor = floor;
            this.hasElevator = false;
        }
    }

I know this code is wrong since it's not how you make a class inherit information from another class, and I'm struggeling to get the "Apartment" class to inherit information from the "BaseStuff" Class.

What can I do to fix it?

Thanks!

  • "I know this code is wrong" is not a precise enough error description for us to help you. *What* doesn't work? *How* doesn't it work? What trouble do you have with your code? Do you get an error message? What is the error message? Is the result you are getting not the result you are expecting? What result do you expect and why, what is the result you are getting and how do the two differ? Is the behavior you are observing not the desired behavior? What is the desired behavior and why, what is the observed behavior, and in what way do they differ? – Jörg W Mittag Apr 11 '21 at 12:00
  • https://idownvotedbecau.se/itsnotworking/ https://idownvotedbecau.se/noexceptiondetails/ – Jörg W Mittag Apr 11 '21 at 12:00
  • Does this answer your question? [Calling the base constructor in C#](https://stackoverflow.com/questions/12051/calling-the-base-constructor-in-c-sharp) – Progman Apr 11 '21 at 12:00
  • Make your BaseStuff class public. See answer below to improve your constructor. Your field declations are accessible in Apartment already, they are public. From anywhere in your Apartment class, you will be able to access read/write these inherited fields in BaseStuff. Same counts for public methods. The inherited fields you want to use in Apartment should be public, or.. protected. Make them protected, when you don't want other classes to access them, *only* Apartment and other derivatives of BaseStuff will be able to access them. – Goodies Apr 11 '21 at 12:32

1 Answers1

2

You can pass constructor arguments to the base class constructor with the base keyword:

class Apartment : BaseStuff
{
    public int floor;
    bool hasElevator;

    public Apartment(int floor, bool hasElevator, string ownersName, int price, string location, int phone, string space)
        : base(ownersName, price, location, phone, space)
    {
        // BaseStuff constructor will take care of the rest
        this.floor = floor;
        this.hasElevator = false;
    }
}
Mathias R. Jessen
  • 157,619
  • 12
  • 148
  • 206