Author Topic: [C++][Tut] Getting started with C++!  (Read 1510 times)

Offline Nitrus

  • Newbie
  • *
  • Posts: 47
  • Rep: 0
    • View Profile
    • Email
[C++][Tut] Getting started with C++!
« on: March 09, 2007, 09:25:50 pm »
This will be split in to 3 sections. The compiler, The knowledge, and The code. This is so you will take an effective step in to learning C++.

The Compiler
There are many many many C, C++, and other C based language compilers spread around the internet today. Two of the most common are Microsoft's Visual C++ (For use with Windows), and the GNU C++ Compiler(commonly known as gcc, mostly for use with *nix). If you do not like your compiler, then you most likely won't like the language, hence there would be no point learning in the first place! So lets get comfortable with compilers before we learn the language itself.

Windows Compilers
The most common Windows compilers are MS Visual C++ 6 through to 2005, Borland's C++ Builder, and Bloodshed's C++ Compiler. There are very many more compilers out there, but I'll cover the most used ones from hereon in, as they generally seem to fit most people.

Visual C++ is a great IDE for C++. It comes with all versions of Microsoft Visual Studio. The most commonly used C++ compiler in the series is Visual C++ 6.0. One reason may be that it uses standard C++ rather than a Visual Interface, allowing easy code conversion to another OS (well, console at least ;)). Another may be that people just seem to like Visual Studio 6.0, it was probably at the height of its existence a few years ago, however.

Next we have Borland's C++ Builder. C++ Builder is a very easy to use compiler, it provides an easy interface, while holding the power of C++ that we all love. Although this is a great compiler, and deserves two thumbs up from me, I like the alternatives, myself. This choice is up to you, however.

Last but not least, we have BloodShed Dev-C++. Dev-C++ is probably one of the best free C++ compilers available for download today. It was made in Delphi, and has a very easy interface. It should mostly be used for learning and novelty development rather than a pro setup. Most professional C++ users should look at Buying or Pirating a commercial product. You can download Dev-C++ from http://www.bloodshed.net/ - (I use Dev-C++).

Linux Compilers

The GCC (GNU C++ Compiler) is a command line tool used to compile C++. It may seem intimidating to use at first, after all, it is a program in the Linux command line, but it really is quite easy once you get the hang of things. Two thumbs up from me.

You can post and add to the list of compilers if you want.


The Knowledge

With C++, you can't just expect to get in there and start coding, it takes time. Any of you with experience in the C, C#, or Java languages should find it easier to learn than that of a person with knowledge of VB. C++ takes time to learn, therefore, it will take time for you to program in it. many people will say that C++ is hard. In fact, C++ is hard, but with effective coding techniques and crucial skills put in motion, C++ will become all but a difficult task. C++ was made as a superset of a language called C, which evolved from a language called B, which evolved from a language called BCPL.

C was a very very successful language, it has been the base of programming for a long long time. C++ is a superset of C, with added features, coding structure, and full object orientation. C++ can be effectively coded with C along side it, therefore it is useful to know both languages to be a professional coder.

From C++, two new languages evolved. C#, and Java. Java is versatile, making it very good for use on the internet. C# I find is like a mix between Java and C, so anyone who knows C or Java would find it wise to learn C# before learning the opposite language. C++ is still a big-daddy in programming today, even though it is old - so don't doubt its power!

With this power comes flexibility and the ability to do almost anything to your computer (virtually, not physically XD). All you need to do is press the right buttons. So to say, you can even make your own operating system like Windows, Mac or Linux with a language such as C or C++. Unfortunately, this is not so with Java or C#, because these two languages must be interpreted.

What is the difference between interpreted and compiled?

Interpreted means the program must be "read" from the operating system via the binaries that have been installed to use the language. I.E. Java and Flash. the Java Runtime Environment is required to run Java applications, just like it is for Macromedia Flash player to run Flash applications. C++, C, and other lower level languages are compiled, meaning they can be directly executed by the OS itself.

Okay, so whats better for me?

