- Impact
- 31
Hey everyone,
Another one of those tutorials I've been requested to write :P I'll start of with the basics and actually will be moving to intermediate throughout the tutorial.
What you'll learn in this tutorial:
What is C++?
Alright, let's begin with a very beginning knowledge. If you want more than this, check out wikiepedia or something All you really need to know is it came from the antiquiated C programming. It is a computer programming languages and is the most flexible out of all of them! (Others include: C, C#, ASP, Perl, the list goes on...)
What is cross-compatibility?
For those of you haven't found out yet, Windows isn't the only operating system ("ahhhh", from the man reading this who jumps out his window in terror of his world being flipped upside down) I know, it may be hard for those of you who have only known windows your entire life to accept this, but you will in time So, that being said, different operating systems work differently. Windows works differently than Linux, etc. That's why to keep it simple we're not going to be coding any GUI's but simply console/terminal programs that will be working both on windows and *nix. However, I won't be teaching how to cross-compile as it is a somewhat advanced system management things. Takes a lot of configuring/rebuilding depending on what you're coding for. I'll dabble slightly on an easy way to do it on Debian systems to Windows but the other stuff will take a lot of explanation. I will probably create a separate tutorial later on doing that.
What is OOP?
OOP is an acronym. It stands for ObjectOrientedProgramming. This basically deals with classes, pointers, etc. which will be explained more in depth when we get to them :D If you already know a web language like PHP this concept should not be totally new as PHP does it pretty much exactly the same!
Ok, now it's really hard to start one of these basic tutorials for C++. There is SO much there is really no good place to start. I guess I'll write the infamous "Hello World" script and explain through and through.
hello.cpp
Let's begin. With comments, you can put anything in them and the compilers skip over them so you don't get any errors for having them in there.
Next on the list, the #include. This will include other files. For the standard C++ libs google but it can also be used to include your own files you've created.
As far as the namespace goes, this is all basically explained in the comment I put in.
int main() - Take note of the structure. int stands for integer. So this is a numerical output as opposed to something like bool which stands for boolean which returns a true/false value. There are others as well but we can talk about them later. This is in the main function the program will execute at startup.
cout - Seeing as we're using the "iostream" lib this is the command we use. The two less than signs make the "cout" print whatever comes after. We'll learn later with inputs that the reverse is true for a different operation. Anything within quotes is plain text. When ending quotes, the << is put in between each new item. In particular there is "<< endl << endl" endl stands for "End line". Simply creates a line break. Then, followed by more quoted text and one more endl. Be sure to end every line with a semi-colon [;].
return 0 - remember earlier I said it was necessary to return a numerical value to int, where this is where it comes into play because the function is int.
new line at the end of code. This is essential to have a new line at the end of your code or your code WON'T compile
Next, we'll be moving on to user inputs. Examine the code below carefully.
Baically using strings here. Comments should say the most of it.
This is becoming very lengthy so I'm going to wrap it up with 2 or 3 more examples for you guys.
Post is too long so it must be posted across a few, continue on!
Another one of those tutorials I've been requested to write :P I'll start of with the basics and actually will be moving to intermediate throughout the tutorial.
What you'll learn in this tutorial:
- What is C++
- What is cross-compatibility
- What is OOP and how to use it
- The basic syntax of a C++ program
- How to compile code into programs!
What is C++?
Alright, let's begin with a very beginning knowledge. If you want more than this, check out wikiepedia or something All you really need to know is it came from the antiquiated C programming. It is a computer programming languages and is the most flexible out of all of them! (Others include: C, C#, ASP, Perl, the list goes on...)
What is cross-compatibility?
For those of you haven't found out yet, Windows isn't the only operating system ("ahhhh", from the man reading this who jumps out his window in terror of his world being flipped upside down) I know, it may be hard for those of you who have only known windows your entire life to accept this, but you will in time So, that being said, different operating systems work differently. Windows works differently than Linux, etc. That's why to keep it simple we're not going to be coding any GUI's but simply console/terminal programs that will be working both on windows and *nix. However, I won't be teaching how to cross-compile as it is a somewhat advanced system management things. Takes a lot of configuring/rebuilding depending on what you're coding for. I'll dabble slightly on an easy way to do it on Debian systems to Windows but the other stuff will take a lot of explanation. I will probably create a separate tutorial later on doing that.
What is OOP?
OOP is an acronym. It stands for ObjectOrientedProgramming. This basically deals with classes, pointers, etc. which will be explained more in depth when we get to them :D If you already know a web language like PHP this concept should not be totally new as PHP does it pretty much exactly the same!
Ok, now it's really hard to start one of these basic tutorials for C++. There is SO much there is really no good place to start. I guess I'll write the infamous "Hello World" script and explain through and through.
hello.cpp
Code:
/**
* Hello World Script by RageD :)
*
* This is a comment by the way
* it can also be done by putting "//" in front of a single line without
* a line break. No quotes of course.
*
*/
// Here is the second way to comment :) But it ends when hitting return
#include <iostream> // Using the IOStream include. Later we'll be using the cstdio as it's preferrable imo
using namespace std; // Declare the namespace. This way we can access the "std" library for C++ giving us certain functionality
int main() // Definition of main function - This is where all C++ programs begin
{
cout<< "Hello World" << endl << endl << "This is a C++ Program!" << endl;
return 0; // Because this is an "int" (for integer) return a number value to the function. 0 exits.
}
Let's begin. With comments, you can put anything in them and the compilers skip over them so you don't get any errors for having them in there.
Next on the list, the #include. This will include other files. For the standard C++ libs google but it can also be used to include your own files you've created.
As far as the namespace goes, this is all basically explained in the comment I put in.
int main() - Take note of the structure. int stands for integer. So this is a numerical output as opposed to something like bool which stands for boolean which returns a true/false value. There are others as well but we can talk about them later. This is in the main function the program will execute at startup.
cout - Seeing as we're using the "iostream" lib this is the command we use. The two less than signs make the "cout" print whatever comes after. We'll learn later with inputs that the reverse is true for a different operation. Anything within quotes is plain text. When ending quotes, the << is put in between each new item. In particular there is "<< endl << endl" endl stands for "End line". Simply creates a line break. Then, followed by more quoted text and one more endl. Be sure to end every line with a semi-colon [;].
return 0 - remember earlier I said it was necessary to return a numerical value to int, where this is where it comes into play because the function is int.
new line at the end of code. This is essential to have a new line at the end of your code or your code WON'T compile
Next, we'll be moving on to user inputs. Examine the code below carefully.
Code:
/**
* Simple User Input by RageD
* (C) RageD 2008.
*
* In this tutorial it is assumed you recall previously
* covered material and all tutorials fromt here on forth
* in this segment.
*
*/
#include <iostream>
#include <string> // Allow us to declare things as "strings"
using namespace std;
int main()
{
string name; // Declare variable as a "string" that comes from the #include <string> header file
// In all honest, you'll probably never use the "string" variable as it's eh. Not
// Very good programming really. But we'll go over that later this is just to start
// Grasping the concept of programming. Secure/efficient programming comes after this :P
cout<< "To exit, please type 'exit' for your name.\n\nPlease enter your name: ";
cin>> name; // This is the user input. The >> means to set the "name" variable to it
if(name != "exit") // If is a conditional statement. It is explained below in the tutorial
{
cout<< "Nice, your name is " << name << endl << endl << "I like it..." << endl << endl;
// Adding the name variable to be output is very similar to the endl as you can see :)
return main();
} else {
return 0; // Loop to the main function over and over until "exit" is typed.
}
}
Baically using strings here. Comments should say the most of it.
This is becoming very lengthy so I'm going to wrap it up with 2 or 3 more examples for you guys.
Post is too long so it must be posted across a few, continue on!