Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Using Multiple constructors in base and sub class

Using Multiple constructors in base and sub class

Scheduled Pinned Locked Moved C#
help
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    miftha
    wrote on last edited by
    #1

    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 {

    G J 3 Replies Last reply
    0
    • M miftha

      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 {

      G Offline
      G Offline
      Guffa
      wrote on last edited by
      #2

      Standard question #2: What error message do you get? Standard question #3: What does your code look like? --- b { font-weight: normal; }

      1 Reply Last reply
      0
      • M miftha

        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 {

        J Offline
        J Offline
        J4amieC
        wrote on last edited by
        #3

        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

        1 Reply Last reply
        0
        • M miftha

          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 {

          G Offline
          G Offline
          Guffa
          wrote on last edited by
          #4

          If you want two different constructors they have to have different parameters. What you have now is two constructors with the same parameters that do the same work, so I don't even see the reason for having both of them. --- b { font-weight: normal; }

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups