Classes and Constructors

Started by
4 comments, last by ArsDiaboli 10 years, 4 months ago

I recently started learning C# for use within the Unity game engine. I've been trying to follow a couple of tutorials on Classes and Constructors, but I simply can't see to understand it. I would appreciate it if anyone could explain Classes and Constructors and what they are useful for. I am just learning so try to keep it as simple as possible.

Thanks in advance!

Advertisement

In object-oriented programming, a class is a construct that is used to define a distinct type. The class is instantiated into instances of itself – referred to as class instances, class objects, instance objects or simply objects. A class defines constituent members that enable its instances to have state and behavior.[1] Data field members (member variables orinstance variables) enable a class instance to maintain state. Other kinds of members, especially methods, enable the behavior of class instances. Classes therefore define thetype of their instances.[2] _____ Wikipedia

In other words a class describes a set variables and a set of functions which are exclusively only part of this class you would define. In your case it is the basic of your language!!

The constructer or ctor is a very special memberfunction. the ctor has the job to initialize your class before the instance of your class exist. to make a comparison. your Ctor is the bootloader of your class. That is the reason why every class need to have a ctor.

In object-oriented programming, a class is a construct that is used to define a distinct type. The class is instantiated into instances of itself – referred to as class instances, class objects, instance objects or simply objects. A class defines constituent members that enable its instances to have state and behavior.[1] Data field members (member variables orinstance variables) enable a class instance to maintain state. Other kinds of members, especially methods, enable the behavior of class instances. Classes therefore define thetype of their instances.[2] _____ Wikipedia

In other words a class describes a set variables and a set of functions which are exclusively only part of this class you would define. In your case it is the basic of your language!!

The constructer or ctor is a very special memberfunction. the ctor has the job to initialize your class before the instance of your class exist. to make a comparison. your Ctor is the bootloader of your class. That is the reason why every class need to have a ctor.

Alright, I understand about the constructor. So are classes mostly used for organizational purposes? I mean, couldn't you fit your code all into one class? Albeit it would most likely be messy code. Can any real-world examples of when classes would be useful be provided?

An object oriented program may be viewed as a collection of interacting objects, as opposed to the conventional model, in which a program is seen as a list of tasks (subroutines) to perform. In OOP, each object is capable of receiving messages, processing data, and sending messages to other objects. Each object can be viewed as an independent "machine" with a distinct role or responsibility. Actions (or "methods") on these objects are closely associated with the object. For example, OOP data structures tend to "carry their own operators around with them" (or at least "inherit" them from a similar object or class) — except when they must be serialized. ----- Wikipedia

Some programmer do it this way but that is not the idea behind a class. classes try to implement the concept of OOP.

but yes sometimes it is only that what you said...

An object oriented program may be viewed as a collection of interacting objects, as opposed to the conventional model, in which a program is seen as a list of tasks (subroutines) to perform. In OOP, each object is capable of receiving messages, processing data, and sending messages to other objects. Each object can be viewed as an independent "machine" with a distinct role or responsibility. Actions (or "methods") on these objects are closely associated with the object. For example, OOP data structures tend to "carry their own operators around with them" (or at least "inherit" them from a similar object or class) — except when they must be serialized. ----- Wikipedia

Some programmer do it this way but that is not the idea behind a class. classes try to implement the concept of OOP.

but yes sometimes it is only that what you said...

Alright. Thanks for your help. I'll go take a look at some examples of classes and hopefully i'll have a better understanding of it.

Just look at classes as objects. For example, you may have a sword in your game. That sword is an object (also called an "instance of a class"). You define the features of that object in your class, its attributes, and what it can do. The class is the "blueprint", the object is the actual use of that blueprint. Like, you could have a mold to make swords (this is your class), and you can have an actual functional sword (an object, an instance of that class, or blueprint).

You can think up some attributes for the sword:

- Length

- Width

- Sharpness

- Weight

And some actions it may perform

- Dulls

- Sharpen

- Cut

So, when you call the "dull" method in your Sword object, it performs some action that does *something*, in this case, it may decrease the value of the "sharpness" attribute.

That's the gist of it, but make sure to read some more. As for the constructor, it's a special method that is called when a class is instantiated (or, an object is created). For example, each and every time you "create" (instance) a sword, the constructor is automatically called and it sets the default values of the sword object. For example, every sword object starts with a value of 10 in its "sharpness" attribute. Constructors are commonly used to initialize the values of an object.

This topic is closed to new replies.

Advertisement