Classes and Structs

Post Reply
User avatar
OwnZ joO
Posts: 1197
Joined: Sun Dec 09, 2007 4:46 pm

Classes and Structs

Post by OwnZ joO »

Ok, as far as you need to know right now classes and structs are the same(they are not, but at this point their differences aren't really important).
I will be talking about classes most of the time because as you will learn later on they are a little more flexible.

A class is basically a blueprint on how to create objects memory of that type.

In object oriented programming, you look at something and decide how to represent it in code. For example a halo map has different sections, the header, the index, ect... So you create a class for a map accordingly, having it contain a header and index.

There are 3 main things that will make up your classes:
  • variables
  • constructors
  • methods
Later on you might get into having events, but at this level you most likely won't.

The declaration of a class looks like this:

[code]
public class ClassName
{
// variables, constructors, and methods here
}
[/code]


Field Variables:
In your classes you will need them to have variables in order to store information about the class.
To create a variable for the class you will need to declare it inside of the class.
I am going to create a class to represent a coordinate on a Cartesian plane as my example.
A point on a Cartesian plane has an x and a y, and they are both numbers, so we will create the class as follows:


[code]
public class CartPoint
{
public int x;
public int y;
}
[/code]


Right now all we have is a class with 2 variables and no constructors or methods.
Before I explain how to create a constructor I should probably explain what it is used for.
Constructors are special methods used to instantiate(set for the first time) the variables of the class.
The constructor is called when you use the new operator, for example:
[code]CartPoint point = new CartPoint();[/code]

Constructors:

The above code will call the constructor of CartPoint and return a CartPoint to store in the variable point.
Notice how CartPoint doesn't have a constructor? Well the .Net framework automatically gives you one if you don't declare one. It sets all the variables to their default value, so point.x and point.y would both be 0 in the above example.

Now to learn how to create a constructor:
[code]
public class CartPoint
{
public int x;
public int y;

public CartPoint(int x, int y)
{
this.x = x;
this.y = y;
}
}
[/code]


Notice that it just has a return type, and make sure you declare it public or else you won't be able to use it.
So the constructor in this example sets the variables x and y with parameters that are sent into the constructor.
I'll give an example of how to use this:

[code]
CartPoint point = new CartPoint(2, 10);
[/code]


In the above example, point.x is 2 and point.y is 10.

Now, there are a something wrong with the above example. The variables x and y are both public, meaning that they can be accessed by something besides a CartPoint object. What's wrong with that? you might be asking. Well, how do you know when one of them has changed, how do you control and make sure that the data is valid? For example if you needed the data to be between 0 and 300, how would you ensure that nothing sets the data to anything besides that?

The solution is to use accessors. Accessors are used to provide access to a variable, but still be able to have control and know when it's being accessed. Also it allows you to change how the data is stored without changing how it is accessed if you do decide to change the storage mechanism.
I'll create an example of an accessor for x. Notice how I will change x to be a private variable:

[code]
int x; // no access modifier(public or private) before defaults to private
public int X
{
get{ return x; }
set
{
// notify that x is changed if you want
// check to see that the value is allowable
if(value >= 0 && value <= 300)
{
this.x = value;
}
}
}
[/code]


You could also leave out the set part of the accessor to make it a read only variable, this means that you can not do anything to directly change the variable, you can only see what it's value is.
Also, just to set the record straight on private variables, private makes it so only objects of the same type can directly access the variable, in this example, that means one CartPoint object can directly access the private variables of another without having to use the accessor like objects of other types.

Methods:
Now to be productive you're going to need methods to act on the objects of CartPoint.
A good example for CartPoint would be a method that finds the distance to another CartPoint.
The formula for distance is the square root of the difference of a's squared + the difference of b's squared
In order to get the distance between 2 methods we'll need to create a method that takes a CartPoint as a parameter and returns a double(because chances are with the square root it will not be an integer)

[code]
public class CartPoint
{
// fields(s) left out
// constructor(s) left out
public double Distance(CartPoint that)
{
int aDifference = this.x - that.x;
int bDifference = this.y - that.y;
// to square a number multiply it by itself
int sumOfDifferencesSquared = (aDifference * aDifference) + (bDifference * bDifference);
// now get the square root
return Math.Sqrt(sumOfDifferencesSquared); // this returns a double
// square root the
}
}
[/code]
User avatar
OwnZ joO
Posts: 1197
Joined: Sun Dec 09, 2007 4:46 pm

Re: Classes and Structs

Post by OwnZ joO »

Remember you can have any return type that you choose for methods in your class, you can even return the type of your class. As an example I will create a method that returns a new CartPoint shifted by shiftX and shiftY:

[code]
public CartPoint Shift(int shiftX, int shiftY)
{
return new CartPoint(this.x + shiftX, this.y + shiftY);
}
[/code]


Keep in mind that you could have negative values for shiftX and shiftY, so you don't need to make another method that subtracts instead of adds to x and y. Another way we can accomplish basically the same task is to just modify the x and y values, this method would have no return type, so it will be a void.

[code]
public void Shift(int shiftX, int shiftY)
{
x += shiftX; // shorthand for x = x + shiftX;
y += shiftY; // shorthand for y = y + shiftY;
}
[/code]


Now, what's the difference between these two methods? The way you would use them to get the same result.
For the first example you would need to do something like this:

[code]
CartPoint point = new CartPoint(2, 3);
point = point.Shift(0, -1);
[/code]


Now the variable point would have a new CartPoint object in it with a value of (2, 2).
As for the void method it would look like this:

[code]
CartPoint point = new CartPoint(2, 3);
point.Shift(0, -1);
[/code]


In this example, the variable point would have the SAME CartPoint object in it, with a value of (2, 2).
You might be asking who cares whether it's the same object or not, and the answer is language designers with PHD's.
I really don't know the difference, but from what my teacher has told me in school, the proper way to do it according to language designers who see the big picture would be to use the first method and create a new object, but in some cases that's not plausible. To me it really doesn't matter, but I just thought I would pass on what I have learned and let everyone decide for themselves.

Alright, now hopefully you should understand how to declare a class and the three basic building blocks of classes. I'll attach a Command Line Project that has the CartPoint class and will show some examples of it. I changed the class a little bit to be more complete. Hopefully I didn't leave out anything important, I didn't really talk about structs much, but you could change CartPoint to a struct by replacing class with struct in the declaration of CartPoint. It would not change the behavior of CartPoint objects at all in the scope of this tutorial. I stopped mid way through the tutorial because I ran out of room and South Park came on, so I lost my train of thought. If anything I said confuses you because it's poorly worded, let me know and I'll work on it.
Attachments
ClassesAndStructs.zip
ClassesAndStructsExample
(25.66 KiB) Downloaded 370 times
User avatar
XZodia
Staff
Posts: 2208
Joined: Sun Dec 09, 2007 2:09 pm
Location: UK
Contact:

Re: Classes and Structs

Post by XZodia »

You missed operators, there rarely used but still an important part which few people know how to implement and is therefore a useful thing to add to this.
Image
JacksonCougar wrote:I find you usually have great ideas.
JacksonCougar wrote:Ah fuck. Why must you always be right? Why.
User avatar
OwnZ joO
Posts: 1197
Joined: Sun Dec 09, 2007 4:46 pm

Re: Classes and Structs

Post by OwnZ joO »

I have never had a situation where I needed to use operators yet, so I don't see many people absolutely needing to know at this point, but I see your point. I'll make one for the + operator later when I go over overloading regular methods and inheritance. But yeah I'll probably add that in, thanks.
Post Reply