|
-
March 29th, 2010, 01:28 PM
#1
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!
-
March 29th, 2010, 01:49 PM
#2
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?
-
March 29th, 2010, 01:53 PM
#3
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.
-
March 29th, 2010, 01:55 PM
#4
Re: Quick question about assigning things in constructors
 Originally Posted by BigEd781
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.
-
March 29th, 2010, 01:57 PM
#5
Re: Quick question about assigning things in constructors
 Originally Posted by Arjay
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!
-
March 29th, 2010, 02:19 PM
#6
Re: Quick question about assigning things in constructors
 Originally Posted by Vandorin
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".
-
March 29th, 2010, 02:35 PM
#7
Re: Quick question about assigning things in constructors
 Originally Posted by BigEd781
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!
-
March 30th, 2010, 01:14 AM
#8
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|