C++ Training

From NexusCrossing

Jump to: navigation, search

I will start out slow with the basics, and then we will work from there. If the initial steps seem too simple, feel free to jump ahead.

Contents

[edit] Lesson 1: Hello World

This is the obligatory first program for just about any programming language. The reason for this is that a "Hello World" program will produce a visible result, and will generally be made up of all of the minimal components of a program in any given language, and a bit more.

[edit] Here is the code:

 ///
 /// \file hello.cpp
 ///
   
 #include <iostream>
 
 int main(void)
 {
   std::cout << "Hello World!" << std::endl;
   return 0; //main always returns an integer.
 }
  

[edit] Comments

The code above shows you a very basic program, and introduces a number of concepts without being obvious.

 ///
 /// \file hello.cpp
 ///

First, look at the first three lines. This is a C++ comment block. Any line in C++ that starts with two forward slashes (//) is a comment. I also added a comment to the return line as well. // means to start a comment from here, until the end of the line.

C++ also suports C style comment blocks. /* C style comment blocks are written like this.*/ C style comments have the advantage that you can put them in the middle of a line if you want, without terminating the line.


[edit] Include Basics

Now lets look at the next line:

 #include <iostream>

This is an include statement. It tells the compiler to include the contents of a header file at this location. More accurately, it tells the preprocessor this. I will go into more detail on that later though. For now, know that it includes the contents of the iostream header file in the program. iostream is the portion of the C++ standard library that handles basic input and output. It provides std::cout and std::endl that we will use in the main function.


[edit] The most basic structure of a function

 int main(void)
 {
  //Stuff
 }

Next we have the main function. int main(void) is the function's signature. It tells the compiler that the function main() will take no arguments (void), and return an integer value. In C++, there are a few possible signatures for main(), but it will always return an integer.

The body of the function is held within brackets ( {} ). This is called a block. A block is a grouping of data marked off by {}. Blocks are used in other contexts as well, and will play heavily into the matters of namespaces and scopes.

[edit] Functions and Functors

Next we have

   std::cout << "Hello World!" << std::endl;

The shortest explanation is that std::cout is the function you call to print information to the console. std::endl is a symbol that says "add a new line character here."

The more complicated version: std::cout is not really a function, but a functor. A functor is an object that has been created to act like a function. The difference is not important at this point, but will be later.

 std::cout << "Hellow World!"

std::cout is a stream operator. That means that you can feed it data, and it will send that data somewhere as a stream of bits and bytes. In this case, it is the stream operator for STDOUT, or standard output. In most cases, that is the terminal.

std::cout is a functor, in that it is an object that acts like a function. The bit shift operator (<<) has been overloaded for the functor (an ostream I think) to tell it to send the data on the right side of the << operator to the object that std::cout represents. This means we use std::cout to write text to the screen.

std::cout is a member of the std namespace. We will go into namespaces very soon.


[edit] Return!

   return 0; //main always returns an integer.


The signature of a function will include a return value. The return statement is used to return control to the calling function (the function that called the function). This is how we return values from a function.

The return value will be of the same data type as the return value stated in the signature of the function. If the function has a return value of void, then you can simply call:

 return;

This tells the function to return without returning a value. You do not HAVE to use a return statement in a void function, but it is a matter of coding style and habit.

[edit] Lesson 2: Namespaces and Scope

[edit] Definition of Scope

In simplest terms, scope is the range in which a variable is valid. It is the part of the code where you can access a chunk of data.

[edit] Definition of Namespace

A namespace is a region of code that is held separate for the purpose of naming identifiers. Namespaces are used to prevent namespace collisions. That is, if you create a namespace, and choose to name a function printf, it will not conflict directly with the printf in the standard namespace.

[edit] What is a Variable

If you have been programming a while, this may sound like a no brainer, but it is an important concept. It is a concept that is vital to the understanding the idea of scope.

In simplest terms, a variable is a human friendly label for a region of memory. C++ is a statically typed language, which means that every variable points to a specific type of data, and only that type of data.

A variable name is different from the region of code that it points too, but in most basic use cases the region of code will be tied to that single variable. At a later time, we will duscuss pointers which make this rule a little fuzzy.

[edit] More on Scope

 #include <stdlib.h>
 
 int random(int min, int max)
 {
   int add = max-min;
   int value = (rand() % min)+1;
   int value += add;
   return value;
 }


The function defined above is a good example for explaining scope.

First of all, it makes a call to the standard funciton rand(). rand() is defined in the global namespace, and is of global scope. That means that it can be called from anywhere in the program.


min, max, add, and value are all of local scope. They only exist inside of a given instance of random(min, max). They are created within the function, and are destroyed when the function exits.

Now for a slightly more complicated example.


 MyClass * my_class_pointer(void)
 {
   MyClass * my_pointer;
   my_pointer = new MyClass;
   return my_pointer;
 }


Ok. This looks simple, and really is. However, it does make issues of scope a little more fuzzy. I will go into more detail on pointers and how to use them in a later lesson, but for now I will say this. my_pointer is a pointer to a value of the type MyClass. This means that the VARIABLE my_pointer does not hold an actual MyClass value, but rather a memory address that points to a value of the type MyClass. The actual memory space is claimed and created by the new operator.

When the function my_class_pointer exits, the variable my_pointer is destroyed as usual. The data that it points to however is not. This is why I say that pointers blur the rules on scope. The address of the data is the return value of the function.


[edit] More on Namespaces

 // File: namedspace.hpp
 
 #include <iostream>
 
 namespace namedspace {
   void print(std::string msg);
 }


 // File: namedspace.cpp
 
 #include "namedspace.hpp"
 
 namespace namedspace{
   void print(std::string msg)
   {
     std::cout << msg << std::endl;
   }
 }


This is a very simple example. It only includes one function. This leaves very few complications.


First, we include the iostream header file. This creates the std namespace and imports a number of IO related functions, types and constants into it. We are using the string data type and the cout functor.

Since cout, string, and endl are all a part of the std namespace, we call them by prefixing their names with std::.  :: is the namespace operator. This tells the system exactly which version of cout, string, and endl we want to use.

Now, consider the following code:

 // File: something.cpp
 
 #include "namedspace.hpp"
 
 int main(void)
 {
   namedspace::print("This should work!");
   return 0;
 }


Assuming the porgrammer linked both namedspace.cpp and something.cpp into the same program, this should function as is. As you can see, we used namedspace::print() to access the print command. Here are a few more examples.

 // File somethingelse.cpp
 
 #include "namedspace.hpp"
 
 int main(void)
 {
   using namespace namedspace;
   print("This should work as well, but is probably not ideal.");
   return 0;
 }
 
 // File somethingbetter.cpp
 
 #include "namedspace.hpp"
 
 int main(void)
 {
  using namedspace::print;
  print("This is a bit better.  It only imports the part that we need.");
  return 0;
 }
 
 // File somethingwrong.cpp
 
 #include "namedspace.hpp"
 
 int main(void)
 {
   print("This will give an error message.  This function cannot see print.");
   return 0;
 }

[edit] Lesson 3: Variables and Data Types

This lesson may get moved higher in the list eventually. It is one of the things that I should get out of the way before I start to introduce practice exercises of any sort. I already spoke a bit about variables and data types in the section on Scope. Here I will go into a bit more detail, especially on data types.

[edit] References Etc.

Personal tools