Understanding Classes

I'M reading C# in 21 days and I'M starting to get just a little confused. I have very VERY basic C++ programming skills such as if/else statements, loops, and very simple functions. When I first starting getting into the section about C# classed, I thought it was simple enough. But now it is just starting to look like a more complicated way of storing values in variables. Can someone pleae help me to better understand what is going on? Thanks.

[code]// A structure with two data members
//==================================

struct point
{
public int x;
public int y;
}

class PointApp
{
public static void Main()
{
point starting = new point();
point ending = new point();

starting.x = 1;
starting.y = 4;
ending.x = 10;
ending.y = 11;

System.Console.WriteLine("Point 1: ({0},{1})", starting.x, starting.y);
System.Console.WriteLine("Point 2: ({0},{1})", ending.x, ending.y);
}
}[/code]

Comments

  • What you are running into is something that everyone moving from singleton programming to Object Oriented Programming faces. In fact the first time I was faced with classing I thought the same exact thing - "what's the point of classes? they are just a collection of variables and what not - why can't I just have the variables alone?" - but they are actually extreemly usefull and time saving once you start getting in the habit of "classing" things.
    In fact the issue you are dealing with isn't a C# related issue - it's an OOP issue.

    What I hate about some of these books is that they give you simple examples of things like a "Point" in your case - but don't really show how usefull it really is - like they assume you will just know.

    Lets expand on the Point class:

    [code]
    public class Point
    {
    public double X { get; set; }
    public double Y { get; set; }

    public Point(double x, double y)
    {
    X = x;
    Y = y;
    }
    }
    [/code]

    right now it's just 2 doubles stored in some kind of wrapper, but it still has it's uses:

    - Passing a point into a function means you only pass one thing rather than two things

    Also - let's say you have a function that deals with X/Y coordinates - like some kind of math that rotates your given x, y coordinates a specified number of degrees?
    you could do something funky like passing the X and the Y as refrences so that you could change them in the function and then the calling code would end up with the differing values - but isn't this easier?

    [code]
    public Point RotatePoint(Point pt, double theta)
    {
    var x = Cos(theta) * pt.X - Sin(theta) * pt.Y;
    var y = Sin(theta) * pt.X + Cos(theta) * pt.Y;
    return new Point(x, y);
    }
    [/code]

    But the real power here would be to simply add this method to the actual class - then you can use this class anywhere in your current code or future projects without ever having to re-adapt it.

    [code]
    public class Point
    {
    public double X { get; set; }
    public double Y { get; set; }

    public Point(double x, double y)
    {
    X = x;
    Y = y;
    }

    public void Rotate(double theta)
    {
    var x = Cos(theta) * X - Sin(theta) * Y;
    var y = Sin(theta) * X + Cos(theta) * Y;
    }
    }

    [/code]

    Now you can rotate the hell out of points without ever blinking an eye - or having to re-look up the math online to do it.

    [code]
    var myPt = new Point(10, 10);
    myPt.Rotate(10);
    myPt.Rotate(-10);
    myPt.Rotate(1);
    [/code]

    or let's say you had an array of 1000 points - the loop to rotate them all 1 degree would be very simple:

    foreach (var pt in pointsArray)
    pt.Rotate(1);

    so if this was a graphics program and you had 1000 images that all needed to move around a single x,y location and your "Point" class was a part of your "Image" class then you would only need to MyImage.Location.Rotate(1);

    Actually in that method up there, you would need to convert from degrees to radians but whatever.

    Anyways - that is a very simple example and this reply is getting very long - but I hope that sheds some light on what classes are good for.

    ><//~Psightoplasm`~
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

In this Discussion