Back to Top

Monday 30 March 2015

Begginer's Guide to C++- Chapter 1: Introduction to C++

This is the first chapter to a long upcoming series, aimed at making you all learn C++, in quite an easy and fun way. You will be able to learn almost all concepts, without getting tired or bored.

We will be using Turbo C++ compiler for the proceedings. You can search for this "Turbo C++ by Neutron", it will get you to a ready to install package. Also, some programmers will say that the conventions, along with the compiler that I use are outdated, and programming has become much more liberal, but I think that developing your skills on any compiler is fine and, you will learn to modify it, to suit other compilers. What is important is to learn to think like a programmer and break your objective into smaller targets/modules. Through my guide, you will learn just that!

C++ is a case-sensitive language, i.e. the words “doctor”,  “Doctor” and “DoCtOr” will be recognised as different by the compiler, so make sure you keep that in mind.

Basic Terminologies:
  1. Keyword: These are the commands that the compiler understands. Thus these are special words that make the compiler do certain things.
  2. Variable/Identifier: These are storage locations, eg. x=5, where x is called the variable (just like mathematics).
  3. Operator: +,-,*,/ etc. Nothing worthy to explain about them, they do what their symbol suggests.
  4. Datatypes: These are a way to specify the kind of variable you are making. Eg- int - integer, char - character.

Rules for naming variables:
  1. It should not be a keyword.
  2. It should not have any special character like ' ', '*', etc. However, '_' is allowed while naming.
  3. The name should not begin with a number.

Operators: The following operators are available in C++,
Algebraic Operators:
1. +,-,*,/...
2. % - It finds the remainder from 2 numbers. Eg- 3%2 will give answer as 1.
      32%10 will give answer as 2.
Conditional/Relational Operators: Used to check conditions.
3. <,>,<=,>=...
4. == - Used to check equality. Consider this statement as "Is this equal?". If you write x==3, you are asking " Is x equal to 3?". I will elaborate on this later, when its use comes up.
Logical Operators:
5. && - And operator, used to join 2 statements with "and". It is just like the usage in English - "I want to eat mangoes and apples." Here we need both apples and mangoes to be given, in order for me to eat them.
6. || - Or operator, used to join 2 statements with "or". It is also used similar to the way we use it normally - "I want to eat mangoes or apples". Here we need at least an apple it a mango, or even both for me to eat them.
7. ! - Not operator, negates the current input. Eg- != will check if two given values are not equal.
Increment and Decrement Operators:
8. ++ - Adds 1 to the variable's value. Eg- If a variable p is equal to 10, p++ will make it equal to 11.  You can write it both as p++ and ++p, it does not matter.
9. -- - Similar to the above operator, but instead subtracts 1 from variable's value.

Datatypes: Datatypes are of the following types:
  1. int- Used to define integer type variables. Range- -32768 to 32767.
Syntax- int variablename;
  1. float- It is used to define a variable with decimal values. Eg- 10.43. Range- 3.4E +/- 38.
Syntax- float variablename;
  1. double- It is similar to float, but it has a greater range. Range- 1.7E +/- 308.
Syntax- double variablename;
  1. char- It declares a character type variable. Characters are enclosed in ‘’ (Single quotes), and are also represented by ASCII Codes. ASCII codes range from - -128 to 127.
Syntax- char variablename;
  1. void- It denotes an empty set of values. I will explain it later. No variable of this type can be declared.

Special Characters: There are some characters that represent a special meaning. Some are as follow -
  • /n - New Line
  • /t - Tab
  • /a - CPU beep sound
  • /' - Prints '
  • /" - Prints "
                            
Rules for variable declaration:
Syntax:
datatype variablename1,variablename2;
You can also initialize a variable by a value while declaring it.
Eg.- int x=10; (initializes an integer variable and designates its value as 10)
There is one rule of thumb, that you must always remember- You can initialize 1 and only 1 variable while defining it. This means you can provide value to only 1 variable while declaring.
Eg- int x=10,y=20;   is invalid
      int x=10,y;          is valid
      int x=y=10;         is invalid
We always write a=b+c; to store the value of b+c in a. b+c=a is wrong, and the compiler will report an error. Eg- It is always "Tom plays a guitar" rather than "Guitar plays a Tom".



Comments:
Comments help in noting down what statement serves what purpose. They are of two types-
  1. Single Line - For writing a single line comment, first put a “//” and then write the comment. Eg- int x=10; //Initializing x
  2. Multiline - Multiline comments begin by “/*” and end at “*/”.
Eg- /* Hey, howdy?
This is a multi-line comment
I hope you understood the concept*/

So friends, I take my leave here. Please go over the article again, and try to memorize the whole thing because these are the most basic and fundamental concepts in C++, and you will need them at every step.