CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Guest

    Another constructor question

    Does anyone understand how to take an object of my Distance class as an argument?

    "The fourth and final constructor must take an object of the Distance class as an argument. This constructor will copy the instance variable values from the argument object to the object being instantiated. "

    The following is what I have but I keep getting error

    // default constructor
    public int Distance()
    {
    feet = yards = miles = 0;
    }
    //second constructor //
    public Distance(int f) { feet = f; }
    //third constructor//
    public Distance(int y, char units) // Both variables cannot be named y, so I named the second units.
    {
    switch(units)
    {
    case 'f':
    case 'F':
    feet = y;
    break;
    case 'm':
    case 'M':
    miles = y;
    break;
    case 'y':
    case 'Y':
    yards = y;
    break;
    default:
    feet = yards = miles = 0; }
    //fourth argument
    public int Distance(int arg1)
    {
    feet = arg1;
    }
    }

    This will not compile.
    not sure what is wrong



  2. #2
    Join Date
    Jan 2000
    Location
    Canada
    Posts
    249

    Re: Another constructor question

    When declaring a constructor, it can't have a return type. In the first and fourth constructors in you program, you have int as a return type. remove this and you should be fine.

    -------------------------------------------
    T. Sean Ryan
    http://rastos0.kjm.htmlplanet.com

  3. #3
    Join Date
    Jan 2000
    Location
    CA, USA
    Posts
    305

    Re: Another constructor question

    Your program also contains two constructors
    taking an int. You got to remove one of them.

    Kannan



Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured