Jump to content


Starting off with C++


4 replies to this topic

#1 CodeCat

    It's a trap!

  • Gold Member
  • 6111 posts

Posted 14 October 2007 - 19:27

Here is a short tutorial for those of you who'd like to learn C++, but don't know where to start. It explains a bit of the basics about C++ programming and also helps you through getting your first program built.


Introduction

C++ isn't the easiest language, and it might be a little bit daunting for a beginning programmer. C++ isn't as explicit and manually-controlled as C, but you have to be aware of what you're doing most of the time. It doesn't do anything you don't explicitly tell it to, so if you forget something, it'll go wrong later on. This also means that you'll be in full control of your program. Nothing is provided for you, but it does give you the opportunity to do things your way. And of course there are plenty of ways to ensure that everything that should be done gets done, but it needs a little more exercise and experience than with most other languages like Java or PHP.

One thing that sets C++ aside from other languages (and is the main reason I like it) is the freedom it offers. It gives you several different programming methods to work with, and it's up to you which way to use them or how to combine them. You could either go hardcore and do everything the C way using only functions, while taking advantage of some of the things C++ offers over C, but you could also do everything object-oriented a-la Java. This kind of freedom is, again, why it isn't as easy for beginners, but once you get the hang of the language and get an idea of working out a proper structure or strategy for a program, you'll quickly come to appreciate what C++ has to offer.

And let's not forget the fact that C++ is almost a perfect superset of C, and most C code will work flawlessly in C++. In fact, most of the C standard library (which comes included with the language) is also part of the C++ standard library. There is a LOT of C code out on the internet, and many code libraries are written in C. When you're using C++ all of that becomes easily accessible to you. The only catch is that those libraries would of course not be using any of C++'s advantages, but only the subset that it has in common with C.


A bit about the C++ build process

C++ is a compiled language. This means that the source code you write (.cpp and .h files) is translated into machine code (which the computer understands) by a compiler, and the end result is called object code. Object code, by itself, is not a program, it's only part of one. For each file of source code, one object code file is created. The final step is then performed by the linker, which has the task of taking all the object code files produced by the compiler, and linking them together into a single binary file. The complete process of compiling source code and linking the object files is called building the program.

Binary files have different properties depending on what they're intended for. An executable is an actual program, which can be run by itself. In Windows these have an .exe extension, and in Linux they are files that have the executable permission set. A (static) library isn't a program, but it contains parts of a program that can be used by new programs. This is done by having the linker link the library with the rest of the program, making the library's code part of the new program. Static libraries usually have a .lib or .a extension. And finally, a shared or dynamic library is a special kind of library, which doesn't need to be made part of the program's executable. Instead, they remain as a separate file, and are linked when the program is run. These libraries have a .dll extension in Windows, and a .so extension in Linux.


Setting up

To be able to write C++ programs, you'll need a text editor (such as Notepad++) and a compiler suite. This usually includes a compiler, a linker and other utilities. There are several around, such as GCC (GNU Compiler Collection) or the Visual C++ compiler, but I'll focus on MinGW, which is the Windows version of the well-known GCC used in Linux. It also happens to be what I use. There are also several Integrated Development Environments (IDE), which make programming easier by offering a graphical interface, which does most of the dirty work in building, and also include a good text editor specifically for that programming language.

I use the Code::Blocks IDE myself (and guess what this forum was named after!), but for this tutorial I'll be using wxDev-C++ because it comes with the MinGW compiler and wxWidgets graphical interface library, which makes it easy to install everything you need in one go.

Start off by downloading wxDev-C++ from here.

The wxDev-C++ installation starts by asking what compilers you'll be using. It can be used with both the Visual C++ and MinGW compilers, but since it comes with MinGW included, just pick MinGW. Next it'll ask what components to install. One option to take note of is the 'Associate C and C++ files with wxDev-Cpp' option, you might want to turn that off if you're prefer to keep your associations. After that, just let the installation do its job. When it's done, choose to run wxDev-C++.

When you run wxDev-C++ for the first time, you'll need to answer some questions. First, pick whatever language and theme you'd like. Next, it'll ask if you want to use code completion. This is a useful feature, so be sure to turn it on. Then it will ask if you'd like to create a code completion cache so the code completion feature can quickly look up names. This can take a while, so depending on how much of a hurry you're in, pick yes or no.


Starting and building a project

Most IDEs, including wxDev-C++, are built around the concept of a 'project'. This is a collection of source (.cpp) files, header (.h) files and other things, which together make up a single program that you're working on. In particular, whatever source files are in a project are what wxDev-C++ will compile and link into your project's executable file.

