Teaching myself C++ part 1. Hello world!

Recommended Videos

cyber_andyy

New member
Dec 31, 2008
767
0
0
About this series:
Or rather, what I hope will be a series.

I've always wanted to learn an advanced programming language. I've loved the idea of being able to sculpt the computer to do what I want it to. My dad happens to be versed in many programming languages, and my mum is versed in visual basic. Me? Some DOS/batch file stuff, HTML and a smidge of VB.

This originally was going to be in a blog format, but since it would take forever to get started, the regular need for help, and space between posts, I took the decision to write up my experiences on the forums and go from there.

I will be editing the learning process. By that I mean I'll only include what is essential, so ill be removing many mistakes I make, various trips to Google to understand things and so on. When it comes to things I had to Google, I will write up what I gained, if anything.

I would love help from people who are versed in C++ and I hope to perhaps help others, also learning C++.

Getting Started:

After talking to my dad and looking on the net, I notice that 'Teach Yourself C++ in 24 hours' is a good book to get started with. My dad also happens to have a copy of the third edition amongst the multitude of code books on the shelves. Its available on line here [http://newdata.box.sk/bx/c/], and the physical copy comes with a compiler and all the code in the book.

As with anything new, I open it, ram the CD into my machine and fire it up. I install a compiler, fire it up and am raring to go.

First I need to...set up the compiler as they have in the book. No reason for this, but I storm ahead anyways.

Now I can start with the 'Hello World!' program.

I enter in the code as shown in my book annnnnddd:

[IMG_inline align="center" height="436" width="590" caption="Errors?" ]http://i156.photobucket.com/albums/t27/cyber_andyy/Teaching%20myself%20C%20series/Errorscreen.jpg[/IMG_inline]

Fuck.

I notice I put a v instead of a c for 'cout'. This is not encouraging. With the speed and dexterity of a walrus, I edit my foolish mistake. Annnnnddd:

[IMG_inline align="center" height="436" width="590" caption="Success!" ]http://i156.photobucket.com/albums/t27/cyber_andyy/Teaching%20myself%20C%20series/success.jpg[/IMG_inline]

Success! - Now to run it.

I double click, a flash of the command prompt appears on screen and then nothing. I'm left confused - Startled. What went wrong?

I take matters into my own hands, with my excellent deduction skills I change that 0 into a 1 and hey presto! Nothing happens. Well that was useful. By clicking the run button enough times, I can get the program to flash on and off like a strobe of failure. Still, I can read the program.

After realizing that my rhythm is much to be desired, I give up on the notion of attaching a screen shot.

I play around and revert some of the things we changed. More importantly, some of the 'set up' code for the compiler it got me to delete. (This is code is added automatically at the beginning of a new source file for you, by the compiler.)

After adding all that it works! I work backwards and deduce a typo in the book, or, something, left out a little .h in the . I change this to and we have a screen shot of my labours:

[IMG_inline align="center" height="436" width="590" caption="Im so good." ]http://i156.photobucket.com/albums/t27/cyber_andyy/Teaching%20myself%20C%20series/Fixed.jpg[/IMG_inline]

I push that any key to continue. And I push it well. For I have just corrected the textbook. I. Am. GOD!




Next week: Comments, Functions, Variables and Constants



Comments appreciated, as are explanations of what that .h does, and weather I should keep doing this :)
 

Lukeje

New member
Feb 6, 2008
4,047
0
0
You need a newer compiler... modern C++ compilers don't need the `.h'. What compiler are you using?
 

Kirtar11

New member
May 2, 2010
1
0
0
You are using really outdated stuff
This is a really good guide:
http://www.cplusplus.com/files/tutorial.pdf

1) Get a new compiler
2) In the modern version of C++, including libraries is done like this
#include
#include
etc.

you don't need the .h

Also, you should always have:
using namespace std ;

after your libraries

This makes it so you dont have to do that :: stuff with inputs and outputs
 

cyber_andyy

New member
Dec 31, 2008
767
0
0
Lukeje said:
You need a newer compiler... modern C++ compilers don't need the `.h'. What compiler are you using?
Bloodshed Dev C++ 4. It was a case of it was on the CD. I did check the website, and the latest they have is version 5.9 beta, released in 2005. I might change once I've found my feet a bit more. But not until I'm a bit more comfortable.

Kirtar11 said:
You are using really outdated stuff
This is a really good guide:
http://www.cplusplus.com/files/tutorial.pdf

1) Get a new compiler
2) In the modern version of C++, including libraries is done like this
#include
#include
etc.

