Learning C++
If you want a language thats powerful and quick, you may want to considering studying C++. It's getting more popular and has many uses.
This article will explain basic things about the language to help anyone starting out.
___________________
>Directives
#include and #define are examples of directives for the C++ preprocessor. When starting out, you only need to sorry about the #include directive. As you progress,
youll figure out uses for others.
>Header files
A header file is like a file that has built in functions and code. C++ has it's own header files you can use, and you can make your own when you're ready enough.
For file I/O ( reading and writing to files ) you could use <fstream> or <iomanip> . If you were taking input for a number and wanted to find its square root, you
would need <cmath> . Always put the header file inside brackets and right after your directive. Example #include <cstdlib> If youre using your own that you created,
save it in the same folder as your source file.
>Namespaces
If you've seen any example programs, you may have seen "using namespace std;". That tells whatever compiler you have to use the namespace of std in your program.
The "std" is the standard library of C++. Syntax is using namespace name_of_namespace;
int main() is how you declare the beginning of your program. Everything between the curly braces { and } after int main() is what makes up your program.
It's optional to put void inside the parentheses.
Example:
int main()
{
code of your program
}
The first and last curly braces show the beginning and end of your program's main code. Between them you can have other blocks of code between still inside
the main function. This will be the case most times for things like structs and such.
Example
{
{
{
}
}
}
Notice how they don't have to be exactly lined up. Include as much whitespace as you want, the compiler ignores it as long as it doesn't interfere with the syntax.
Every line of code in C++ will end in a ; usually. To have the compiler ignore things that aren't code, you can use comments.
Single line commment: //compiler ignores everything after the two slashes
Comment block (more than one line):
/*<starts here and
ends here*/
To output text or a variable in C++, use cout. To take input for a variable, use cin. To ignore the enter key use cin.ignore() . You can output specific text or a variable you've made.
Example: cout<<"Your name is <<age; Of couse you would have to declare age as in integer first, but that just shows how to use the insertion operator. The one for
output is << and input is >>. To start a new line, use \n. There are more escape characters for example, \a is a beep and \t is a tab. You'll learn more as you use them.
>Variables
I'll just cover the basic variable types, there are many out there. The basic ones are int, float, and char.
int = number variable
float = number with decimal variable
char = character variable
Declare a variable for number > int number = 7;
or
int number;
number = 7;
>If statements
Use if statments to do something if a condition is true, or not if it is false. Once you feel comfortable with these, move onto switch statments.
If statment example:
if ( condition ) {
execute whatever}
else {
execute if condition isnt true
}
Declare a variable after using namespace std and before int main to make it a global variable. That means you can mention it's name anywhere in the code later and it
will have the value you assigned without a doubt.
>Arrays
Declare an array by telling the array type, name of array, and elements in parentheses.
Example: char space[20];
Example:cout<<space[2]; //arrays use an index of 0, just like javascript and php
That declares a character array called space with 20 elements. You can use the array to access elements, have enough room to deal with the characters,
or other things. Sometimes its easier to use strings when dealing with input and C++ has it's own string class which is apart of the "std" namespace.
>Loops
for - declare variable, set condition for variable, update variable
while - execute while a certain condition is true
do-while - execute once then check again if the condition is true
Say you wanted to take the numbers 60 through 80 and multiply each by 23 then add 4. Knowing how to loop can make this a breeze. Not knowing can
make you write out the code 21 times. The for loop would be acceptable for this:
for ( int x = 60; x < 81; x++ ) {
cout<<x*23+4;
}
>Making and calling your own functions
Between the namespace you declare and int main, you can put a prototype for your own function. You can use the functions in your program or inline the function
and call it wherever you want later. This can save a lot of time and space in your program.
return_type function_name ( arguments ); //make sure you put the semi-colon as this is actually a function
Heres an example prototype:
int find_modulus ( j , k );
char find_ascii_total ( char a, char b )
or make an inline function and call it later
using namespace std;
inline void greeting()
{
cout<<"Whats up?";
}
int main()
{
greeting();
}
I'll end with an example program. It takes input for your name and age, then records them in a text file.
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
string name;
int age;
cout<<"Enter your name\n\n";
cin>> name;
cin.ignore();
cout<<"Enter your age\n\n";
cin>> age;
cin.ignore();
ofstream makefile ("stats.txt", ios::app);
makefile<< name<< " is " <<age<< " years old.";
makefile.close();
}
___________________
Always read all you can online or buy a book, which I haven't done yet but is probably a good idea. Check out
http://www.cprogramming.com/ and the cboard
for many FAQs. Hope this article helps some people.