Home Classroom C++ C++ with Barney! Class 2

C++ with Barney! Class 2

1148

Data types

[You're Ted and you decide to learn some C++ from your friend Barney. This is the second day of the class]

You:Hey, Barney! Here I am, again!

Barney:Ah, welcome back, buddy! So how was your C++?

You:Great! I tried both in Visual C++ and gedit. But I’d prefer Visual C++ because it’s so simple.

Barney:Yes, but if you need a more user-friendly IDE for Linux I recommend using NetBeans or . It works in both Windows and Linux.

You:Okay, I’ll try those. So what are we going to do today?

Barney:Right, let’s start with comments. Comments are the remarks you make in the C++ code. They have no effect on the compiler, so it just ignores them.

You:If it is not compiled why do we use comments anyway?

Barney:Well, comments are intended for the programmers, may be you or another programmer who would later view your source code. If not for comments, he would have to spend a considerable time trying to guess what your code is doing. It’s a very good practice to comment as much as you can.

You:I see.

Barney:There are two kinds of comments in C++. Line comments and block comments.

 

//This is a line comment

 

/* This

is

a block

comment*/

 

As you see, you can place several lines of remarks within a block comment. However you can achieve the same with several line comments. The choice of the type of comments is personal. But the thing to keep in mind is that you have to comment whenever you can.

You:Okay.

Barney:Now let’s move into C++ data types. Our previous programme had no use, except for printing some lines in the screen, which of course is an odd thing to use a computer language for.

You:I was also thinking that, but didn’t tell ya. ;-)

Barney:Yes, the real value of a computer programme comes when it can handle various data and give you outputs after analyzing them. In short, your programmes should be able to handle data. To be able to handle data, you need to know about certain data types that are used in C++. Can you tell some types of data you would want your programme to handle?

You:Mmmm, numbers, words, pictures?

Barney:Yes, you can handle all those in your programmes, although it is complex to handle pictures and videos. There are several data types that you can use to handle numbers. “int” can handle integers, “float” and “double” can handle numbers with decimal points, that is fractions, etc. For example, you can declare an integer called a with a value of 5 like this:

 

int a=5;

 

You:What is this a about?

Barney:You should give a name to every variable you declare. We call a a variable because its value can be changed later, that is, not a constant. We may define several of these variables in a programme, so all of them should have different names. We call these names ‘identifiers’.

You:So the identifier is a and it has a value of 5?

Barney:Absolutely. We can declare the variable first and assign a value later on, like this:

 

int a;

a=5;

 

You:I get it. So let me declare a float.

 

float a=4.5;

 

Am I correct?

 

Barney:There you go! But keep in mind, you cannot declare a variable whose identifier is already used, even if it is used with another data type. Okay?

 

You:Okay.

 

Barney:Good. Now take a look at this table. It lists some of the common numerical data types you can use in C++.

 

Data type Description Size Range
short int (short) Short Integer 2 bytes -32768 to 32767
int Long Integer 4 bytes -2147483648 to 2147483647
float Floating point number 4 bytes +/- 3.4e +/- 38 (~7 digits)
double Double precision floating point number 8 bytes +/- 1.7e +/- 308 (~15 digits)

 

As you see, you can use “short int” or simply “short” to store integers but it has a limited range. Why would someone need “short” when they already have “int”?

You:Mmm, perhaps it can be used to conserve memory because a short needs only two bytes?

Barney:There you go! If we want to store a large number of integers and if we know they’re in the range of “short”, that will be the best choice, since it takes only half the space as “int”.

 

Also, as you can see from the table, you can use “float” or “double” when you need to store numbers with fractional parts or very high values, but “double” has more range and precision than “float”. The range of a float is ±3.4 x 10±38 with a precision of 7 significant digits.

 

Alright, now you can store numbers in your programme, what more would you want to do with these numbers?

 

You:Well, I may need to add them, multiply them and do all sorts of arithmetic.

Barney:Of course, yes. Doing arithmetic with these variables is quite intuitive. To add 16 and 27, we would write a programme as follows:

 

int i = 16 + 27;

 

Now i will have a value of?

 

You:43!

 

Barney:You’re really good at arithmetic! Okay. Likewise, we can subtract, multiply, divide and find residues. Let’s see some examples:

 