you don't need the .h

Also, you should always have:
using namespace std ;

after your libraries

This makes it so you dont have to do that :: stuff with inputs and outputs
Any suggestion on a free compiler?

Had a look at that tutorial, looks excellent. Will be using with the book.
 

Vhite

New member
Aug 17, 2009
1,978
0
0
In some cases (I think its a matter of compiler) can your program disappear as fast as it appeard,you should add few lines so everyone can see what have they done and also what compiler is that? And you could metioned that iostream is there for input and output and you could aloso metioned how basic stuff like text output and what is #include before you get to Functions and Variables.
 

cyber_andyy

New member
Dec 31, 2008
767
0
0
Vhite said:
In some cases (I think its a matter of compiler) can your program disappear as fast as it appeard,you should add few lines so everyone can see what have they done and also what compiler is that? And you could metioned that iostream is there for input and output and you could aloso metioned how basic stuff like text output and what is #include before you get to Functions and Variables.
I wasn't sure about how deep I should to go this early, as I want to keep it from being too complex. In the next part, I'll go more in depth, and we will see how it turns out.
Vhite said:
Any suggestion on a free compiler?
Try newer dev-c++ or code::blocks
I will look into both of them.
 

Darktau

Totally Ergo Proxy
Mar 10, 2009
917
0
21
Windows does C++ express (its free) and works fine.

"Also is it bad using Visual Studio?"

No, use it!

and microsoft does free game making tutorials. (For PC games and 360)

http://creators.xna.com/en-GB/
 

yoyo13rom

New member
Oct 19, 2009
1,002
0
0
Kirtar11 said:
You are using really outdated stuff
This is a really good guide:
http://www.cplusplus.com/files/tutorial.pdf

1) Get a new compiler
2) In the modern version of C++, including libraries is done like this
#include
#include
etc.

you don't need the .h

Also, you should always have:
using namespace std ;

after your libraries

This makes it so you dont have to do that :: stuff with inputs and outputs
Outdated? Heh, I still sometimes use the old faithful borland C++ with blue background.
I mean the syntax may be a little different, and the interface more colourful, but the basics and algorithms are the same.
It's all about different preferences if you ask me.
The most advantages you could get form a newer C++ program builder is an autocomplete option.
 

zakski

New member
Mar 24, 2009
145
0
0
Savory said:
... Is it closed with [] or ?

Also is it bad using Visual Studio?
is good if its from the standard template library
visual studio is way over complicated for beginners stay away from it
 

Vhite

New member
Aug 17, 2009
1,978
0
0
cyber_andyy said:
Vhite said:
In some cases (I think its a matter of compiler) can your program disappear as fast as it appeard,you should add few lines so everyone can see what have they done and also what compiler is that? And you could metioned that iostream is there for input and output and you could aloso metioned how basic stuff like text output and what is #include before you get to Functions and Variables.
I wasn't sure about how deep I should to go this early, as I want to keep it from being too complex. In the next part, I'll go more in depth, and we will see how it turns out.
Vhite said:
Any suggestion on a free compiler?
Try newer dev-c++ or code::blocks
I will look into both of them.
Im not saying that you should talk about streams and cout and cin objects but you should tell something about it so its not just magical word.
 

Vhite

New member
Aug 17, 2009
1,978
0
0
zakski said:
Savory said:
... Is it closed with [] or ?

Also is it bad using Visual Studio?
is good if its from the standard template library
visual studio is way over complicated for beginners stay away from it
Agree,I prefer dev-c++ because it has easy way to instal 3rd party libraries.
 

knhirt

New member
Nov 9, 2009
399
0
0
First of all, good luck! Learning to program is a long and arduous task. You'll need plenty of patience and resolve.

cyber_andyy said:
I take matters into my own hands, with my excellent deduction skills I change that 0 into a 1 and hey presto! Nothing happens. Well that was useful.
In case you haven't read up on it yet: The "return" value at the end of your program signifies what the program returns to your operating system when it finishes up. 0 is standard here for "everything is a-okay".
 

cyber_andyy

New member
Dec 31, 2008
767
0
0
Neptunus Hirt said:
First of all, good luck! Learning to program is a long and arduous task. You'll need plenty of patience and resolve.

cyber_andyy said:
I take matters into my own hands, with my excellent deduction skills I change that 0 into a 1 and hey presto! Nothing happens. Well that was useful.
In case you haven't read up on it yet: The "return" value at the end of your program signifies what the program returns to your operating system when it finishes up. 0 is standard here for "everything is a-okay".
So what does happen when I change that 1 to 0? I didn't notice a change.
 

