Jump to content
  • Advertisement
Sign in to follow this  
Kenny77

C# Inheritence

This topic is 3553 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I had a problem yesterday with inheritence (RV : Automobile) but fixed it by adding: public RV(int price, int doors, int seats, int hp, string name) : base(price, doors, seats, hp, name) I don't really understand what it's doing with that added part. All I'm told by the book that I have is: "...if the base class does not have a default constructor, every derived constructor must explicitly invoke one of the base class construtors using the base keyword. The keyword base identifies the base class for the current object." Doesn't the line: public class RV : Automobile { ... } Identify the base class?

Share this post


Link to post
Share on other sites
Advertisement
By adding that you're saying "before you call the constructor I'm defining here, call this constructor from the base class and send it these parameters". If you don't add this then the default constructor of the base class will be called. If there is no default constructor in the base class you have to specify one, since some constructor from the base class has to be called. When the book says "The keyword base identifies the base class for the current object", what it means is that the keyword "base" refers to the type specified as the base for inheritance when you declare the class name.

Share this post


Link to post
Share on other sites
So should I make price, doors, seats, hp and name all protected? Because I always thought private members couldn't be inherited.

Share this post


Link to post
Share on other sites
Quote:
Original post by Kenny77
"... The keyword base identifies the base class ..."

Doesn't the line:

public class RV : Automobile
{
...
}

Identify the base class?

Try this:

"... The keyword base identifies the base class' constructor ..."

A base class might have several different constructors, base lets you invoke one of these. If you wish to invoke the default constructor then you don't need to call base explicitly, of course if you omit base and there is no default constructor then it's an error, in much the same way that if you invoke base with a set of arguments that don't match any existing constructor it is also an error.

The identification of which base class is, as you rightly pointed out, specified by the declaration of the class itself.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!