Page 1 of 2

Sorting the code && Storing the files.

Posted: Sat Oct 03, 2015 6:14 pm
by AronE.
When I look at someone's code, I get this feeling that I won't ever find an answers for my questions - how should I sort my code? And also, how should I know whats the most efficient way to store the files I make? I bought some books, read them, but didn't find anything about this topic...

I can't exactly specify what answers Im looking for... So I will break this into one question : when you write a program, what do you think of while doing it?
Like for example : ,,Well, this is the functioning of some GUI component, I will store it in a different file, so that it will be read by the CPU only when its used, and will just stay there, if its not.", ,,Store this here, this one there, because its more efficient." or ,,I will make a new method, just because it is good for the processor..." etc...

I just don't know how to store my files and sort my code... When I look at someone's code I can clearly see that they are sorting it, while mine is just some methods, some variables, some lines who are being thrown and that's it... And I most likely make it in one or two or three files which are in one folder, while other programmers store their files in different folders, each folder for a different thing.

Re: Sorting the code && Storing the files.

Posted: Sun Oct 04, 2015 3:10 pm
by dandymcgee
The answer to this question varies wildly depending on many different factors. Primarily: programming language, type of project, and size of project.
I will give you three drastically different examples to give you an idea of what I mean. Then explain what they have in common.

To start, I need to address an obvious misconception you have about compiled programs.
AronE. wrote:Well, this is the functioning of some GUI component, I will store it in a different file, so that it will be read by the CPU only when its used, and will just stay there, if its not.", ,,Store this here, this one there, because its more efficient." or ,,I will make a new method, just because it is good for the processor..." etc...
Your file structure does not affect the performance of a compiled program. It *can* affect the time it takes to compile the project, but for most small projects there's probably not much point worrying about that. If you really care, you can go read more about compile-time optimization for your specific environment. When you compile a program, all of your code is essentially mushed together into one big binary file; the .exe. Obviously what happens is a bit more complex than "mushed together", but the important thing to know is that files are organized for the benefit of the developer, not the benefit of the computer.

First, let's consider a typical Windows workstation. If you don't use Windows, translate the names and ideas into whatever your OS uses.

You typically have a home directory, on Windows that will be e.g. "C:\Users\Dan\", on Linux it's "\home\dan\", etc. In essence, this is your "root" directory or project directory, if you will.

In this folder you typically have:

Code: Select all

/user/dan
    /Documents
    /Downloads
    /Music
    /Pictures
    /Videos
Videos go in the "Videos" folder, pictures in "Pictures", music, and downloads in their respective folders. Everything else goes in "Documents" under which you can make further sub-directories at your discretion. The idea is to logically categorize things in a way that makes them easy to find. To give every type of file an obvious, dedicated home.

Now, let's consider a web project. We'll use HTML, CSS, and JavaScript. Whether or not you consider web development "programming" is irrelevant. While specifics change between languages and environments, many of the underlying goals are shared.

In the root folder, you will find HTML files:

Code: Select all

/html
    /css
        default.css - Contains majority of the stylesheet info for the site
        blog.css    - Blog-specific styles
    /img
        banner.png  - Website banner used by all pages
        dandy.png   - "About Me" picture
    /lib
        jquery.js   - The JQuery library (use for fancy blog stuff)
    /scripts
        blog.js     - Custom blog script, depends on JQuery lib, used by blog.html
    index.html      - Introductory page, requires CSS
    blog.html       - Simple blog, depends on jquery library and a few custom JavaScript routines and CSS
    about.html      - About Me page, has a picture of me [dandy.png], requires CSS
    links.html      - Links to my friends' websites, requires CSS
This structure is fairly straightforward. You can name the folders whatever you want, but it's typically something along these lines. There is no mandatory standard, just be consistent. To key to organization in any project is always to be consistent.

Why are index.html and about.html different files? Because they are different pages of the web site. Why is blog.css separated from default.css? Because it only applies to one page and it's silly to include it everywhere. The more stuff you jam into one file, the harder it becomes to remember where each logical piece of the project resides.

Lastly, let's consider a C++ project. Specifically, and object-oriented organizational approach. As I alluded to above, different languages will have different organizational units.

You're making a simple object-oriented Pong game. Two paddles, a ball, some power-ups, some way to get user input, and some way to render output to the screen. Your organizational structure might look something like:

Code: Select all