nezroy

New member
Oct 3, 2008
113
0
0
You should spend your time learning either C or C#. C++ is a language specifically designed to help experienced programmers solve hard problems. It's just about the least-beginner-friendly language you will ever encounter, and more importantly, the least useful moving forward. It isn't just C with classes, and it is NOT the language to start with. In fact it's not a language you'll probably ever even need to use anymore in the modern programming world unless you have very specific performance requirements (and you don't, nor are at a point to identify if you someday might).

If you want to really learn about programming with all of the fundamental "advanced" topics (early binding, static typing, compilation and linking, memory handling, asynchronous/multi-threaded programming, sockets and communication, etc.), then pure C is the way to start. Particularly if you ever want to write drivers or Unix kernel code. (And if you want to learn about all those things and also drink the OO kool-aid too, then at least start with Java).

If you want to learn how to make a modern Windows PC do cool things, then C# is the place to start. The esoteric and increasingly archaic knowledge you'd learn from C about memory allocation, string handling, and deep socket knowledge, will all be traded-in for the the cherry-picked aspects of OO that are actually cool and worthwhile in C#.

Honestly, it's increasingly difficult to justify why any new programmer should learn the fundamentals of malloc and pointers from the C world, as these are menial tasks that are rightly abstracted in modern languages to keep programmers from making unavoidable mistakes that lead to inevitable and gaping security holes. Understanding OO concepts like interfaces and polymorphism, or tackling asynchronous/parallel programming, are easily tasks worthy of proving your mettle in this brave new world. So really, just go with C#.
 

Vhite

New member
Aug 17, 2009
1,978
0
0
nezroy said:
You should spend your time learning either C or C#. C++ is a language specifically designed to help experienced programmers solve hard problems. It's just about the least-beginner-friendly language you will ever encounter, and more importantly, the least useful moving forward. It isn't just C with classes, and it is NOT the language to start with. In fact it's not a language you'll probably ever even need to use anymore in the modern programming world unless you have very specific performance requirements (and you don't, nor are at a point to identify if you someday might).

If you want to really learn about programming with all of the fundamental "advanced" topics (early binding, static typing, compilation and linking, memory handling, asynchronous/multi-threaded programming, sockets and communication, etc.), then pure C is the way to start. Particularly if you ever want to write drivers or Unix kernel code. (And if you want to learn about all those things and also drink the OO kool-aid too, then at least start with Java).

If you want to learn how to make a modern Windows PC do cool things, then C# is the place to start. The esoteric and increasingly archaic knowledge you'd learn from C about memory allocation, string handling, and deep socket knowledge, will all be traded-in for the the cherry-picked aspects of OO that are actually cool and worthwhile in C#.

Honestly, it's increasingly difficult to justify why any new programmer should learn the fundamentals of malloc and pointers from the C world, as these are menial tasks that are rightly abstracted in modern languages to keep programmers from making unavoidable mistakes that lead to inevitable and gaping security holes. Understanding OO concepts like interfaces and polymorphism, or tackling asynchronous/parallel programming, are easily tasks worthy of proving your mettle in this brave new world. So really, just go with C#.
Im not really experienced programer but I have to disagree,for me was c++ pretty easy to learn and while its true that you probably wont use it in modern programing it is not useless, it makes you learn other programing languages much easier (as far as I have heard knowing C++ basicly means that you already know 90% of C#) and if you want to work as programer you should know more then one programing language so you dont have pick only one.
 

rex922

New member
Sep 30, 2009
289
0
0
ok i think ill correct you on a few things
i use visual studio 2010
in it #include does not require .h
but #include<windows.h> does

after your #include

you can add using namespace std; //causes the default namespace to be std

adding that line will allow you to use cout instead of std::cout

a last little note

instead of writing cout<<"Hello World!\n"; you can write cout<<"Hello World!"<<endl; instead
 

nezroy

New member
Oct 3, 2008
113
0
0
Vhite said:
it makes you learn other programing languages much easier
This is true of basically any language. Learning one provides a foundation for learning others.

and if you want to work as programer you should know more then one programing language so you dont have pick only one.
The idea for knowing more than one is to know more than one language with different pros/cons for diverse tasks. The idea is to have a hammer and a wrench, not two different sized hammers.

That said, C++ is not like C#, just as C++ is not like C. Some of the syntax is similar but most of the paradigms and programming best practices are quite different between the 3.