It all depends on what you're looking for. Myself, I'm a mix between VB, Java, C++, and C#. VB, Java, and C# are interpreted, but they are still effective programming languages for the OS that can run them. C++ is compiled, meaning it can make fast executing applications run directly from the OS. Neither should be considered a "better" language, because they all do what they were made to do, and thats all that they should be used for.

A good chain of languages to learn would probably be something like this:
Delphi->Java->C#->C->C++->HTML and PHP
This may seem a bit ambitious, but its a good mix. Delphi is a RAD useful for fast applications. Java is great for versatility. C# is a powerful language for use with Windows. C and C++ are great languages for use with almost all OS's. and HTML and PHP are Web languages used to make things such as websites, and without a knowledge of these you would probably soon find yourself in the dark.


The Code
Coding is the hardest part about this, as is always. People will say that C++ sucks for making GUI's, when infact, that is not so, It just requires a lot more time and effort to make a good GUI.

For this purpose, we will be making a Console application to be ran through Command line. command line applications are generally the easiest to learn first, which is why I will not be taking you through GUI development in this tutorial. I haven't even grasped GUI's myself, so you will most likely NEVER get a GUI tutorial out of me.

copy this code down in to your compiler:
Code: (cpp) [Select]
#include <iostream>
using namespace std;

int main()
{

&nbsp;&nbsp; // This is a comment, it will let the author remark on his code.

&nbsp;&nbsp; cout << "Hello world!\n";&nbsp; // This is a statement, therefore must be issued with a semicolon at the end of the line
&nbsp;&nbsp; // The above can just as easily be written as:
&nbsp;&nbsp; // cout << "Hello world!" << endl;
&nbsp;&nbsp; // but instead we used "\n" at the end of HelloWorld, which does the same thing!

&nbsp;&nbsp; return 0; // Seeing as main() is a function, we must return a value. In this case, 0 means it has executed properly.
}

compile your project. Windows uses will need to open an instance of CMD to execute this, otherwise it will just close the prompt window before you can blink. Linux users can execute their output using something like this:
Code: (cpp) [Select]
./a.outdepending on the output file.

Now you will see that it outputs "Hello World" Followed by a new line. Remove the "\n" from the end of "Hello World" and see what happens ;)

Now, go through it line by line.

Code: (cpp) [Select]
#include <iostream>
using namespace std;

This part of the code is the most vital for your programs execution. It is the part of the code that is preprocessed before anything else happens. the cout function and everything else vital to creating a console application is inside the "iostream" header.

#include <iostream> simply includes the <iostream> header, which as stated, contains all of the vital functions. using namespace std; means it is using a namespace called std, which is also as vital for program execution. Think of a namespace as a placeholder for execution.

Code: (cpp) [Select]
int main()
{
&nbsp;&nbsp; ...
&nbsp;&nbsp; ...
}

This is the main function. After the preprocessed piece of code, this is the first part of the program that executes, therefore main() MUST and I mean MUST be a part of every single C++ program.

Code: (cpp) [Select]
cout << "Hello World!\n";
This outputs the line "Hello World!" as well as a new line. Read the comment below the bit of code for more information.

Code: (cpp) [Select]
return 0;as the comment says, seeing as main() is a function, it must return a value. returning 0 generally means it has executed properly.

C++ is case sensitive, therefore it was made so that almost everything that is written in C++ apart from variables, strings, and comments is in lower case, to avoid confusion.




THE END

What do you think?
« Last Edit: June 24, 2008, 01:41:42 pm by Freddy1990 »

Offline Zoiked

  • Full Member
  • ***
  • Posts: 125
  • Rep: 0
  • What happens if I put text here. :P
    • MSN Messenger - Bllizzd@gmail.com
    • View Profile
    • ZoikedNet
Re: [C++][Tut] Getting started with C++!
« Reply #1 on: April 08, 2007, 12:27:50 am »
Great Tutorial but there is one thing I disagree with......
Quote
Delphi->Java->C#->C->C++->HTML and PHP

I Think this is a bad learning line because the the fact that this is a very broad range of languages for one person to learn.

Also, Html should be first because of its simpleness and its fun to learn. Next (Sadly) Visual Basic should be next to introduce the programmer into OOP in a simplified language. Next should either be Delphi(I Think Its Outdated!) or C# or just skip to C++ or Java(C++ Would be better though).