/user/Dan/Documents/Projects/SuperPong
    /engine
        GamePad.cpp     - Logic for game pads (e.g. XBox controller)
        GamePad.h
        Keyboard.cpp    - Logic for keyboard input
        Keyboard.h
        Render.cpp      - Logic for rendering game to screen
        Render.h
    /game
        Paddle.cpp      - Paddle object
        Paddle.h
        Ball.cpp        - Ball object
        Ball.h
        PowerUp.cpp     - PowerUp object
        PowerUp.h
    Main.cpp            - Entry point, create window and start game
The header (.h) files contain class declarations to be #include'd in other file. The code (.cpp) files contain the actual definitions and logic for the subsystem or object contained therein. The folder structure, again, can be whatever you want. I chose to separate the "engine stuff" from the "game stuff". Sometimes people will make a "headers" folder and put all of their headers there. Sometimes it's all in one big folder.

Hopefully though, you are starting to get an idea of not only why folder structure is important, but what sorts of things are organizational units. A "paddle" and a "power-up" are two completely different organizational units. So we separate them into different files. Both are part of the Pong game, which is why they're both in this project directory, but that doesn't mean they should be in the same file. Likewise, just because a ball interacts with a paddle, doesn't mean they are part of the same organizational unit.

If you'd like to provide more details about your environment, your language choice, and your project, then we can talk more specifically about how you might apply these ideas to your own code.

As always, if you have any questions please post them here and I'll do my best to answer them.

Re: Sorting the code && Storing the files.

Posted: Tue Oct 13, 2015 3:29 pm
by AronE.
What I got from here : storing the files doesn't matter for the computer, it doesn't have to be in a certain, specific way, it's all to prevent confusion when programming.
You didn't say anything about sorting the code, however I think i have some basic idea now, I've watched couple of videos and I hope I got it right.

Questions :
1.You mentioned directories, when the project is on my PC, I can write for example ,,D:\projects\Project1".. or whatever, without any problem... But what if I put the project in an installer and tell my friend to test it? Then the user will most likely install the program in some other folder, or let's say disk C, the program won't work, because if he puts it in ,,C:\Program Files" the program won't recognise it..

Or to put that in short:
I set a directory in my program to : ,,D:\projects\Project1"
He puts the program in ,,C:\Program files". The program gets confused.


How to prevent that?

2.How to make exe files that run the whole thing?

3.This is sort of off-topic, but it still concerns me and it really bugs me.
How loops work?

All I know is this :
1. In a for loop, we set a variable, a condition and update expression.
2. It reads the code inside,
3. then goes back and checks if the condition is met, if it's not, it keeps reading the code inside the loop until the condition is met.

Image

But how exactly do they work(if there's something i don't know)? Why people use them so much? And HOW do they use them, not only in games but also in other projects? I keep asking myself these sort of questions when I see it in a code, like for example I watched ,,Java reading from text files" tutorial and some guy (forgot his channel name) used a while loop. I tried it out, the code didn't run without the loop.

A while loop looks like an if statement and a for loop had a baby, is that correct?

P.S. : I'm not working on a project, I'm afraid to start one, because I don't have any ideas of what project to make, I don't know what to do even if I have an idea of what to make, I don't have a team, I know HTML,CSS and Java only(HTML and CSS arent programming languages, but... They can be mixed with a programming languages, so they can be quite usfeul.). So my knowledge goes only this far and I'm planning to learn couple of more languages in the future, but first I want to be completely sure that I get the basics, before rushing into a second language.

Re: Sorting the code && Storing the files.

Posted: Tue Oct 13, 2015 8:59 pm
by AaronGlazer
AronE. wrote:You mentioned directories, when the project is on my PC, I can write for example ,,D:\projects\Project1".. or whatever, without any problem... But what if I put the project in an installer and tell my friend to test it? Then the user will most likely install the program in some other folder, or let's say disk C, the program won't work, because if he puts it in ,,C:\Program Files" the program won't recognise it..

Or to put that in short:
I set a directory in my program to : ,,D:\projects\Project1"
He puts the program in ,,C:\Program files". The program gets confused.


How to prevent that?.
You would want to link to files using the local path. In other words, you'd put the folder "resources" in the same directory as the executable and access it via the path relative to the executable.

The directory would look like this:

Code: Select all

/user/AronE/game
    game.exe
    resources/
        images/
            logo.png
            player.png
        music/
            track01.ogg
            track02.ogg
And the way you'd access these files, rather than "/user/AronE/game/resources/images/logo.png" you'd just use the relative path, or "resources/images/logo.png"

Re: Sorting the code && Storing the files.

Posted: Tue Oct 13, 2015 9:44 pm
by dandymcgee
AronE. wrote:1.You mentioned directories, when the project is on my PC, I can write for example ,,D:\projects\Project1".. or whatever, without any problem... But what if I put the project in an installer and tell my friend to test it? Then the user will most likely install the program in some other folder, or let's say disk C, the program won't work, because if he puts it in ,,C:\Program Files" the program won't recognise it..

Or to put that in short:
I set a directory in my program to : ,,D:\projects\Project1"
He puts the program in ,,C:\Program files". The program gets confused.


How to prevent that?
AaronGlazer got this exactly right. There are two primary types of paths: relative and absolute. Absolute paths always start at the root directory (in your example, drive letter D:\). Relative paths are relative to some agreed upon path, such as your project directory; or, in the case of a terminal (e.g. Bash on Linux or Command Prompt on Windows), the current directory; or, in the case of a running executable, the working directory.

Usually, the working directory is defined as the path in which the executable (.exe) file resides when it is executed. However, this isn't always the case. For instance, a Windows Service's working path is always C:\Windows\System32\ by default (because it is being executed inside of a SVCHOST, which resides in the System32 folder). Also, your program can change it's own working directory if it pleases.
AronE. wrote:2.How to make exe files that run the whole thing?
It sounds like you need to read up on how a compiler works.

At the very basic level, when you compile your code you're performing some or all of the following steps:
  1. Lexical analysis
  2. Syntax analysis
  3. Semantic analysis
  4. Code optimization
  5. Code generation (e.g. ".hpp/.cpp" -> ".obj")
  6. Linking (your .obj files are linked with any library you've statically linked)
  7. Executable is created, and any local assets (images, config files, etc.) or dynamically linked dependencies (e.g. ".dll") are copied to output directory
Image

In modern programming environments, all of this is usually hidden away under a single button (e.g. "Build Project"). That said, there are many reasons why one might customize the build process to meet project-specific requirements.
AronE. wrote:3.This is sort of off-topic, but it still concerns me and it really bugs me.
.. how exactly do [loops] work(if there's something i don't know)? Why people use them so much? And HOW do they use them, not only in games but also in other projects? I keep asking myself these sort of questions when I see it in a code, like for example I watched ,,Java reading from text files" tutorial and some guy (forgot his channel name) used a while loop. I tried it out, the code didn't run without the loop.
A loop is used any time you have to repeat the same task more than once.

Consider this: You're working on a Space Invaders clone and you're implementing projectiles. When the player ship fires a bullet into the sky, you need to check if it hits any of the 55 invader ships.

We could easily (though rather tediously) accomplish this using only conditional statements like follows:
Invader *invaders[55];

//Determine which Invader the player's bullet has hit (if any)
Invader *ProjectileHitTest(Projectile *playerBullet)
{
    if (playerBullet.Intersects(invaders[0])) {
        return invaders[0];
    } else if (playerBullet.Intersects(invaders[1])) {
        return invaders[1];
    } else if
        //TODO: Implement hit test for 2 - 53
    } else if (playerBullet.Intersects(invaders[54])) {
        return invaders[54];
    } else if (playerBullet.Intersects(invaders[55])) {
        return invaders[55];
    } else {
        return NULL;
    }
}
Now consider this same logic written using a basic for loop:
Invader *invaders[55];

//Determine which Invader the player's bullet has hit (if any)
Invader *ProjectileHitTest(Projectile *playerBullet)
{
    for(int j = 0; j < 55; ++j) {
        if(playerBullet.Intersects(invaders[j])) {
            return invaders[j];
        }
    }
}
Note: I used 'j' instead of 'i' because [ i ] happens to be a BBCode for italic font on the forums.

We just saved ~100 lines of code and get basically the same performance. In addition, our code is easier to write, easier to comprehend, easier to maintain, and our .exe is smaller (less disk space, less memory).
AronE. wrote:A while loop looks like an if statement and a for loop had a baby, is that correct?
Yeah.. pretty much. Though the for loop also has a conditional (the second statement, "j < 55;" in the example above). You could read the above for loop as "Set j to 1, increment j by 1 while j is less than 55."
There's also a do { //stuff } while (condition); loop. You can replace any of these three loops with any of the other two and get identical behavior. Choosing the "right" loop is merely a matter of preference and context.

Generally:
If your condition is binary in nature (e.g. "bool running = true;" in a game state machine), use a while loop.
If your condition is binary and you need to ensure the loop always runs at least once, use a do while loop.
If your condition is based on an incrementing value, use a for loop.

Again, there isn't always a concrete reason to choose one over the other. Personally, I use for loops 90% of the time, while loops the 9% where they make sense, and do while loops for that 1% chance that I need to force a loop to run once and don't feel like doing the first task before a while loop.

Re: Sorting the code && Storing the files.

Posted: Thu Oct 15, 2015 1:17 pm
by AronE.
I know it's off-topic(again), but I wanted to ask...

When I started programming I imagined myself behind the keyboard, coding on two monitors and making programs of almost all kind, being able to make a chatroom, you know, something like this, or maybe even a whole browser...

At the beginning, I knew how the basics would look like (Because I started learning C 3 years ago, when I was 13 and got scared of loops, they were very difficult for me to understand back then.)
So pretty much I had an idea how the basics would look like.

Then, I wanted to know how to make a GUI, I looked it up and for less than a week I learned JavaFX.
After that, I found out that making a chat room needs some knowledge in Java Networking.
And now, I found out that in order to make a functional progressbar I need some knowledge in threads or something like it.(Everyone uses it in order to make a working progressbar, so I guess there is no other way. I believe this is Java multi-threading, but I'm not sure...)
Are there more things I should be aware of?

Re: Sorting the code && Storing the files.

Posted: Thu Oct 15, 2015 6:01 pm
by dandymcgee
AronE. wrote:I know it's off-topic(again), but I wanted to ask...

When I started programming I imagined myself behind the keyboard, coding on two monitors and making programs of almost all kind, being able to make a chatroom, you know, something like this, or maybe even a whole browser...

At the beginning, I knew how the basics would look like (Because I started learning C 3 years ago, when I was 13 and got scared of loops, they were very difficult for me to understand back then.)
So pretty much I had an idea how the basics would look like.

Then, I wanted to know how to make a GUI, I looked it up and for less than a week I learned JavaFX.
After that, I found out that making a chat room needs some knowledge in Java Networking.
And now, I found out that in order to make a functional progressbar I need some knowledge in threads or something like it.(Everyone uses it in order to make a working progressbar, so I guess there is no other way. I believe this is Java multi-threading, but I'm not sure...)
Are there more things I should be aware of?
There are always going to be things you don't know. That's the nature of learning any complex skill set. I've been programming for around 10 years, and I'm still learning new things every week. Nobody can really say which thing you should learn next; that depends on your personal goals. It's also not possible for me (or anyone else) to list everything you might need to know, especially without knowing your project requirements.

I've said it before and I'll say it again: the best way to learn how to program, is to start programming. You seem to have a decent grasp on basic concepts, so all you need to do is set some personal goals and strive to achieve them. Don't worry about getting stuck or making mistakes, that's when you learn the most.

Re: Sorting the code && Storing the files.

Posted: Fri Oct 16, 2015 4:14 pm
by AronE.
The thing is, I do program. Sometimes I'm really confident and when I watch tutorials I mute the video and try to learn only by looking at the code... (Maybe I'm lazy or something, idk) And when I re-watched the ,,java reading files" again along with the explanation I got it almost in an instant.

Loops are difficult to me simply because I've almost never used them, today was the first time I actually needed a while loop for something... When someone explains the basics (talking about for and while loops) they only use the counting from 0 to 10 as an explanation which is limited... They never say anything else about them, that's why I'm left without any clue how they are actually useful, aside from counting. For the last 2-3 days I've been thinking about loops, practiced some of the things I got in mind and I'm starting to get how they actually are useful, but I certainly will need some more time in order to completely understand them.

I need to think of a non-game project... Do you guys have some ideas, what were your first projects or you just all started with game-making?

Re: Sorting the code && Storing the files.

Posted: Fri Oct 16, 2015 4:57 pm
by dandymcgee
AronE. wrote:The thing is, I do program. Sometimes I'm really confident and when I watch tutorials I mute the video and try to learn only by looking at the code... (Maybe I'm lazy or something, idk) And when I re-watched the ,,java reading files" again along with the explanation I got it almost in an instant.

Loops are difficult to me simply because I've almost never used them, today was the first time I actually needed a while loop for something... When someone explains the basics (talking about for and while loops) they only use the counting from 0 to 10 as an explanation which is limited... They never say anything else about them, that's why I'm left without any clue how they are actually useful, aside from counting. For the last 2-3 days I've been thinking about loops, practiced some of the things I got in mind and I'm starting to get how they actually are useful, but I certainly will need some more time in order to completely understand them.

