Copyright 2010, Fabian Heredia. Some rights reserved.
License:

Introduction
Before we can start lets cover the basics. What's a program? What is programming? you may ask. Well that is simple, a program is a sequence of instructions a computer can understand. Why are you doing this? Being a programmer isn't that easy so I decided to start writing this docs as I learn in a hope someone will find it useful. What do I need? You will need a compiler and, if you please, an IDE. This isn't cover in this book, however, you must learn to use whatever you choose.
Our first program
Create a new C++ file and call it "first.cpp". Now type (DON'T COPY!), compile, and run the following C++ code:
- Code: Select all
#include <iostream>
#include <string>
using namespace std;
int main(int argc, char *argv[])
{
string first, last;
if(argc < 3)
{
cout << "Enter your first name: ";
cin >> first;
cout << "Enter your last name: ";
cin >> last;
}
else
{
first = argv[1], last = argv[2];
}
cout << "Hello " << last << ", " << first << "!";
return 0;
}
This might seem cryptic at a first glance. Lets go line by line:
- Code: Select all
#include <iostream>
#include <string>
This is called a a preprocessor directive. We are telling the preprocessor to include a file. In this case the following libriaries:
- IOStream
- String
- Code: Select all
using namespace std;
Namespaces are like folders. The instruction using tells that we will be referring to the namespace std until we say otherwise. std refers to standard thats the namespace where all our basic functions / objects reside.
- Code: Select all
int main(int argc, char *argv[])
In here we are declaring a function called main. In C++(and many other compiled languages) main is special since it is where the program will start. In C++ we get some variables by default, int argc and char *argv[]. The first one contains the number an arguments the program received upon runtime. argv[] is the array of arguments. By default we get 1 which is the location the program is run from. EX(*NIX and Windows):
- Code: Select all
/home/fabianhjr/programs/first
C:\Documents and Setting\fabianhjr\programs\first.exe
WORK IN PROGRESS!
Please report any typos, mistakes, misleads, or anything of your concern.



