WELCOME TO SPOTS13 C++ TUTORIAL
SECTION 1.1Well guys, I've decided to start this thread with the intention of making a tutorial guideline for everyone, in which we are going to review C++ from the basis and going over and over, I'm gonna be explaining in detail every step we are doing right here, so everyone is able to understand it in a very easy way, also I'm gonna give some common mistakes and some useful tips for programming in C++ and basically in any other language.
So lets Get Started
OUR FIRST PROGRAM
print a code line
well in this first exercise the idea is to get familiarized with the C++ code and show some important features within the language.
#include <iostream>
// library which allows the programmer to print data on screen
int main()
// "main" function runs the program
{
std::cout<<"Welcome to your first programe in C++\n";
//"std::cout" is used to show a message on screen, the one that goes inside the ""
return 0;
// indicates that the program was terminated successfully
}
// ends "main" function
this will show
Welcome to your first programe in C++
IMPORTANT:
please note that every instruction ends with a semicolon ;.
"//" are used to mark comments and everything inside them will not affect the program the only purpose of them is the clarify some points of our program for other users who might read it .
every code line which starts with a # state ans the "#include<iostream>" is because that lines are processed before the program is compiled, as it is a raw program you must show the processor which headlines it uses, in this case the library "<iostream>" allows the processor to use the file of the headline that includes the input/output stream, this file must be included for every program which receives data imputed by keyboard or that shows data on screen.
"\n" is a escape sequence that place the mouse in a new line in the screen, just a like the "ENTER" key does. (I might make a char including every escape sequence available).
the parenthesis just after the "main" function indicates that it is a construction space which is called function. C++ programs are usually made of
one or more functions or classes (which I'm showing you later) there must be just one "main" function because the program is being executed when the main function appears even doe if main is not the first function declared.
the reason the the "main" function is being declared as an "int" is because it allow us to return a integer value in this case it allows the program to return the value 0 when it is executed properly.
the blue words on the code are called as keyword, they are reserved words for C++ and they are used for an specific task.
the open key parenthesis "{" must begin the body of every function and the close key "}" ends the body of it.
the cout is used to print a characters on screen, the notation std:: before it specifies that we are using a name that we have taken from #include<iostrem> and that belongs to the "name sapace" the "name spaces" are a more advanced characteristic of C++ and I'll be explaining it later on
"//" are used to mark comments and everything inside them will not affect the program the only purpose of them is the clarify some points of our program for other users who might read it .
every code line which starts with a # state ans the "#include<iostream>" is because that lines are processed before the program is compiled, as it is a raw program you must show the processor which headlines it uses, in this case the library "<iostream>" allows the processor to use the file of the headline that includes the input/output stream, this file must be included for every program which receives data imputed by keyboard or that shows data on screen.
"\n" is a escape sequence that place the mouse in a new line in the screen, just a like the "ENTER" key does. (I might make a char including every escape sequence available).
the parenthesis just after the "main" function indicates that it is a construction space which is called function. C++ programs are usually made of
one or more functions or classes (which I'm showing you later) there must be just one "main" function because the program is being executed when the main function appears even doe if main is not the first function declared.
the reason the the "main" function is being declared as an "int" is because it allow us to return a integer value in this case it allows the program to return the value 0 when it is executed properly.
the blue words on the code are called as keyword, they are reserved words for C++ and they are used for an specific task.
the open key parenthesis "{" must begin the body of every function and the close key "}" ends the body of it.
the cout is used to print a characters on screen, the notation std:: before it specifies that we are using a name that we have taken from #include<iostrem> and that belongs to the "name sapace" the "name spaces" are a more advanced characteristic of C++ and I'll be explaining it later on
Section 1.2
well assuming you have read the last section and that you have done your first program already we are to introduce the input stream operator which is std::cin, it uses the operator >> for the extraction; we are going to make a program that receives two integer numbers by keyboard (which are inputted by the user) it calculates the addition between both numbers and prints the result using the std::cout that we have already seen before
Note* I leave spaces in between lines for better readability of the source code and the comments if you find it annoying please let me know.#include <iostream> // allows the program to do operations with input and output data
//function that initialize the program
int main()
{
//variable declarations****
int a;
// this variable handles the first number
int b;
// this variable handles the second number
int add;
// this variable handles the sum of a & b
std::cout<<"please enter the first number to be added:\n";
//asks the data to the user
std::cin>>a;
// program reads the inputted value and assigns it to "a" variable
std::cout<<"please enter the second number:\n";
// asks data to the user
std::cin>>b;
// program reads the inputted value and assigns it to "b" variable
add = a + b;
// assigns the result of the operation "a+b" to the variable "add"
std::cout<<"the result is: "<< add<< std::endl;
// prints the value that is stored in "add" variable and "std::endl" ends the line
retun 0;
// indicates that the program ended correctly
}
//function "main" ending, (ends the program)
THIS WILL SHOW
please enter the first number to be added:
"a" value (let's say you entered 100)
please enter the second number
"b" value (let's say you entered 23)
the result is: 123
** the values are just an example
IMPORTANT:
int a; , int b; , int add; they all are declaration, the identifiers "a","b","add" are variable names, a variable is a store place within the memory of you pc, in which you can store a value for using it while the program is running. These declarations determine that variables a,b,add are data categorized as int type, that means these variables contain integer values (every number between (-infinity & infinity with out using decimals). Every variable must be declared with a name and a type before using it on the program, you can declare in the same line every single variable that shares the same type, just as shown
int a, b, add;
this reduce legibility to our program and prevents you from commenting on each single variable, REMEMBER if you are to declare variables this way you must separate them by colons (,), I recommend using a space after the colon is set but that is not necessary is just for it to look nicer
you can declare or assign value to your variables whenever you like in your program, just remember to declare them before using them
the assignation done in the program "add = a+b;" is an assignation that you do as a programmer and is not shown on screen after the compile is done, while you are programming you have to take care of which type of assignation do you need for the variables you use, user made (asking for an input value entered by keyboard)or programmer made just like the one before
Section 1.3
Now that we have learned how to show data in screen using std::cout and asking for data with std::cin we can’t go further without checking the charts of operators relation and equality, this is because we are introducing in this section to the instruction if, if’s are made of relations between data, just like in real life, if you eat you meal then you are going to have dessert this is the basic idea of the if instruction
Its structure is:
This is basic if construction later on, we might see some more complex instructions, like nested if’s with “else†instruction and so on.if(condition)
{ // this open key is a must a is the homonymous to the “thenâ€
// here you tell the program what to do is the condition is true
} // this close key ends the if instruction
In the next example we are moving on the “using†declarations this safes the need of using “std::†before each instruction like we did in the programs before, once we use “using†deckarations instead of use “std::cin†you simply use the cin or cout, endl or w/e instruction you have declared ; from now on we are going to use “using declarations†for each program.
In the next program we are going to use if’s to compare 2 numbers inputted by keyboard and show in screen which of them is greater, smaller, equal, not equal…. and so on.
on the following we have the charts that are needed for this program and the ones we are going to be creating in the future.
and
so let's start
#include <iostream>
// allows the program to use input and output streams
Using std::cout; //the program will use cout
Using std::cin; // the program will use cin
Using std::endl; // the program will use endl
//the function main initialize the program
Int main ()
{
Int a; // first number to be compared
Int b; // second number to be compared
Cout<< “write two integer numbers to be compared: â€; // ask the data to the user
Cin >> a>>b; // this allows the program to read and assign the value of the user’s numbers and store in on a & b in that order.
If( a==b ){ // opens the true state of the condition
Cout <<a<<â€==†<<b<<endl;
// is the value stored in a was equal to the value of b print both values separated by “==â€
} // close the true state of the condition
If( a!=b ){
Cout <<a<<â€!=†<<b<<endl;
// is the value stored in a was different to the value of b print both values separated by “!=â€
}
If( a<b ){
Cout<<a<<â€<†<<b<<endl;
// is the value stored in a was greater than the value of b print both values separated by “<â€
}
If( a>b ){
Cout <<a<<â€>†<<b<<endl;
// is the value stored in a was smaller than the value of b print both values separated by “>â€
}
If( a<=b ){
Cout <<a<<â€<=†<<b<<endl;
// is the value stored in a was smaller or equal to the value of b print both values separated by “<=â€
}
If( a>=b ){
Cout <<a<<â€>=†<<b<<endl;
// is the value stored in a was greater or equal to the value of b print both values separated by “>=â€
}
Return 0; // indicates that the program ended successfully
} // this ends the main function and the program
This Will Show
ORwrite two integer numbers to be compared: “a†“b†(let’s say 3 7)
3 != 7
3 < 7
3 <= 7
ORWrite two integer numbers number to be compared “a†“b†(let’s say 20 10)
20 != 10
20 > 10
20 >= 10
Write two integer numbers to be compared “a†“b†(let’s say 100 100)
100 == 100
100 <= 100
100 >= 100
IMPORTANT:
We are using open key and close key on the if’s that we are using, if we use a basic if is not necessary to use them, but is preferable, but later on maybe on the chapter 3 we’re going to see compound if’s with more than one instruction and with those it is a must to use the key’s operators
the operations hierarchy is just as in normal math including parenthesis and everything
END OF SECTION 1
SECTION 2
SECTION 2.1
well as many of you might know while checking these c++ is an OOP which means object oriented programming language and in this section we are introducing you guys for the real stuff, and I mean classes, objects, functions and more after this section I'm willing to give you guys some terminology some exercises and some auto-evaluation exercises with their answers so have fun a let's begin
first I'm going to explain what classes are with a simple analogy let's suppose that you drive a car and for it to accelerate you got to press the accelerator
but before you could do that there must be an engineering who designed the accelerating system which the accelerator hides, this allows the non-experienced users to be able to drive just by knowing the function of the pedal and not the process involved.
well now let's take our car example to the c++, for doing a task in an application there is required a function (just like the "main" we use in the last chapter) the function describes the mechanisms that are in charge of doing his tasks , the function hides the user the complex tasks that it does, just as the accelerator pedal, in C++ we create a unit application called class to store a function just as the car design stores the pedal's engineering we are going to see more about this topic in the future so don't worry.
let's begin with a short example of what a class is, let's GradingSheet is a class that represents a grading sheet that a random teacher uses to maintain his students grades, and the function main creates an object , GradingSheet. The function main uses the object and it's function to welcome the teacher to his grading sheet program, this is a simple explanation of the topic later on we are going to see more sophisticated ways for improving and to achieve a better software if you like prefer to say it that way.
#include <iostream>
using std::cout;
using std::endl;
//Definition of the class GradingSheet
class GradingSheet
{
Public:
//function that shows a welcome message to the user of GradingSheet
void showmessage()
{
cout<<" Welcome to your Grading Sheet"<<endl;
}// end of function "showmessage"
};//end of the class GradingSheet
// function main initializes the program
int main()
{
GradingSheet myGradingSheet; // creates an object "GradingSheet"
myGradingSheet.showmessage(); //recall the function "showmessage" from the object "GradingSheet"
return 0; // indicates that the program ended properly
} // end of main
this will show
Welcome to your Grading Sheet
IMPORTANT:
before the function main is able to create an object of the class GradingSheet, we must show to the compiler which are member functions and which data members belongs to that class, this is known as "class definition" . the "class definition" GradingSheet contains a function called showmessage which shows a message on screen. Remeber that a class is like the construction plans; that is why we need to create an object of the class GradingSheet and recall it's fucntion showmessage for allowing the program to execute that line ans show the welcome message on screen.
the class definition starts with the keyword "class" and next goes the name of the class in this case "GradingSheet" a user defined class goes with the first letter in caps
the Body of the class is in between of to open and close keys "{" "}" and the definition of the Body ends with a semicolon ";"
Remember the function "main" is always auto recalled by the program major functions are not recalled automatically, as we are going to see he have to explicit call the function "showmessage" in an explicit way to indicate it to do his task
the keyword public is an access identifier, in the program we define the function "showmessage" as "public" to indicate the obvious, it has public access, this means any other function in the program (just as main) can recall it, and also the member function of other classes (if there are any), later on we are going to see the "private" identifier
Coming soon:
In the mean time I'm going to post a compiler and an IDE for you guys to use while practicing
Good luck play safe
Have fun and practice a lot,
for you
---
Spots13.





















