Welcome, Guest
You last visited:


You are not connected. Please login or register

Getting Started in C++

View previous topic View next topic Go down  Message [Page 1 of 1]

1 Getting Started in C++ on Sat Nov 06, 2010 7:19 am

Blacksun

avatar
GFX Team
GFX Team
Credits
IMOK aka braindeadflow(Mike) -For writing the tutorial
AIOlab/Unigaming Communities -For being awesome places to chill and to learn something and for always being useful
Microshaft -for making MSVS 2010




Forward
Welcome, if you are viewing this you are most likely interested in learning the language of C++, this could be for several reasons from anything to coding for your PC to linux, systems and beyond. From this point I expect you have a compiler that you at least somewhat understand how to operate. Personally I use Microsoft Visual Studio 2010 Professional, but you can use whatever you like, remember to find an up to date compiler so you have up to date libraries, binaries etc. Now onto the coding, remember this is geared towards the newbies who know nothing, or almost nothing. To write your code I suggest using Notepad++

Covering basic terms and Important S*** in our source
What is a source...? gtfo!

Comments/Commenting
In this section I will cover something that is similar in all languages(except HTML), commenting. Commenting with comments allows you to mark up your work so you know when you re-visit your source what a certain function or block of code does. It is also extremely useful if you plan to make your binary open source so other programmers can see what certain code does and how it operates.

First let us cover a one line comment
Code:

//This is a simple one line comment



That is how you make a simple one line comment using // to start the comment. You can use this to mark your functions, loops, globals, etc.

Multi-line comment
Code:

/*comment
fffffff
fffffff
*/



Multi lined comments are useful when you have comments that are more than one lines such as if you wish to include GNU or copyright information if you decide to release your source(src)