Html->Visual Basic(6 or .NET)->C#(Just Basics)->C++

Offline sparkz

  • Leecher
  • Posts: 3
  • Rep: 0
    • View Profile
Re: [C++][Tut] Getting started with C++!
« Reply #2 on: April 16, 2007, 12:29:26 pm »
Very nice, just got a few nit-picks.

Dev-C++ is an IDE that uses Mingw compiler - it is not a compiler itself.
int main() must return an integer value, not just any random value.

Other than that - great job (Y)

Offline Se7eN

  • Global Moderator
  • *****
  • Posts: 458
  • Rep: 2
  • Cpp Guru
    • View Profile
    • Email
Re: [C++][Tut] Getting started with C++!
« Reply #3 on: May 10, 2007, 07:37:24 pm »
Great Tutorial but there is one thing I disagree with......
Quote
Delphi->Java->C#->C->C++->HTML and PHP

I Think this is a bad learning line because the the fact that this is a very broad range of languages for one person to learn.

i agree expecally since c is the only one on the list that does not fully support oop (besides html and php...their web languages so they don't count :P)

Also, Html should be first because of its simpleness and its fun to learn. Next (Sadly) Visual Basic should be next to introduce the programmer into OOP in a simplified language. Next should either be Delphi(I Think Its Outdated!) or C# or just skip to C++ or Java(C++ Would be better though).

Html->Visual Basic(6 or .NET)->C#(Just Basics)->C++
Quote from: TheGuyWhoGotOn
Ok you look like you're going to break a lot of rules so here is a heads up, next time if you try to log in and cant, its because your banned.

Offline WhoCares357

  • Wiki Writer
  • *****
  • Posts: 37
  • Rep: 0
  • Freddy Follower
    • View Profile
    • Email
Re: [C++][Tut] Getting started with C++!
« Reply #4 on: June 05, 2007, 11:04:50 am »
What's the difference between c++ and c#?
to accept all is to accept nothing

Offline pplsuqbawlz

  • Community Member
  • ******
  • Posts: 12
  • Rep: 3
    • View Profile
Re: [C++][Tut] Getting started with C++!
« Reply #5 on: June 16, 2007, 08:07:54 am »
SEE: http://www.tiobe.com/tpci.htm - JAVA/C/C++ etc.

Offline disturbedone2004

  • Leecher
  • Posts: 2
  • Rep: 0
  • Comp Sci Major Senior Lvl
    • Yahoo Instant Messenger - Disturbedone2004
    • View Profile
Re: [C++][Tut] Getting started with C++!
« Reply #6 on: September 28, 2007, 07:05:13 pm »
I feel I should mention that while /n and endl both end the current line and begin a new one, endl flushes the memory buffer when used. I always use endl.
There are 10 kinds of ppl in this world

those who understand binary,

and those who don't.

Offline Freddy1990

  • Owner
  • *****
  • Posts: 2301
  • Rep: 19
    • MSN Messenger - freddy1990@gmail.com
    • AOL Instant Messenger - Freddy199O
    • View Profile
    • Email
Re: [C++][Tut] Getting started with C++!
« Reply #7 on: December 12, 2007, 12:52:46 pm »
Quote
Delphi->Java->C#->C->C++->HTML and PHP
That's bs...
First of all, html isn't a programming language, it's a markup language, that's 2 completely different things...
And if you want to learn java you shouldn't waste ur time learning Delphi, c++ or any other native language because java works very differently...
For that matter it's better to start with the language you want to learn rather than to learn another one before that which you won't use...

Offline darak555

  • Leecher
  • Posts: 9
  • Rep: 0
    • View Profile
    • Email
Re: [C++][Tut] Getting started with C++!
« Reply #8 on: June 15, 2009, 03:44:02 pm »
i have basic C# C++ J# and web developer

Offline BroB0t

  • Newbie
  • *
  • Posts: 10
  • Rep: 0
    • View Profile
    • Email
Re: [C++][Tut] Getting started with C++!
« Reply #9 on: July 06, 2009, 04:53:21 pm »
Nice tut helped me out.