Advertisement Jump to content
Sign in to follow this  
Shadowraix

Classes and Constructors

This topic is 1872 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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!

Share this post


Link to post
Share on other sites
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 instancesclass objectsinstance 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.

Share this post


Link to post
Share on other sites

 

 

 

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 instancesclass objectsinstance 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?

Share this post


Link to post
Share on other sites

 

 

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...

Edited by exOfde

Share this post


Link to post
Share on other sites

 

 

 

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. 

Share this post


Link to post
Share on other sites

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.

Edited by ArsDiaboli

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!