Click to See Complete Forum and Search --> : Another constructor question


February 24th, 2000, 06:18 AM
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

weaver
February 24th, 2000, 09:24 AM
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

kannanbalu
February 24th, 2000, 02:20 PM
Your program also contains two constructors
taking an int. You got to remove one of them.

Kannan