Funding for 'IT Lab' Project, Phase 1: Progress of sticker sales. Purchase a sticker to help us reach our target.Updated: 2010-02-28 11:53
C++ with Barney!

by Thameera Priyadarshi Senanayaka
An introduction
[You're Ted and you decide to learn some C++ from your friend Barney]
You:
Well, here I am. So you agreed to teach me some C++?
Barney: Ah, Ted, yes. You're at the right time. Come on let's get
started at once. Take a seat here.
Barney: Well, as you must have already heard, it's a programming
language, developed in 1979 by a person called Bjarne Stroustrup.
You:
God, how did you ever manage to pronounce that name? Is C++ that difficult?
Barney: :-) Don't worry, it's quite easy to learn. It'll be a piece
of cake for you to grasp the basics, but will need some toil to master it, just
like any other language. But if you meet me regularly, at least once a month,
you will find you are mastering your C++ continuously.
So would you like to hear about how C++ came into the picture?
You:
Mmm, yes, if you won't make it long.
Barney: I'll try to be brief. In the very early days people had to
program using zeros and ones, which was very, very difficult. Then came a
programming language called Assembly which was a better replacement, but that
was also too hard for the normal folk like us to learn. Then came several more
higher level languages like Basic and Fortran. Still problems remained. It was
at this time a language called C was introduced. Using C was quite easy, and
programmers willingly adopted it. C used a programming style called structured
programming. But when the programs began to get complex it was very hard to
maintain the code. The solution was object orientation.
You:
Object what?
Barney: Object orientation. I will explain that another time, but
until then, the object oriented programming concept made the code more
manageable and was the best solution for the problems programmers faced at the
time. But it was not possible to implement this in C. So what should have they
done?
You:
Well, they could have created another language.
Barney: Yes, but it will be hard to adapt to a brand new language,
so they thought of extending the existing C to cater to the object oriented
requirements. That is how C++ came. In fact, C++ is an expanded and enhanced
version of C.
You:
Hmm, C++ seems to be an age-old language. Why should I learn it anyway? There
are a dozen of new languages like C# and Java.
Barney: The truth is that most of these languages including the two
you mentioned have been written using C++. They are more user-friendly and
easier to learn, but not as powerful as C++ is.
You:
What do you mean by powerful?
Barney: Just to show an example, Windows was written using C++.
You:
I totally get it.
Barney: Java and C# have more or less the same structure as C++ so
if you learn C++ you can easily learn those, and if you already know any of
them, learning C++ will be easier. But you won't be able to do all the things
you can do with C++ using the others.
So first of all, let's see what you are going to need if you're going to program in C++. Ted, what's your operating system?
You:
Windows. But I also use Linux frequently.
Barney: Okay. In the case of Windows you will need to download a
C++ compiler. Just google it and you will have many. For example Microsoft and
Borland have their own compilers. But I'd recommend to use Microsoft Visual C++
Express. You can download it for free from the MSDN site.
You:
Great. And for Linux?
Barney: The basic C++ compiler for Linux is GNU C++, but we call it
g++ for short. If you're not sure whether you have it installed, just type:
g++ -v
in the console and it'll give you an error message if you don't have it. In that case you'll have to install it using the command:
sudo apt-get install g++
You will find some other IDEs also, like NetBeans.
You:
Right. Now that I have a compiler what's next?
Barney: If you are using Visual C++ you need to open it and create
a new project by using File -> New
-> Project. Then you get a dialog box like this. Select Empty Project in the General section.
Type in a name for the project and press Ok.
In the Solution Explorer which appears on the left side of the screen, right click on Source Files and choose Add -> New Item. Now another box appears and select C++ File (.cpp). Now give a name for the file and click Add. You get an empty text area where you can put your code.
You:
Okay, but what was that ‘.cpp’ thing?
Barney: .cpp is the
extension you give to the source codes of the files written using C++. Just
like you use .htm for web pages.
You:
I get it.
Barney: If you’re using Linux, you can use a text editor like gedit to type in the code and you can
compile it in the console. I’ll show you how to compile, but first let’s try
our first C++ program. You agree?
You:
Hell, yes! :-D
Barney: Right. As always, let’s make our first program a Hello
World! First we’ll do this using Visual C++ but later I’ll show you how to do
it on Linux console.
You:
Okay.
Barney: In the new CPP file you just created enter this code. It
will look like greek at first, but soon these will be crystal clear to you.
Here’s the program:
#include<iostream>
using
namespace std;
int
main()
{
cout<<"Hello
World!"<<endl;
system("pause");
return 0;
}
This is the source code of the program. Now you have to compile and run it to see the result.
You:
That means we have to compile this first and run it, right?
Barney: Yes. But in many IDEs like Visual Studio, you can simply
run it and it will do the compiling itself. But I recommend that you compile
first, otherwise the warnings that the compiler makes will probably go
unnoticed.
So to compile this go to the menu and choose Build -> Build solution. Or you can simply press F7. By the way, remember that we’re using Visual C++ here. In other IDEs this may change a little. But you can always notice this command since it will be labeled Build or Compile.
You:
Right. So here we go. I just compiled it. What are these weird messages that
come in the bottom of the screen?
Barney: Don’t worry about them at the moment. All you need to focus
on now is the last two lines which says that we have 0 errors, 0 warnings and
building the project was succeeded. If this was not the case you’ll need to
debug your code, that is to check the code for errors. You don’t have any
errors now.
You:
So far so good.
Barney: Yes, now we will run the program. You choose Debug -> Start debugging or simply
press F5. You can also click this green arrow in the toolbar.
Voila!
You:
:-O Wow! That’s amazing! Why that Press
any key to continue thing?
Barney: I’ll explain. Before that we’ll see how we can do this in
Linux.
You:
Alright.
Barney: You have to enter the code in a text file. For this you can
type:
gedit hello.cpp
in the console and gedit will show up. You enter the code as you did earlier, but make sure you DON'T type the system("pause") command. I'll tell you why later.
#include <iostream>
using namespace std;
int main()
{
cout
<< "Hello World!" << endl;
return
0;
}
So what's next?
You:
Think that we need to compile it.
Barney: Exactly. You can compile it using the command g++ followed by your filename. Like
this:
g++ hello.cpp
You:
No weird lines seem to appear.
Barney: Yes, if it compiles correctly it won’t display anything.
Otherwise it will show the errors including where the error is. If you check
the directory where you added you will notice that a file named a.out has been created. This is the
executable file g++ has created from your source code. To run this type:
./a.out

