Start programming with C++ by Fabian Heredia

Discussion about particular books go here.
Forum rules
Please make title book name & author.
Books about computers or hacking get stickied

Start programming with C++ by Fabian Heredia

Post by fabianhjr on Wed Nov 03, 2010 8:31 pm
([msg=48511]see Start programming with C++ by Fabian Heredia[/msg])

Start programming with C++
Copyright 2010, Fabian Heredia. Some rights reserved.
License:
Image

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
The first one IOStream helps us with the basic Input and Output while our second library String contains the object string. This avoids us having to use more complex instructions for the moment.
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.
Last edited by fabianhjr on Wed Nov 03, 2010 8:54 pm, edited 1 time in total.
Donate bitcoins to me! [1DhRP3hHgmSLQdRTZyT8VPTmzAj7Z2rsGA]
Dunno what bitcoins are? BitcoinMe
fabianhjr
Poster
Poster
 
Posts: 286
Joined: Tue Sep 21, 2010 7:48 pm
Blog: View Blog (0)


Re: Start programming with C++ by Fabian Heredia

Post by sanddbox on Wed Nov 03, 2010 8:35 pm
([msg=48513]see Re: Start programming with C++ by Fabian Heredia[/msg])

<3. Very cute that you're using HTS to write a book. :D

Good work so far.
Image

HTS User Composition:
95% Male
4.98% Female
.01% Monica
.01% Goat
User avatar
sanddbox
Expert
Expert
 
Posts: 2331
Joined: Sat Jul 04, 2009 5:20 pm
Blog: View Blog (0)


Re: Start programming with C++ by Fabian Heredia

Post by Goatboy on Wed Nov 03, 2010 8:56 pm
([msg=48518]see Re: Start programming with C++ by Fabian Heredia[/msg])

¿Por qué no lo haces cómo artículo? ("Why not make it as an article?" for all you non-Spanish-speaking lamers).
Assume that everything I say is or could be a lie.
1UHQ15HqBRZFykqx7mKHpYroxanLjJcUk
User avatar
Goatboy
Expert
Expert
 
Posts: 2806
Joined: Mon Jul 07, 2008 9:35 pm
Blog: View Blog (0)


Re: Start programming with C++ by Fabian Heredia

Post by fabianhjr on Wed Nov 03, 2010 9:06 pm
([msg=48519]see Re: Start programming with C++ by Fabian Heredia[/msg])

Porque lo voy a estar editando y es un libro.(bueno, va a serlo un dia)
También porque necesito mas comentarios y sugerencias.
Cuanto tenga lo suficiente lo voy a pasar a un txt y dejar solo el link.
Challenge: Discover what I just said and learn some spanish. :)
Donate bitcoins to me! [1DhRP3hHgmSLQdRTZyT8VPTmzAj7Z2rsGA]
Dunno what bitcoins are? BitcoinMe
fabianhjr
Poster
Poster
 
Posts: 286
Joined: Tue Sep 21, 2010 7:48 pm
Blog: View Blog (0)


Re: Start programming with C++ by Fabian Heredia

Post by Goatboy on Wed Nov 03, 2010 9:46 pm
([msg=48520]see Re: Start programming with C++ by Fabian Heredia[/msg])

Because I will be editing and a book. (Well, will be one day)
Also © n I need more comments and suggestions.
As I'll have enough to pass a txt and leave only the link.


Google Translate FAILS

Also, I had to add a translation or risk warning you. Rules, and whatnot.
Assume that everything I say is or could be a lie.
1UHQ15HqBRZFykqx7mKHpYroxanLjJcUk
User avatar
Goatboy
Expert
Expert
 
Posts: 2806
Joined: Mon Jul 07, 2008 9:35 pm
Blog: View Blog (0)


Re: Start programming with C++ by Fabian Heredia

Post by fabianhjr on Wed Nov 03, 2010 10:31 pm
([msg=48521]see Re: Start programming with C++ by Fabian Heredia[/msg])

D: Sorry forgot that rule!
Translation:
Because I will be editing it and it is a book not an article(Well, it will be one day)
Also because I am requesting comments and suggestions
When it gets mature enough I will pass it to a 1337 txt and host it myself.
Donate bitcoins to me! [1DhRP3hHgmSLQdRTZyT8VPTmzAj7Z2rsGA]
Dunno what bitcoins are? BitcoinMe
fabianhjr
Poster
Poster
 
Posts: 286
Joined: Tue Sep 21, 2010 7:48 pm
Blog: View Blog (0)


Re: Start programming with C++ by Fabian Heredia

Post by sanddbox on Wed Nov 03, 2010 11:41 pm
([msg=48527]see Re: Start programming with C++ by Fabian Heredia[/msg])

It's a pity the Spanish class I'm taking is so shitty (Spanish I). The first quarter just passed and they haven't even taught us how to conjugate verbs.
Image

HTS User Composition:
95% Male
4.98% Female
.01% Monica
.01% Goat
User avatar
sanddbox
Expert
Expert
 
Posts: 2331
Joined: Sat Jul 04, 2009 5:20 pm
Blog: View Blog (0)


Re: Start programming with C++ by Fabian Heredia

Post by Goatboy on Thu Nov 04, 2010 3:37 am
([msg=48536]see Re: Start programming with C++ by Fabian Heredia[/msg])

sanddbox wrote:It's a pity the Spanish class I'm taking is so shitty (Spanish I). The first quarter just passed and they haven't even taught us how to conjugate verbs.

Lo siento.

@fabi: I look forward to seeing this posted in its entirety. If you want any proof-reading, let me know.
Assume that everything I say is or could be a lie.
1UHQ15HqBRZFykqx7mKHpYroxanLjJcUk
User avatar
Goatboy
Expert
Expert
 
Posts: 2806
Joined: Mon Jul 07, 2008 9:35 pm
Blog: View Blog (0)



Return to Books

Who is online

Users browsing this forum: No registered users and 0 guests