int a=14-32;      //Gives -18

int b=7*8;        //Gives 52

int e=12/9;         //Gives 1, because int cannot hold fractions

float c=12/9;     //Gives 1.3333333…

int d=40%9;       //Gives 4, which is the residue when 40 is divided by 9

 

You:I see. It’s really simple!

Barney:Yes. You can also do this operations with variables, like this:

 

int p=22, q=6;

int r=p*q;

 

Now, Ted, how would you print the results of these operations on screen?

You:You could use “cout”, couldn’t you?

Barney:Exactly. We can use “cout” to print variables also.

 

int p=123, q=4;

int r=p%q;

cout<<r;

 

You can use a little formatting here, like this:

cout<<p<<” % “<<q<<” = “<<r;

What would be the output?

You:Mmm, I’ll write it down:

               123 % 4 = 3

Correct?

Barney:Yeah, now I think you understand how “cout” works. You can use the << operator to separate stuff within “cout”. Also now you may understand the use of double quotes.

You:Yeah, I think I get it.

Barney:You’ll soon forget these if you didn’t try them by yourself. So make sure you try out the things we learnt in different ways.

You :Okay.

Barney:Alright, Ted, let’s see some other types of variables. Suppose you need to use a variable to store logic or binary values, that is, true and false. You can use the “bool” data type for this. A bool can contain only two values, true or false, or, in other words, 1 or 0. Let’s see an example:

bool b1=true;

bool b2=fals;

bool b3=1;

 

You:What happened to b3?

Barney:You can use 0 and 1 instead of false and zero. 1 means true and 0 means false.

You:Ah, I see. That can be handy.

Barney:Yes, the bool is quite a useful type in many programming situations. You’ll learn more of this on the go. So, Ted, what other data types would you need to store in a variable?

You:Well, we surely need words to be stored.

Barney:Yes, though we don’t use the word “word” to call them. We call a single letter “a character” and a sequence of these characters “a string”. To store a character we can use the data type “char”.

 

char c=’A';

char d=’m';

You:What are the single quotes for?

Barney:You need to contain the characters within single quotes. Otherwise, if we had used

 

char d=m;

 

then, it means that we are equating “d” to another character variable “m”.

You:I get it.

Barney:But char can hold only one character. When you need to store more than one character in a single variable, you need to use the type “string”.

 

string myname=”ted”;

string myfriend=”barney”;

 

You:We should use double quotes there?

Barney:Yes, we must put strings inside double quotes. And also notice that I have used long identifiers. This is really useful to avoid confusion. It’s always recommended that you use descriptive identifiers. For example if we need two integers to hold the balance of this year and previous year in a shop, you could well use:

int m=2800, n=3220;

but it would be more reasonable to use:

int thisYearBalance=2800, lastYearBalance=3220;

 

Although these identifiers are long, they’re quite clear and will save you a lot of time when you return to check your source code some time later.

 

You:Yeah, I see it. So it’s better never to use one letter identifiers, I guess?

Barney:No, you always need short identifiers, especially for temporary variables which are used only in a limited scope of the programme. But you should use descriptive ones for variables which affect most parts of the programme and those which are of critical importance.

You:Okay.

Barney:And one more thing about strings. You can join several strings very easily using the + operator, like this:

 

string a=”Hey “;

string b=”there!”;

string c=a+b;

cout<<c;

 

This will print Hey there! in a single line in the screen.

 

You:It’s pretty simple.

Barney:Yeah, but sometimes string manipulation can get complex and this is an area every programmer should master. We’ll discuss this in detail later.

You:So these are all the variable types we have?

Barney:No, of course not. There are many more types. But for the time being the ones we learned today suffice for now. You’ll learn about more complex types like vectors, stacks, etc in the days to come. So, Ted, I think it will be enough for today. I need some time for myself.

You:Oh, alright! Let’s meet some other day again. Thanks a lot!

Barney:And if you have to send me any questions or suggestions please mail them to thameera123@gmail.com.

You:Sure! Bye, Barney!

Barney:Bye, Ted!

Comments

comments

Engineer. Loves tic-tac.

NO COMMENTS

Leave a Reply