See?
You:
Yeah, that's great! That Press any key to
continue thing is no more there.
Barney: Yes. Now that you know how to compile and run a C++
program, let's see what we have actually written in the code. What is the first
line of code?
You:
#include <iostream>
Barney: Yes, this command tells the compiler that we will need the
header file called iostream and asks
it to load it to the memory.
You:
What's a header file?
Barney: Simply put, a header fileis like a library in C++ that
contain various commands. To use these commands you need to import these
libraries. The cout command we used
is from this iostream library. Get
it?
You:
Totally. Next line is using namespace
std;
Barney: Yes, it tells the compiler to use the namespace called std. A namespace is a place where you
organize commands. You'll learn about this another day. Just remember that if
you did not use this command cout and
endl wouldn't have worked. In that
case you would have to use std::cout and
std::endl. So what do you choose?
You:
Sure enough, writing using namespace std;
is better than having to put std:: all the way in the program.
Barney: You're really smart. What's the next line?
Barney: This is the beginning of the body of the program. We
declare a function called main. This
is the entry point to the program. int means
that we will return an integer value as the result of this method. You will see
that shortly.
Now this open curly bracket { just after int main() marks the beginning of this main method. You can see this closing curly bracket } at the end. It tells that the main method ends there. Right?
You:
Right. The next line is cout<<"Hello
world!"<<endl;
Let me guess. It tells the compiler to write Hello world in the screen?
Barney: Absolutely. The <<
is called the output operator. It tells to make the elements to the right of
it, the output of the thing to the left of it. cout is the console output. That is your screen. So this command
prints Hello world in the screen.
You:
What about that <<endl; part?
Barney: endl is used to
tell the compiler to go to a new line. Now that "Hello world!" and endl
are two separate elements, they have to be separated by <<. You will grasp this when you
move on.
You:
Hey, you have put quotation marks with hello
world but they're not printed. How come that?
Barney: Quotation marks are used to indicate that Hello world! is a string and not a
variable or function. I'll teach you this better next time when we'll be doing
variables.
You:
Okay. The next line is system("pause").
But you didn't use that in the Linux console.
Barney: If you omitted this line when using Visual C++, the output
will appear in the screen and vanish in a moment. To prevent this we ask the
program to pause until the user presses a key. Actually pause is the DOS command used to pause things in the good old days
of DOS. This is a system call and we use system
to indicate this.
We don't need to use this in the console because the output won't vanish anyway. Also pause won't work in Linux.
You:
I see. So what is this last line: return
0;
Barney: You remember we promised that we will return an integer
value when we exit from the main method. Well, here's that integer. 0 tells the
operating system that the program exited normally without errors. If the
program did not run to this point and crashed it would have returned a non-zero
value.
Barney: Wait, didn't you notice any other thing that we didn't talk
about in the code?
You:
Mmmmmm, the closing curly bracket?
Barney: No, we talked about that. Anything else?
You:
Ah, these semicolons!
Barney: Yes. Every statement in C++ should end with a semicolon,
except for several special ones. You will learn this on the go, but remember, if not otherwise stated, you
always have to use a semicolon after each statement. Otherwise it will give a
compile error.
You:
Alright.
Barney: So what do you think? I think it's time we finish today's
lesson.
You:
Certainly. That was so great! Thank you very much!
Barney: And if you face any problems while installing the compiler,
compiling it or any other thing we discussed about, drop me a mail at thameera123@gmail.com and I'll try to
answer it to the best.
You:
Shouldn't that be barney123?
Barney: No, Ted, just do as you're told! Bye!
Note: The explanations have been simplified for the beginners to grasp
the facts easily.
Post new comment