Let's start by giving it a test. Go to File > New > Project. You'll get a list of several project types. The difference between these is how wxDev-C++ sets the project options up for you, so it's best to pick what fits what you're planning to do. A Console Application is a simple text-mode program using a console/terminal window (known to some as a 'DOS window' even though it isn't DOS at all), and a wxWidgets project is a project using the wxWidgets graphical interface library. For now, pick 'Console application'.

After you choose where to save the project file, you'll get a window with some C++ code already in it, and on the left is the project browser. The project browser lets you easily see what's in your project, which for now is just one file, main.cpp. By convention, the file where the 'start' of a C++ program is is usually called main.cpp, though some people use different names for their files. In the end, it doesn't matter what you name them, as long as you can easily understand where everything is.

The code that is on your screen now is already a working C++ program, even though it won't do actually do much yet. Go to Execute > Compile (or press Ctrl+F9) to build the project. This program is so simple it compiles almost instantly, but big projects can take over an hour to build! At the bottom of the screen, you'll see error messages if anything goes wrong. This program was error-free, so nothing shows up, but if there were any errors, this is where you'd find them. Now, give the program a test run by going to Execute > Run (or press Ctrl+F10). A console window will pop up with the text 'Press any key to continue . . .'. Press a key and the program ends again. Not very advanced, but at least it shows that everything is working correctly. In future, if you want to build and then run the program straight away, you can use Execute > Compile & Run (or press F9).

Now that everything is set up and working correctly, there's one little quirk about wxDev-C++ you should know about. When working with console programs, the console window will often disappear as soon as your program is finished, without giving you a chance to read what's on the screen. This is what the system("pause"); line in the example program is for: it pauses the program just before it exits, giving you a chance to read everything. If you'd like to get rid of the 'Press any key' message while still pausing the program, use system("pause > nul"); instead.


Next steps

Now that everything is working correctly and you know how to start, build and run projects, you can start learning the C++ language itself and what it has to offer. Here are several links you might find useful:

C++ language resources
<a href="http://www.cplusplus.com/" target="_blank">http://www.cplusplus.com/</a> - Has a very good tutorial to explain the language, and information about most of the standard library.
<a href="http://www.cppreference.com/" target="_blank">http://www.cppreference.com/</a> - Information about the C++ standard library and other things.
<a href="http://www.unc.edu/depts/case/pgi/pgC++_lib/stdlibug/ug1.htm" target="_blank">http://www.unc.edu/depts/case/pgi/pgC++_lib/stdlibug/ug1.htm</a> - Information about the C++ standard library classes and containers.
<a href="http://www.parashift.com/c++-faq-lite/" target="_blank">http://www.parashift.com/c++-faq-lite/</a> - Advanced questions answered about C++.

Programming libraries and toolkits
<a href="http://www.wxwidgets.org/" target="_blank">http://www.wxwidgets.org/</a> - wxWidgets homepage, a must-read if you want to start writing GUI programs. The wxWidgets library is already included with wxDev-C++, so you can get started right away!
<a href="http://www.libsdl.org/" target="_blank">http://www.libsdl.org/</a> - The Simple Directmedia Layer (SDL) is a simple and easy-to-use library for writing multimedia programs such as games. But be aware, this is not included with wxDev-C++, so you need to compile the library before you can use it. Not recommended for beginners!
<a href="http://msdn2.microsoft.com/en-us/library/aa139672.aspx" target="_blank">http://msdn2.microsoft.com/en-us/library/aa139672.aspx</a> - Win32 library reference. Win32 is the standard programming interface for Windows programs, written in C, and is included with wxDev-C++. I don't recommend programming with this alone (wxWidgets is much easier) but for more advanced stuff you'll probably need it sooner or later.
<a href="http://www.winprog.org/" target="_blank">http://www.winprog.org/</a> - Some resources for writing Win32 programs, if you really, REALLY want to. :P

Compilers and IDEs
<a href="http://www.mingw.org/" target="_blank">http://www.mingw.org/</a> - MinGW compiler. If you decide you want to set this up manually, download the binutils, gcc-core, g++, mingw32-make, mingw-runtime and w32api packages, and unpack them all into a folder somewhere.
<a href="http://www.codeblocks.org/" target="_blank">http://www.codeblocks.org/</a> - Code::Blocks IDE. Personally I think this is much better than wxDev-C++, as it offers far more features for advanced use. wxDev-C++ is good for beginners, but after a while you do start to notice what it lacks. If you decide to give this a try, be sure to install MinGW as well, and get the latest nightly build of C::B, not the ancient 1.0 RC2 version.
<a href="http://www.microsoft.com/downloads/details...44-c96d69c35dec" target="_blank">http://www.microsoft.com/downloads/details...44-c96d69c35dec</a> - Free version of the Visual C++ command-line compiler and .NET tools (not the IDE!). If you decide to use this instead of MinGW, be sure to also download the Microsoft Platform SDK.
<a href="http://msdn2.microsoft.com/en-gb/express/aa975050.aspx" target="_blank">http://msdn2.microsoft.com/en-gb/express/aa975050.aspx</a> - The Visual C++ 2005 Express IDE. This is a smaller, free version of Visual C++ 2005. Use this if you can't stand wxDev-C++ or Code::Blocks. You need to register to use it though, but it comes with the compiler included (though not the Platform SDK!).
<a href="http://msdn2.microsoft.com/en-gb/vstudio/aa700755.aspx" target="_blank">http://msdn2.microsoft.com/en-gb/vstudio/aa700755.aspx</a> - Microsoft Platform SDK. Download and install this if you're going to be using the free VC++ compiler or VC++ 2005 Express.


Of course, if there's any questions you'd like answered, there's the helpdesk staff. Although for questions about this tutorial specifically you can just reply to this topic.

Happy programming!

Edited by CodeCat, 21 July 2008 - 15:04.

CodeCat

Posted Image
Posted Image

Go dtiomsaítear do chód gan earráidí, is go gcríochnaítear do chláir go réidh. -Old Irish proverb

#2 walkingGhost

    Visitor

  • Member
  • 26 posts

Posted 29 March 2008 - 21:37

I just wanted to suggest an alternative to WxWidgets: Qt. It was my own approach to C++, and even as a beginner you can get amazing results within a relatively short period of time (some weeks, if you start from scratch like me), thanks to the good documentation and the intuitive and consistent naming of the functions/classes.

If you have understood the basics of C++, and you want to create Graphical Interfaces for your programms, it may be the perfect tool for you.

Framework
Qt- An opensource GUI- Framework, with the best documentation I ever saw.
I'd suggest to download the installer, not the sources (for beginners).
It installs not only librarys, header files and compiler (MinGW, if required) but also many examples and the Qt- Assistant (which is very helpful!).

IDE
As IDE I recommend Eclipse (you need the C/C++ version), preferably with the Qt- Eclipse- Integration. (or alternatively, if you are a bit more experienced, a combination of notepad and command prompt).
After the installation of eclipse and the Qt-integration kit you just have to specify the location of your Qt- Installation (window->preferences->Qt) before you can start.
If you decided to use the editor/ commandprompt- combo, you should first read the documentation of the qmake- command with your Qt- Assistant.
(Qt uses platformindependent, human- readable project files (*.pro), and the qmake- command processes these to makefiles for your compiler).

Library for diagrams, spectrograms, etc.
As a Qt- extension for drawing diagrams, charts... Qwt is the library of choice.

Math- library
Gnu Scientific Library
Contains special functions (Bessel, erf, gamma...), solvers for linear equations and polynoms, algorithms for optimization problems (e.g. simulated annealing), transforms in all flavors, physical constants and much more.
Also remember rule 1: never, ever implement time- critical functions on your own if somebody already did- try to understand the ones from others instead, you will benefit much more from that.

Questions?
PM me!

Important note:
All these tools are GPL- licensed and thus not only free for noncommercial use, but also opensource.
If you intent to sell your programs, use other toolkits/libraries you shouldn't need to read articles like this :dope:.
currently: Posted Image

#3 bourne6

    Newbie

  • Member
  • 2 posts

Posted 18 July 2008 - 00:35

Ya hey! Could you fix the link to the program please. It's dead.

Here is what im talking about:

"Start off by downloading wxDev-C++ from here."

That link is dead

#4 CodeCat

    It's a trap!

  • Gold Member
  • 6111 posts

Posted 21 July 2008 - 15:05

Fixed, but you could've easily figured out the right link yourself. :unsure:
CodeCat

Posted Image
Posted Image

Go dtiomsaítear do chód gan earráidí, is go gcríochnaítear do chláir go réidh. -Old Irish proverb

#5 CodeCat

    It's a trap!

  • Gold Member
  • 6111 posts

Posted 05 December 2008 - 12:04

Unpinned now that the wiki has a better version.
CodeCat

Posted Image
Posted Image

Go dtiomsaítear do chód gan earráidí, is go gcríochnaítear do chláir go réidh. -Old Irish proverb



1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users