I need to think of a non-game project... Do you guys have some ideas, what were your first projects or you just all started with game-making?
My first real projects were bots for Diablo II that I wrote in AutoIt scripting language, along with various Windows pranks (like moving the mouse cursor to a random location every 30 seconds).

Actually, some artifacts have survived the ages. You can find a few of my first ever programs on my old FreeWebs site http://www.freewebs.com/dandymcgee/myfiles.htm. I set that up in 2006, not long after I got my own computer and was allowed to use the dial-up unsupervised. To be honest, I'm absolutely amazed FreeWebs is still hosting it for free after 10 years. "Fool in the Rain - beep song" is rather entertaining. When I first started learning to play piano I would write scripts that played songs with the PC's internal speaker. Basically the 2006 equivalent of minecraft note blocks, haha.

Re: Sorting the code && Storing the files.

Posted: Sat Oct 17, 2015 7:42 am
by AronE.
dandymcgee wrote: My first real projects were bots for Diablo II that I wrote in AutoIt scripting language, along with various Windows pranks (like moving the mouse cursor to a random location every 30 seconds).

Actually, some artifacts have survived the ages. You can find a few of my first ever programs on my old FreeWebs site http://www.freewebs.com/dandymcgee/myfiles.htm. I set that up in 2006, not long after I got my own computer and was allowed to use the dial-up unsupervised. To be honest, I'm absolutely amazed FreeWebs is still hosting it for free after 10 years. "Fool in the Rain - beep song" is rather entertaining. When I first started learning to play piano I would write scripts that played songs with the PC's internal speaker. Basically the 2006 equivalent of minecraft note blocks, haha.
I like them, very simple. ;) :) I've heard that the only difference between scripting and programming languages is that programming languages have to compile, while scripting languages don't need compiling. However, I don't know how to make my cursor freak out, or put an icon in the Windows bar below. Is it because you use a scripting language or just I never knew how to do it?

Btw, how old are you?

Re: Sorting the code && Storing the files.

Posted: Sat Oct 17, 2015 2:23 pm
by dandymcgee
AronE. wrote:Is it because you use a scripting language or just I never knew how to do it?
You can do it in any language, but AutoIt makes it really easy. (That's it's purpsose: automating Windows tasks. Hence it's name.)
AronE. wrote:Btw, how old are you?
I'm 24, but I wrote that stuff when I was 14.

Just questions.

Posted: Sun Oct 18, 2015 9:45 am
by AronE.
This is already off-topic, so I changed the subject to "just questions"...

Can someone tell me how and why is database so important? MySQL is a language that creates, changes and pretty much controls database, but how is it important? The only thing I know it's useful for is making accounts(registration) and using them(log-in), atleast that's what I was told. But also, someone told me that MySQL can be used to store different files, is that true and if yes, how is it useful?

Edit: I know my questions are already just stupid, but...
What is a framework? The only explanation I got is this : already made and tested code that's being reused by different programmers.

Edit2: If I'm not mistaken, only GPU and Sound drivers are needed for a PC to work properly, but how to know what are the correct ones for my PC after reinstalling the OS? I know the GPU manufacturer is needed when installing a driver, but what next? Just searching it on google, downloading it and keep clicking ,,next"?

P.S.: Don't get me wrong, I do make researches on these questions, I just either can't find a proper explanation or can't understand it. English is not my primary language. Besides I have alot of questions I couldn't find answers to before.

Re: Just questions.

Posted: Sun Oct 18, 2015 2:17 pm
by dandymcgee
AronE. wrote:Can someone tell me how and why is database so important? MySQL is a language that creates, changes and pretty much controls database, but how is it important? The only thing I know it's useful for is making accounts(registration) and using them(log-in), atleast that's what I was told. But also, someone told me that MySQL can be used to store different files, is that true and if yes, how is it useful?
MySQL is not a language. MySQL is a database management system (DBMS). There are many different brands of database management systems: MySQL, Oracle DB, Microsoft SQL Server (MSSQL), etc. Each is essentially a full package of tools for creating and managing databases.

The language that most of them use is called SQL (Structured Query Language). At least, this is true for relational databases (there are other sorts of databases that have recently become popular in the web development world, called "document-oriented databases" but nevermind those for now).

The primary purpose of a relational database is to store large amounts of data in a structured way. Generally, you define specific types of data as well as how those types are related to one another. Later, you can query the data based on those relationships and gather interesting information very quickly that would have been very difficult without the power of SQL.

For example, you can define two types (called "tables" in a relational database) called Student and Class. Then you can create a property in the Student type which points to a class that the student is taking (these properties are called "columns" in the context of a database). Then you can populate the database with 400 classes and 10,000 students. Next, you create a lookup table (called "StudentClasses") which defines which students are taking which classes. Let's assume there's 400 classes * 20 students per class = 8,000 such relationships.

Now, let's say you want to know which classes "Dandy McGee" is taking. You could:
a) Look through all 8,000 records for "Dandy McGee" yourself using a loop and reading from a file or something rudimentary like that.
b) Run the following query on our database [SELECT className FROM StudentClasses WHERE studentName = 'Dandy McGee']

