CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Mar 2010
    Posts
    5

    Quick question about assigning things in constructors

    I am basically creating a shape, and have two classes, "Shape" and "Point" Now I am declaring a position in point, a simple
    Code:
    public class Point : Shape
        {
            //Instance Variables
            private double y;
            private double x;
    
    
            // Getters
            public double X
            {
                get { return x; }
                //set { x = value; }
            }
            
    
            public double Y
            {
                get { return y; }
                //set { y = value; }
            }
    
            // Methods
            public override void Move(double dx, double dy)
            {
                double a = X + dx;
                double b = Y + dy;
            }
    
            // Constructor
            public Point(double x0, double y0)
            {
                x0 = X;
                y0 = Y;
                Position = this.Position;
                
            }
    And my code for shape so far is
    Code:
     private Shape position;
    
            public Shape Position
            {
                get { return position; }
                set { position = value; }
            }
    
            public virtual void Move(double dx, double dy)
            {
                X = x + dx;
                Y = y + dy;
               
            
            }
            // Constructor
            public Shape(double x0, double y0)
            {
                
            }
    
            public Shape()
            {
            }
    I'm really confused on the two constructors, I am supposed to create a new point object, and assign it to position, but I'm confused on how I would do that. I don't even have a general idea, can someone help me? Did I do the point code wrong somehow?

    -Thanks!

  2. #2
    Join Date
    Jun 2008
    Posts
    2,477

    Re: Quick question about assigning things in constructors

    So, I am not sure why you have Point inheriting from Shape. A Point is not a Shape, it is a Point. The framework already gives you a Point class in the System.Drawing namespace, I suggest you use that.

    Also, a position is not a Shape, so I don't even understand that property at all. I am a bit confused regarding your question, can you elaborate a bit for us?

  3. #3
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Quick question about assigning things in constructors

    You need to start out slow - IMO, you have too much code written before understanding the basics.

    Code:
     
     
    public class Point
    {
            //Instance Variables
            private double _y;
            private double _x;
    
    
            // Properties
            public double X
            {
                get { return _x; }
                //set { _x = value; }
            }        
    
            public double Y
            {
                get { return _y; }
                //set { _y = value; }
            }
     
           // Constructor
            public Point(double x, double y)
            {
                _x = x;
                _y = y;
            }
    }
    Next, run the following code in a debugger and step through each line and look at the variables.
    Code:
    Point p = new Point( 10.2, 25.0 );
     
    double x = p.X;
    double y = p.Y;
    You should notice how the properties have been set in the constructor. Now look at your original constructor and notice the difference.

  4. #4
    Join Date
    Mar 2010
    Posts
    5

    Re: Quick question about assigning things in constructors

    Quote Originally Posted by BigEd781 View Post
    So, I am not sure why you have Point inheriting from Shape. A Point is not a Shape, it is a Point. The framework already gives you a Point class in the System.Drawing namespace, I suggest you use that.

    Also, a position is not a Shape, so I don't even understand that property at all. I am a bit confused regarding your question, can you elaborate a bit for us?
    point is supposed to extend shape.
    it has an x and y coordinate, overrides the move method (from shape) and Has a constructor that initializes the properties X and Y and sets the Position property to this.

    The part I am confused on is in shape, where the constructor creates a new object, and assigns it to position. Hope that makes things clearer.

  5. #5
    Join Date
    Mar 2010
    Posts
    5

    Re: Quick question about assigning things in constructors

    Quote Originally Posted by Arjay View Post
    You need to start out slow - IMO, you have too much code written before understanding the basics.

    Code:
     
     
    public class Point
    {
            //Instance Variables
            private double _y;
            private double _x;
    
    
            // Properties
            public double X
            {
                get { return _x; }
                //set { _x = value; }
            }        
    
            public double Y
            {
                get { return _y; }
                //set { _y = value; }
            }
     
           // Constructor
            public Point(double x, double y)
            {
                _x = x;
                _y = y;
            }
    }
    Next, run the following code in a debugger and step through each line and look at the variables.
    Code:
    Point p = new Point( 10.2, 25.0 );
     
    double x = p.X;
    double y = p.Y;
    You should notice how the properties have been set in the constructor. Now look at your original constructor and notice the difference.
    This was extremely helpful! I think you are right and that I was trying to rush a bit, thank you so much! I understand the part I was confused about now. Thank you so much!

  6. #6
    Join Date
    Jun 2008
    Posts
    2,477

    Re: Quick question about assigning things in constructors

    Quote Originally Posted by Vandorin View Post
    point is supposed to extend shape.
    it has an x and y coordinate, overrides the move method (from shape) and Has a constructor that initializes the properties X and Y and sets the Position property to this.

    The part I am confused on is in shape, where the constructor creates a new object, and assigns it to position. Hope that makes things clearer.
    When you are taught OOP, you are told that inheritance is a "is a" relationship. It is not a mechanism to simply pass on methods as used in your code. Point "is not" a shape, even though they share some common properties such as x and y coordinates. It really makes no logical sense. You should have a Point class, and then your Shape class would have a Point member to define its x and y location. This is called "composition".

  7. #7
    Join Date
    Mar 2010
    Posts
    5

    Re: Quick question about assigning things in constructors

    Quote Originally Posted by BigEd781 View Post
    When you are taught OOP, you are told that inheritance is a "is a" relationship. It is not a mechanism to simply pass on methods as used in your code. Point "is not" a shape, even though they share some common properties such as x and y coordinates. It really makes no logical sense. You should have a Point class, and then your Shape class would have a Point member to define its x and y location. This is called "composition".
    Yeah, I was rushing too much, and some of my code was messed up. I've got it now though, thanks to everyone for helping me!

  8. #8
    Join Date
    Mar 2004
    Location
    Prague, Czech Republic, EU
    Posts
    1,701

    Re: Quick question about assigning things in constructors

    As BigEd781 said, the inheritance between Point and Shape is strange, but if you believe you need it... Your constructor is wrong. You are assigning values of x,y fields to constructor's parameters, and also assigning Position to itself. I would write it
    Code:
            public Point(double x0, double y0) : base(x0, y0)
            {
                X = x0;
                Y = y0;
            }
    Also, you should look over value types (struct keyword), because they are often used for types like Point.
    • Make it run.
    • Make it right.
    • Make it fast.

    Don't hesitate to rate my post.

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