Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7582499
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T18:26:57+00:00 2026-05-30T18:26:57+00:00

So, at school we got an assignment to make a car in OOP, until

  • 0

So, at school we got an assignment to make a car in OOP, until now its been pretty easy and straight forward. But now I need to create four constructors, one with no parameters, two with one parameter and one with two parameters.

As far as I know the way overloading works is that it checks the amount of parameters you supply it with and then checks which constuctor it has to use. As two of the constructors are the same, both accepts ints, only one needs to change the amount or gears, and the other needs to change the maximum speed.

Is there a way to do this without passing an extra parameter?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T18:26:59+00:00Added an answer on May 30, 2026 at 6:26 pm

    As far as I know the way overloading works is that it checks the amount of parameters you supply it with and then checks which constuctor it has to use.

    No, overloading isn’t based solely on the number of parameters – it’s based on their types too.

    However:

    As two of the constructors are the same, both accepts strings

    That’s a problem. You can’t declare two constructors like this:

    public Foo(string x)
    {
    }
    
    public Foo(string y)
    {
    }
    

    Those signatures clash as far as overloading is concerned.

    I would suggest having public static factory methods, so you can specify what you’re trying to create:

    public static Foo FromGears(string gears)
    {
        return new Foo(...);
    }
    
    public static Foo FromMaximumSpeed(string maxSpeed)
    {
        return new Foo(...);
    }
    

    You’d then possibly have one constructor which accepted both values, and default whichever one’s missing when you call the constructor from the factory method.

    However, there are two other oddities in your description:

    • You’re using strings for two values which sound like they should be numbers (or possibly one number, and one number-and-unit)
    • You’re talking about declaring constructors, but then using the word “change” as if they’re going to change an existing instance. That doesn’t make sense – constructors are used to create new objects.

    EDIT: Okay, now we know a bit more, here’s the sort of thing I mean:

    public class Car
    {
        private const int DefaultGears = 5;
        private const int DefaultTopSpeed = 180;
    
        private readonly int gears;
        private readonly int topSpeed;
    
        public Car(int gears, int topSpeed)
        {
            this.gears = gears;
            this.topSpeed = topSpeed;
        }
    
        public static Car CreateWithGears(int gears)
        {
            return new Car(gears, DefaultTopSpeed);
        }
    
        public static Car CreateWithTopSpeed(int topSpeed)
        {
            return new Car(topSpeed, DefaultGears);
        }
    }
    

    Note that you could use optional parameters and named arguments for this too in C# 4:

    public class Car
    {
        public const int DefaultGears = 5;
        public const int DefaultTopSpeed = 180;
    
        private readonly int gears;
        private readonly int topSpeed;
    
        public Car(int gears = DefaultGears, int topSpeed = DefaultTopSpeed)
        {
            this.gears = gears;
            this.topSpeed = topSpeed;
        }
    }
    

    Then:

    Car car = new Car(gears: 4);
    Car car = new Car(topSpeed: 30);
    

    I wouldn’t recommend that in general though – certainly not while you’re still relatively new to the language. There are various subtleties around optional parameters.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I currently have a school assignment that involves both PHP and asp.net. Now the
I am using external Users database for different projects. Now I have got School
I normally program on Windows, but I got a macbook pro from my school,
We got this assignment from school to reproduce this example (created in Flash) into
I've got a website that was created about an year ago and its been
I'm writing a simple booking program for a car rental (a school assignment). Me
Recently i got an assignment at school, where we are to write a small
I have an assignment for school that is really got the best of me.
So my school website has been hacked cos my professor got XSS-ed. So I
We have an assignment in school where we got a header file and we

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.