This is an really simple example, but there are *much* more complicated queries you could write which would be extremely difficult or inefficient to perform on a simple text file storage system. The database is highly optimized to perform such queries ridiculously fast on extremely large amounts of data. For instance, we have one database at work that has over 10 billion rows of data in it and I can write a query that will find a specific record in under one second.
AronE. wrote:Edit: I know my questions are already just stupid, but...
What is a framework? The only explanation I got is this : already made and tested code that's being reused by different programmers.
Yup. Code reuse.
AronE. wrote:Edit2: If I'm not mistaken, only GPU and Sound drivers are needed for a PC to work properly, but how to know what are the correct ones for my PC after reinstalling the OS? I know the GPU manufacturer is needed when installing a driver, but what next? Just searching it on google, downloading it and keep clicking ,,next"?
Well, your computer uses *many* drivers (hundreds), but often the default ones will work just fine. In the case of video drivers (GPU), that is sometimes true, sometimes not. Either way, the latest drivers will pretty much always be significantly better than Microsoft's generic video driver.

I don't recommend you download drivers from an arbitrary website; that's extremely dangerous (drivers run at the system level with the highest privileges). Find out who the manufacturer of your video card is and go to their website.
If you have an NVidia card, download drivers from NVidia's site: http://www.nvidia.com/Download/index.aspx?lang=en-us
If you have an AMD (e.g. Radeon) card, download from AMD's site: http://support.amd.com/en-us/download
If you have Intel: https://downloadcenter.intel.com/

Those are the three main GPU manufacturers, but if your GPU was made by someone else just google the company name and find the official driver website.

All of those sites will tell you which OS is supported by each driver. You always want to download the latest driver that is compatible with your OS.

As for installing, I *do not* recommend just hitting "Next" a bunch of times without reading the prompts. Many people do this, then complain when nothing on their computer works right. Also, a bunch of installers have embedded junk software in them and if you just click "Next" without unchecking the boxes you will end up with a ton of crap on your computer you don't want. Personally, I *always* click "Custom Install" vs. "Express Install" if that's an option, which will let you opt out of all of the useless junk.

Just questions.

Posted: Tue Oct 27, 2015 9:03 am
by AronE.
Can someone explain the keywords static and final used in methods and variables?

P.S. : (Optional) I'm trying to figure out also how public, private, protected work for both methods and variables, but I think i can figure this one out on my own.They are not hard to be explained/understood theoretically, but It's hard to practice. I never knew why, or how EXACTLY they work.
For example : private variable cannot be accessed directly from another class, but actually can be accessed in a way and is used in getters and setters.

Re: Just questions.

Posted: Tue Oct 27, 2015 10:26 am
by superLED
AronE. wrote:Can someone explain the keywords static and final used in methods and variables?

P.S. : (Optional) I'm trying to figure out also how public, private, protected work for both methods and variables, but I think i can figure this one out on my own.They are not hard to be explained/understood theoretically, but It's hard to practice. I never knew why, or how EXACTLY they work.
For example : private variable cannot be accessed directly from another class, but actually can be accessed in a way and is used in getters and setters.
If you have static methods etc. in a class, you just have to include the class (#include "class.h") and you can start using its methods without making an object of that class. You use the methods by writing for example "ChatBox::addText('Hello');"

Public: Everything can access and change these variables. If you include the class with these public variables, you can just grab them an do whatever with them. Try to avoid this. Only methods should be public (the ones that the outside world should be able to access)
Private: Only this class can access the variables. Nothing outside can look at it or change it (unless you are doing it through a public method).
Protected: I think this one is for variables that only this class and classes that are "polymorphing" it can use (or how I should word it).

PS: Some of the information could be wrong, and would be glad if someone could correct me so I could get my shit straight too.