Using Multiple constructors in base and sub class
-
I have the following problem. Say, that I have a class called Point which has x,y and the distance from (0,0). There are two constructors, one take all three values and the other take only x ,y. I subclass this to a circle class which has its own radius and distance from (0,0). To initialize I want to use two different constructor that use either of the base class constructors. This causes an error in the program. Why is that, is there any solution Check the following code and the error msg. // Point3 class represents an x-y coordinate pair. using System; namespace CylinderTest { // Point3 class definition implicitly inherits from Object public class Point3 { // point coordinate int x, y; // default constructor public Point3() { // implicit call to Object constructor occurs here } // constructor public Point3( int xValue, int yValue ) { // implicit call to Object constructor occurs here X = xValue; Y = yValue; } // property X public int X { get { return x; } set { x = value; // no need for validation } } // end property X // property Y public int Y { get { return y; } set { y = value; // no need for validation } } // end property Y // return string representation of Point3 public override string ToString() { return "[" + X + ", " + Y + "]"; } } // end class Point3 } // Circle4 class that inherits from class Point3. using System; namespace CylinderTest { // Circle4 class definition inherits from Point3 public class Circle4 : Point3 { private double radius; // default constructor public Circle4() { // implicit call to Point constructor occurs here } // constructor public Circle4( int xValue, int yValue, double radiusValue ) : base( xValue, yValue ) { Radius = radiusValue; } // constructor public Circle4( int xValue, int yValue ) : base( xValue, yValue ) { } // property Radius public virtual double Radius { get { return radius; } set {
-
I have the following problem. Say, that I have a class called Point which has x,y and the distance from (0,0). There are two constructors, one take all three values and the other take only x ,y. I subclass this to a circle class which has its own radius and distance from (0,0). To initialize I want to use two different constructor that use either of the base class constructors. This causes an error in the program. Why is that, is there any solution Check the following code and the error msg. // Point3 class represents an x-y coordinate pair. using System; namespace CylinderTest { // Point3 class definition implicitly inherits from Object public class Point3 { // point coordinate int x, y; // default constructor public Point3() { // implicit call to Object constructor occurs here } // constructor public Point3( int xValue, int yValue ) { // implicit call to Object constructor occurs here X = xValue; Y = yValue; } // property X public int X { get { return x; } set { x = value; // no need for validation } } // end property X // property Y public int Y { get { return y; } set { y = value; // no need for validation } } // end property Y // return string representation of Point3 public override string ToString() { return "[" + X + ", " + Y + "]"; } } // end class Point3 } // Circle4 class that inherits from class Point3. using System; namespace CylinderTest { // Circle4 class definition inherits from Point3 public class Circle4 : Point3 { private double radius; // default constructor public Circle4() { // implicit call to Point constructor occurs here } // constructor public Circle4( int xValue, int yValue, double radiusValue ) : base( xValue, yValue ) { Radius = radiusValue; } // constructor public Circle4( int xValue, int yValue ) : base( xValue, yValue ) { } // property Radius public virtual double Radius { get { return radius; } set {
-
I have the following problem. Say, that I have a class called Point which has x,y and the distance from (0,0). There are two constructors, one take all three values and the other take only x ,y. I subclass this to a circle class which has its own radius and distance from (0,0). To initialize I want to use two different constructor that use either of the base class constructors. This causes an error in the program. Why is that, is there any solution Check the following code and the error msg. // Point3 class represents an x-y coordinate pair. using System; namespace CylinderTest { // Point3 class definition implicitly inherits from Object public class Point3 { // point coordinate int x, y; // default constructor public Point3() { // implicit call to Object constructor occurs here } // constructor public Point3( int xValue, int yValue ) { // implicit call to Object constructor occurs here X = xValue; Y = yValue; } // property X public int X { get { return x; } set { x = value; // no need for validation } } // end property X // property Y public int Y { get { return y; } set { y = value; // no need for validation } } // end property Y // return string representation of Point3 public override string ToString() { return "[" + X + ", " + Y + "]"; } } // end class Point3 } // Circle4 class that inherits from class Point3. using System; namespace CylinderTest { // Circle4 class definition inherits from Point3 public class Circle4 : Point3 { private double radius; // default constructor public Circle4() { // implicit call to Point constructor occurs here } // constructor public Circle4( int xValue, int yValue, double radiusValue ) : base( xValue, yValue ) { Radius = radiusValue; } // constructor public Circle4( int xValue, int yValue ) : base( xValue, yValue ) { } // property Radius public virtual double Radius { get { return radius; } set {
miftha wrote:
// default constructor public Cylinder() { // implicit call to Circle4 constructor occurs here }
Oh no it doesnt!!
// default constructor public Circle4():base()// explicit call to Point constructor required { }
-- modified at 3:35 Monday 20th February, 2006 -
I have the following problem. Say, that I have a class called Point which has x,y and the distance from (0,0). There are two constructors, one take all three values and the other take only x ,y. I subclass this to a circle class which has its own radius and distance from (0,0). To initialize I want to use two different constructor that use either of the base class constructors. This causes an error in the program. Why is that, is there any solution Check the following code and the error msg. // Point3 class represents an x-y coordinate pair. using System; namespace CylinderTest { // Point3 class definition implicitly inherits from Object public class Point3 { // point coordinate int x, y; // default constructor public Point3() { // implicit call to Object constructor occurs here } // constructor public Point3( int xValue, int yValue ) { // implicit call to Object constructor occurs here X = xValue; Y = yValue; } // property X public int X { get { return x; } set { x = value; // no need for validation } } // end property X // property Y public int Y { get { return y; } set { y = value; // no need for validation } } // end property Y // return string representation of Point3 public override string ToString() { return "[" + X + ", " + Y + "]"; } } // end class Point3 } // Circle4 class that inherits from class Point3. using System; namespace CylinderTest { // Circle4 class definition inherits from Point3 public class Circle4 : Point3 { private double radius; // default constructor public Circle4() { // implicit call to Point constructor occurs here } // constructor public Circle4( int xValue, int yValue, double radiusValue ) : base( xValue, yValue ) { Radius = radiusValue; } // constructor public Circle4( int xValue, int yValue ) : base( xValue, yValue ) { } // property Radius public virtual double Radius { get { return radius; } set {