This is yet another one of my basics tutorial, however, as you can tell this one is on C++, and not one of the web programming languages like I normally do tutorials on.
Starting out Before you can start programming in C++ it is a good idea to get a compiler that works well. I recommend getting Visual C++ 2008 express edition, as it is free and fairly powerful, but there are a number of compilers out there.
Basic C++ applications start out like so:
cpp
int main(){
return EXIT_SUCCESS;
}Using Comments in CodeComments in C++ can consist of either a single line comment:
// comment here, or a block comment:
/* Comment here
and here
and here */In source code comments can look like so:
cpp
int main(){ // create the main function
return EXIT_SUCCESS; // exit saying that the program ran successfully
}If you wanted to use block comments instead that can look like so:
cpp
/* This function attempts to return
an integer upon successful completion
*/
int main(){
return EXIT_SUCCESS;
}
However, people normally format their block comments like so:
cpp
/* This function attempts to return
*an integer upon successful completion
*/
int main(){
return EXIT_SUCCESS;
}
To avoid confusion.
NOTICE - There is an additional * and space in each line to make the comment block line up correctly.
Data TypesThere are a number of "primitive data types" in C++ that consist of
int,
double,
float,
long,
char,
bool, and others. These are known as "primitives" because they don't require that you import anything into your code to use them successfully.
There are also more complex data types, like String, or Student (we will create a simple class for this in a second). The complex data types need to be initialized to work correctly.
Here is a simple example of a student class:
cpp
class Student{
private:
char* name;
public:
Student(char* nName){
name = nName;
}
char* getName(void){
return name;
}
};Which can be implemented like so:
cpp
Student* test = new Student("Freddy");
printf("The Student %s\n", test->getName());
return 0;The endI know this isn't the best tutorial, but I hope it shows some of the basics behind C++.