Click to See Complete Forum and Search --> : Quick question about assigning things in constructors
Vandorin
March 29th, 2010, 01:28 PM
I am basically creating a shape, and have two classes, "Shape" and "Point" Now I am declaring a position in point, a simple
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
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!
BigEd781
March 29th, 2010, 01:49 PM
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?
Arjay
March 29th, 2010, 01:53 PM
You need to start out slow - IMO, you have too much code written before understanding the basics.
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.
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.
Vandorin
March 29th, 2010, 01:55 PM
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.
Vandorin
March 29th, 2010, 01:57 PM
You need to start out slow - IMO, you have too much code written before understanding the basics.
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.
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. :D Thank you so much!
BigEd781
March 29th, 2010, 02:19 PM
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".
Vandorin
March 29th, 2010, 02:35 PM
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!
boudino
March 30th, 2010, 01:14 AM
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
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.
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.