Header Files/Includes
Header files are .h files that your compiler will use to hold vital(usually) information such as special encryption, using kernals, or WiFi connection data etc. For the PC your most basic Headers that you will have are iostream, stream, and of course stdio. So below I will display how your properly use them. These MUST go at the top of your file before any codeblocks(a comment marking them Headers/Includes before them is OKAY) I will not explain what these are as of yet, you should know if you have a little understanding of compiling code. ALL header and includes start with the pound(#) symbol.

Yes there are more than these three below but these are the basic and are in almost EVERY PC program, it also depends what you need to include depending what you need.

basic use of headers and includes with commenting
Code:

/* Includes
And Headers */
#include
#include
#include



Using longer functions/Paths more efficiently
This is quite easy to do and makes your job as a programmer MUCH easier. Defining data will make it easier later on so you don't have to type out long unsigned values or data strings over, and over again. This is usually included right after your includes(skip a line or two)

Defining a function as something else
Code:

#Define SEX missyisaladyinthestreetandapornstarinbed
/*Makes it so instead of keep having to...
*Use that function name you can
*refer to it as
*SEX
*/



Globals and Pre-defined Variables
Globals and Variables are common short snippets of code that you will include such as variables(gVar for example) that would be used when doing math, calculations and using numbers. It would also be a smart idea to comment your variables. Often used with functions such as pause so it is able to be used in multiple code blocks and functions as pause();

Example of using a global and varis
Code:

//Globals
void pause();

//Variables
gVar1; //variable #1 truly equals 0 in this case
gVar2=2; //when called upon it will display the value 2



Basics of functions, using them and defining them
Knowing how to properly use functions and what they do is extremely important as you will soon find out when you code. A function can hold any data into registers, loops(such as (foo==0) or if blah then else or just plain if..) You can also increment by using ++ or decrement using -- but I will explain functions and what can be done with them at a later point. ALL programs have a main function! Some people will choose not to properly use the main function but it tells to console what to do first. If you don't choose to use main then at least put something there such as cignbin etc.

the basic skeleton of a function

Code:

int main();
{
//CODEBLOCK HERE
}



This is a very basic function, that ALL programs should include. Now let me explain what each part of that function does.

A look at the main function
Code:

int main();



int defines what type of function it is(integer etc). Main is the name of our first function and (); is used with EVERY function. The semi colon( is use to end a line such as when you use endl; (will be explained more later on)

Explaining curly brackets
Code:

{
//CODEBLOCK HERE
}


The curly brackets are used to tell the assembler where your code is, the { tells where the start of your code block is(where you include your loop, math, print outs etc) the } tells the console that is the end of that particular function, though that is not all you have to do to end a function!

The must have return
Before you completely end your code you MUST return your data, this goal can be simply achieved by using either the value 0, or 1. More often than not you will use the value 0. This tells the console to wash its hands with soap because our work here is done. And here is what one looks like.

Code:

return 0;



Once again we must have the semi colon( to end the line and in most cases 0 is the value to return.

What a small function looks like with a return(0)

Code:

int main();
{
//CODEBLOCK HERE

return 0;

}



The return goes before the final bracket of a particular function.

Printing data(text) to our console screen
In more cases than not you will use printf depending what your doing(also depends on what language as some have their own variants) In this case I will explain how you use cout, which stands for console output, and in this case you are going to be outputting text. Make sure you understand the above sections so you understand what the function is doing. This will go in your functions codeblock(//CODEBLOCK HERE)

First let me explain what you are going to do below. We already know that cout outputs text and displays it to our screen but lets explain the rest.

endline

Code:

//crap here endl;



endl stands for end line(who couldn't guess that?) and of course you have to end it with a semi colon. We should already know what the pause(); global does(it pauses-skips before display another string of text)

displaying our text

Code:

//This holds the string we want to display
<<"What we want this sexy biatch to spit out"<<


You need the quotations wrapped around whatever you want the console to say then displayed around that you MUST have << and <<

Final text output with pauses

Code:

int txtdisplay();
{
cout << "Hello and Welcome to this simple Program!" << endl;
pause();
cout << "Developed by YOU" << endl;
pause();
cout << "Lets was our hands" << endl;

return 0; //return

}



Using Characters and Sums included in a function
For this section of this wonderful tutorial you must have understood the above sections as for this we will be using a new function that plays around with displaying numbers, doing extremely simple addition and displaying the values.

Telling our proggy the character and values we want to use
I already explained how to define variables such as gVar2=2; above, so I will not re-explain that here as it would be a good idea to keep your code neat and clean so if you plan to use something like the variable 2 multiple times you should use gVar2=2; if you plan to display it multiple times. Now if you want to display a character(letter) than this is how.

Defining a Character for use

Code:

//FNC above etc
char ('R');
//rest of function



That small sample of code in our function tells whatever function you are currently using to refer to the character(later will be used as ch) as the letter R.

Defining a int sum and doing math with it
This is very simple and if you know any basic math it should be a breeze! I will show how to define the value and then adding it.

Code:

int sum = 1337; //defines that value as 1337

sum = number + 7331 //takes the above value and adds 7331 to it



Example function of taking numbers and characters and displaying them(number variables are pre-defined)

Code:

int missyisaladyinthestreetandapornstarinbed();
{
char ('R'); //defines the character as R

cout << "The value of gVar1 is: "<< gVar1 << endl; //displays gVar1(it equals 0)
cout << "The value of gVar2 is: "<< gVar2 << endl; //displays the value of gVar2
cout << "The Character in ch is: "<< ch << endl; //displays R

int sum = 1337; //defines the value(sum) as 1337

sum = number + 7331 //takes the sum(#) and adds 7331 to it

//Then display that value

cout <<"The sum of our magic number is: " << sum << endl;
cin.get(); //keeps the window from closing

return 0; //make a succesful return

}



The FINAL code
Congratulations, if you followed this entire tutorial this is somewhat the idea of what your code should look like. Now you should just save it as something.cpp and go ahead and compile it with your compiler. Sorry if there's any mistakes, I only had time in the morning to write this so it might be sloppy.

Final code

Code:

//Includes/Headers
#include
#include
#include

#define fake blahjustanexample
using namespace std; //using namespace(std is NOT a sickness, it means standard)

//Globals
void pause();

//Variables
gVar1; //variable #1 truly equals 0 in this case
gVar2=2; //contains the value 2

//Our Main function

//int defines function type
//main is our FIRST function
//cout is console output(displays definded text)
//endl ends the line
//{ and } are used to define the code blcok

int main();
{
cout << "Hello and Welcome to this simple Program!" << endl; //displays text to console
pause(); //does a pause
cout << "Developed by YOU" << endl;
pause();
cout << "Hello World" << endl;

return 0; //returns

}

// Just called it math, really just displays values/chars
int missyisaladyinthestreetandapornstarinbed(); //you can rename this if you wish
{
char ('R'); //defines the character as R

cout << "The value of gVar1 is: "<< gVar1 << endl; //displays the gVar1 (it will show zero)
cout << "The value of gVar2 is: "<< gVar2 << endl; //displays the gVar1 (it will show two or whatever you definded it as)
cout << "The Character in ch is: "<< ch << endl; //Displays R

int sum = 1337; //defines the value

sum = number + 7331 //takes the above value and adds to it

//Then display that value

cout <<"The sum of our magic number is: " << sum << endl;
cin.get(); //keeps the window from closing

return 0; //return

}


/*comment
Tutorial by imok(mike)
Please vist....
AIOlab.com
Unigaming.net
*/



Closing
Thank YOU so much for reading this, hopefully it helps at least one person in some way. I do understand this is fairly simple for some people but I really believe for all the new coders and programmers this will prove useful as I know this is something that is not even close to easy to learn!

The Different types of 'If' Statements and how to use them
First off what is an if statement you ask? An if statement is a method for your user(or whoever is using the program) to select a type of data, such as a number to then receive certain info if they selected certain data. A very simple example in actual words would be... "If you are reading this=Than you want to learn C++" They are extremely important in further processes of programming with C++. These may be familiar as they are used in PHP, C , C#(sharp), java etc(again not HTML) Let us begin.

Our Operators
An Operator is simply A way to compare data such as if something is equal '=' or greater than etc, this is fairly simple and most readers will already know these from fourth grade math, but in case there is some you do not know I will provide examples of all the basic ones. The following examples are all TRUE.

greater than

Code:

1337 > 0001



less than

Code:

0001 < 1337



greater than or equal

Code:

1337 >= 1337



less than or equal

Code:

9 <= 10



equal to

Code:

9999 == 9999



not equal to

Code:

1 != 0



A Basic if statements
I will explain what a basic if statements looks like in C++ syntax, it is fairly simple and I will provide real examples later on, on how it can be used in a real program. A basic if statement is the easiest to understand while some other types of 'if' statements might be a little tougher to grasp. For this example it will be simple to get and we will use an example people can easily grasp as of now that is important. After I explain each type of 'if' I will use all the types in actual code to see what the full biz natch looks like. First off if you are comparing any type of value you must remember to keep it like so.

Code:

if(val1 , val2)
else....



If...

Code:

if ( 0 < 1337 ) //less than statement
cout<<"The value zero is much less than 1337";



This code will display the selected message if the value 0 is LESS than 1337 and is true, than the message "The value zero is much less than 1337" will be displayed on the console screen by using cout, which we learned in Part One.

If Else Statement

Code:

if ( 1337 == 1337 ) {
cout<<"The value 1337 is of course equal to 1337";
}
else {
cout<<"The value 1337 does not equal 1337";
}



This is a statement that when the value 1337 is compared and if they equal than display the message, else then display another message. A simple way to explain this in actual human words would be "If you are a boy" 'Then you are a male' else you are a girl.

A simple source of a simple program showing you how to use different 'if' statements
In this commented example I will show you how the main function compares your age and displays a message. This is fairly simple to achieve and can also be thought of with words, but if statements when you first start off will most have to do with math as it is the easiest thing to understand as math is universal.

Complete Example of If statements

Code:

//includes
#include
//using namespace standard
using namespace std;

//Our main function
int main();
{
int age; //Our variable that we will be using

cout<<"What is your age?: "; //displays txt to console
cin>> age; // The input is put in age
cin.ignore();
if ( age < 30 ) { // If statement
cout<<"You are pretty young!\n"; //Nice job your young and alive ... prints to console
}
else if ( age >= 31 ) { //If else
cout<<"You are getting old"; //returns with an txt output
}
else {
cout<<"I do not know how old you are"; // Else statement, if one of the values is not there then it assumes this output
}
cin.get(); //if we ignore than we gotta get it back!
}



That was a common simple example of telling you how old you are based on the age you enter, now if you didn't enter an age that matches one of the if, or if else statements then the console display output should tell you it does not know how old you are, because it doesn't have anything to compare your age too.

Explanation of Loops
If statements are important and so are loops. A loop allows a programmer to repeat a codeblock however many times they wish for whatever reason they want. A loop is the method you want to use as a programmer if you want data to keep being repeated(such as you want a constant error to appear 3 times) then that will be a loop as by default it should appear once.

Most Basic Loops
The Basic loop is the one you will use the most.

Code:

#include

using namespace std; // So the program can see cout and endl

int main()
{
// The loop goes while x < 10, and x increases by one every loop
for ( int x = 0; x < 10; x++ ) {
//displays the value of x until the conditional 10 is reached
cout<< x < }
cin.get();
}



While-While Loops
A While-While Loop is useful if you want to add to a Boolean value etc, they are extremely simple to understand and to properly use in your coding structure. This is most likely the simplest type of loop for any programmer to use. In

While While Loop Example SRC

Code:

//includes
#include

//needed for the system to properly use cout and endl;
using namespace std;

//main function
int main()
{
int x = 1; // Declares this variable(X) as the value 1

while ( x < 10 ) { // While x is less than 10
cout<< x < x++; // ++ will keep adding to the value 1 so it will go 2,3,4.. and so on until the conditional (value=10) is meet.
}
cin.get();
}



In this short block of code we have our main function which has the variable x which is defined as the value 1. It then keeps adding the value to itself until the conditional is meet, so it will keep adding to the variable until it is meet, until the value is equal to 10.

Do While Loops
Simple really, do something, while something is happening. It will do something then while a (condition) is declared. I will once again provide a simple example for the programmers to use. First off this is the simple layout of the Do While syntax, this has been called other things.

Code:

do {
} while ( condition ); //such as 9 == 9



Code:

//includes
#include

//used for cout and endl
using namespace std;

int main() //function main
{
int x; //x integer

x = 0; //declares x the value 0
do {
//even though its false it still prints Hello World
cout<<"Hello, world!\n";
} while ( x != 0 );
cin.get();
}



Using a loop to add a custom value
Instead of using x(integer etc)++. ++ By default will keep adding the value 1 to whatever the integer was declared as, well if you dont want to increment by the value one you can easily change it. So in shortbread it would be like so.

Code:

//increments the variable x by the value 2
x+2



And the long term full display of incrementing by a different value other than the value 1(+, the default)

Code:

//includes
#include

//needed for the system to properly use cout and endl;
using namespace std;

//main function
int main()
{
int x = 1; // Declares this variable(X) as the value 1

while ( x < 1000 ) { // While x is less than 10
cout<< x < x+10; // ++ will keep adding to the value 10 so it will go 10,20,30,40. and so on until the conditional (value=10) is meet.
}
cin.get();
}



More on functions
Since this is something I sorta forgot in Part One I will cover more on different types of functions now. The reason is that these functions will prove more useful in performing loops such as using the mult function etc.

Code:

int mult ( int x, int y );



That defines the function as mult(take a guess) and uses the variables x and y as an example. Please like always remember to include the int to define the function along with the semi colon, if you forget that it will think you are writing the actual function such as you would in a loop. This is a common error most coders will make as if you do not have it this will no longer really be a global or variable it would be a real function which you are defining.

Code:

//Includes
#include
#include //if you wish to get fancy

using namespace std; //namespace for cout and endl

//Global
int mult ( int x, int y );

//Main Function
int main()
{
int x;
int y;

cout<<"Enter two cents: ";
cin>> x >> y;
cin.ignore();
cout<<"The product of your two cents: "<< mult ( x, y ) <<"\n";
cin.get();
}

int mult ( int x, int y )
{
return x * y;
}






_________________
[You must be registered and logged in to see this image.]
View user profile

2 Re: Getting Started in C++ on Sat Nov 06, 2010 10:56 am

Wow.

You know C++??


_________________


[You must be registered and logged in to see this image.]

[You must be registered and logged in to see this image.]

Sigs by Blacksun, thanks a lot!!

[You must be registered and logged in to see this image.]

View user profile http://pspcommunity.forumotion.com

3 Re: Getting Started in C++ on Sat Nov 06, 2010 10:57 am

Blacksun

avatar
GFX Team
GFX Team
neeeeeeee


_________________
[You must be registered and logged in to see this image.]
View user profile

4 Re: Getting Started in C++ on Sun Nov 07, 2010 12:08 pm

Lol nice tut anyways. Awesome


_________________


[You must be registered and logged in to see this image.]

[You must be registered and logged in to see this image.]

Sigs by Blacksun, thanks a lot!!

[You must be registered and logged in to see this image.]

View user profile http://pspcommunity.forumotion.com

5 Re: Getting Started in C++ on Tue Nov 09, 2010 9:35 am

sunsucker

avatar
Top Member
Top Member
nice tut

View user profile

Sponsored content


View previous topic View next topic Back to top  Message [Page 1 of 1]

Permissions in this forum:
You cannot reply to topics in this forum