Search This Blog

Sunday, 26 January 2014

Some Basic Concepts

Now that we have seen a C++ program in action, you must be excited and would want to know more about it. But let’s not rush it and start from the beginning. In this post, we will learn various important terms in C++ (and programming, in general), so that when we write a program, we know what we are doing.
·         Compiler: C++ is a compiler based language. A compiler is a software that converts the human-readable  C++ code to machine code that can be understood and executed by the computer. A Compiler is different from an interpreter, which is also used in some languages for the  same task, as it converts the whole source code (the code you write) to object program in one go, and lists the errors, if any, at once. There are many compilers available on the net for C++, the chief being Microsoft Visual C++, Intel C++ Compiler, Borland, and the now obsolete Turbo C++. A list of free C++ compilers can be found here.
·         Tokens: Tokens, by definition, are the smallest individual unit in C++. Tokens are of many types. These are:
1.       Variables/Identifiers: A variable is a portion of memory used to store a value. Each variable is identified by its respective identifier. An identifier is basically a unique name for a variable. There are few rules when it comes to declare valid identifiers:
·         It should begin with a letter. The rest of the identifier can use letters, digits or a sign of underscore.
·         Uppercase and lowercase letters are distinct.
·         Keywords cannot be used as identifiers.
2.       Keywords: These are the reserved words that have some-predefined meaning to the compiler. These keywords can only be used in the way they are defined. The C++ keywords are listed in the table below:
asm
continue
float
new
signed
try
auto
default
for
operator
sizeof
typedef
break
delete
friend
private
static
union
case
do
goto
protected
struct
unsigned
catch
double
if
public
switch
virtual
char
else
inline
register
template
void
class
enum
int
return
this
volatile
const
extern
long
short
throw
while
3.       Operators:          Operators are characters which do some specific task in C++. They are of following types:
·         Arithemetic
·         Relational
·         Logical/Boolean
·         Assignment
·         Increment/Decrement
·         Conditional
·         Comma
·         sizeof
Operators in C++ are an important topic and deserve a post of their own. Hence, I won’t go into details here.
4.       Constants: Constants are the values which never change during program execution. They are also called as literals. Constants can be classified as:
·         Integer Constant: eg. 123
·         Floating point constant: eg 123.45
·         Character constant: eg ‘a’
·         String Constant: eg “C++”
Constants would also be discussed in detail in another post very soon.
·         Comments: Comments are the text in the source code that is ignored by the compiler. These are written for the human readers so that they can understand what is happening in a program. Comments are included mainly for debugging purposes. Hence, even though they are not required, it is considered a good programming practise to insert comments in your program. Comments are also of two types:
1.       Single Line Comments: These comments begin with ‘//’(without the quotes). Everything written after // in the line is ignored by the compiler.
2.       Multi Line Comments: These comments begin with ‘/*’ and end with ‘*/’(without the quotes). Everything between those symbols is ignored.

That’s enough for today folks, we will study each of the above mentioned terms in detail in the following posts. Till then, Good bye, Happy Learning!

Saturday, 25 January 2014

"Hello World" C++ Program

A “Hello World” program is usually the first program that beginner programmers learn. It is essentially a program that gives an output : “Hello World”. It is used to showcase the basic syntax and structure of the programming language. The Hello World program in C++ is as follow:

Hello World Program

Code Explained:
1.       #include<iostream.h>:  This is called a pre-processor directive. A pre-processor directive instructs the compiler which library files to include in the program. A library file is a file which contains reusable pre-written code which can be used in other programs. The “iostream.h” file contains code concerning input and output.
2.        void main() : It is a function header. C++ programs are written using functions, which are modules which do some specific work. Functions help to modularize the code which is useful while debugging. The main function is the entry point of a C++ program where the program execution begins. The body of the function is enclosed within curly braces({}). We will study functions in detail in later chapters.
3.        cout<<”Hello World”; : This is the statement to print “Hello World”. cout is an output statement defined in the “iostream.h” library which is used to give an output. “<<” is known as cascading signs which shows that data is to be outputted. “Hello World”, ie the message to be printed is enclosed in double quotes, as it is a string, (we will study them in detail later). At the end, every C++ statement end with a ‘;’, which is also called terminator.
A Minor Change
If you followed every step and tried to compile and run the program, you must have noticed that the output console window just flashes in and out within a fraction of second. This is because C++ is a very fast language, and it displays the text and closes the program very quickly. In order to see the output, we have to make a little change.
Add this line before the main function:
#include<conio.h>
And this line at the end:
getch();
At the end, your program must look like this:

If you have followed the steps correctly, you will get the following output:


What does the modification do?
1.       #include  : “conio.h” is another library files. It stands for “Console Input Output”. It defines statements related to input/output using the console.
2.       getch() : It is a predefined function in conio.h. It pauses the program until any character key is pressed by the user on the keyboard.
What did we learn

Today, we learned the how to write the most basic C++ program and learned a few new terminologies. Next day, we start learning C++ in earnest, see what goes on behind the scenes of popular programs we use.

Friday, 24 January 2014

Introduction to C++

C++ is one of the most popular computer languages ever. If you are reading this on a computer, chances are that it has more than one software that is written in C++. It has been used in designing a variety of softwares, including but not limited to console applications, windows applications, video games smartphone apps and even operating systems. That's why even though 35 years have elapsed since it was first conceived, it is still an active member of the computer languages community.

C++ was developed in 1979 by Bjarne Stroustrup at Bell Laboratories. It was originally named C With Classes as it introduced object oriented approach to the language C.

C++ is a multi paradigm, object oriented, statically typed, mid level language. There are numerous implementations of C++, like Microsoft Visual C++, Intel C++ etc., each introducing something new in the languages. The language is constantly under development and new libraries are being added every now and then. Hence it is important to know C++ if you want to be a programmer worth your metal.

For these reasons, C++ is also taught in schools and in colleges in computer science courses. To know C++ is a great asset in programming community as it opens the gates to a new world for the programmers.

Hope you find your learning exciting. Good luck and Happy Learning!