Thursday, November 29, 2018

C++ Variables and Constants




Many times, we need to store some data in our program. How to accompany this? So, Hello guys, Welcome to this article.
In this article, we are going to learn about C++ Variables, Data-type of Variables, Declaring Variables and Declaring Constants. So let’s begin.

1. C++ Variables:

- A variable is a name given to a memory location.
Why do we need to give a name to a memory location?
- Every data we want to store will be stored in the memory. The memory is basically a group of cells and each of these cells have its own address associated with it. Whenever we request memory to store data, memory is allocated. So whenever we want to manipulate that data stored in that memory location, we will need the address of that memory location. There are certain problems associated with this.
i) We will need to always type the whole address whenever we want to access that particular data. Practically, the memory addresses are very long hexadecimal numbers, so it is tedious to remember the address.
ii) Generally, we manipulate a big amount of data in the programs, so it is practically impossible for anyone to remember the memory location address for each individual data and use them without getting confused.
iii) The memory is allocated from RAM whenever requested, and as the name suggests, it is Randomly Accessed Memory, and hence the memory address we will get for data is generated randomly, and it will never follow any pattern.

Here, a variable comes to rescue.
Whenever we want to store data, we declare a variable with a name. What happens here is that a memory block is allocated to store the data. The memory block allocated is given a name which is our variable name. So whenever we want to access that data, we just use the name of that variable. Isn’t it easy to remember a variable name rather than remembering that long hexadecimal address?


This is what basically a Variable is.

2. The Data Type of Variable

The Data type is a means to tell the compiler the type of data that a variable will store.
Why do we need to tell the compiler the type of data?
- Basically, different size of memory is allocated for the different type of data. So whenever we declare variable, we need to tell the compiler the type of data, so the proper amount of memory will be allocated to store that variable.

There are 5 data types supported in C++.
i) int
- It is used to indicate an integer type data. 2 bytes of memory is allocated for int type variable in a 32-Bit Operating System.
For example, the age of the user will be stored in an int type variable.
i) char
- It is used to indicate a single character type data. 1 byte of memory is allocated for char type variable in a 32-Bit Operating System.
For example, Gender of the user will be stored in a char type variable.

iii) float
- It is used to indicate floating point number type data. Floating point number means a number with decimal a point. 4 bytes of memory is allocated for float type variable in a 32-Bit Operating System.
For example, the area of the circle will be stored in a float type variable.

iv) double
- It is used to indicate floating point number type data with double precision. Double precision means it will contain more digits after decimal point. 8 bytes of memory is allocated for float type variable in a 32-Bit Operating System.
For example, the mass of electron will be stored in a double type variable.

v) void
- It indicates no data. It is not used as a data type for a variable. It is basically used as the return type for functions.



3. Declaring Variables

We shall follow the following syntax to declare a variable.

Syntax:
<data type>  <name of variable>   =   <initial value> ;

Syntax Explained:
-data type is the type of data which our variable will hold. E.g., int, char etc.
-name of variable is the name that we give to variable.
-initial value is an default value for variable, it is optional.

For example, if we want to declare a variable to store the age of the user, we will write it as
int age=18;
Here the initial value is optional and the variable can be also declared as:
int age;
And now we can access the Age of User by just using age variable.

But it is a good habit to always initialize variables while declaring.




4. C++ Constants and Declaring Constants

A constant is basically a data whose value cannot be altered later.
Syntax:
const   <data type>  <name of constant>  = <initial value>  ;
The keyword “const” keyword tells that the data is constant. An initial value is must while declaring a constant.
For example,
const float radius=3.2;
Now the value of radius cannot be altered anywhere in the program.



So this is a sample program to demonstrate Variables and Constants.


The Output of this program is,



So this was all about C++ variables and constants.


______________________________________________________

if any doubt or feedback please feel free to contact : 
    rahulrb15899@gmail.com

    +91 7775919753  (WhatsApp)

Tuesday, November 27, 2018

Hello World Program in C++



In this tutorial, we are going to code our first program: Hello World Program. This program is considered as the most common, even learning of any programming start with this program. In this program we are going to simply print “Hello World” on Console (Black Command Prompt Screen).
So let’s move ahead…

This is how a C++ Hello World Program looks like:




Now we will discuss all the statements one by one.

Consider,

As discussed in the previous article, firstly we include Header File which we will need in our program. “#include” is a Preprocessor Directive to include header files in our program. Preprocessor Directives are the line of code that starts with “#”, they are called so because they are executed by Preprocessor before the compilation of code (it’s obvious that we will need to include files before the process of compilation). “#include” is used to include files and “#define” is used to define Macros. There are some more Preprocessor Directives, but this two are most commonly used.
The name of the Header File to be included is provided within Angular Braces (like #include <iostream>) or within Double Quotes (like #include “iostream”).
The iostream header file provides us with stuff for Basic Input and Output. Iostream stands for the  input-output stream.

Consider,



Line No. 6, it defines our main function which is the entry point of our program.
The “int” is a data type which specifies what our main function is going to return at the end. It is called as the return type of function (here main function). We will learn more about them in the Function’s article.
“main” is the name of the function.
“main()”, this parenthesis specifies that main is a function.
Every function starts with an Opening Curly Brace “{“ and ends with a Closing Curly Brace “}”.

Consider,



Here we do our task of printing Hello World.
“std::cout” is the object of class ostream which is used to print on screen in C++, it is defined in “iostream” Header File. Since it belongs to the standard class of C++, we need to add “std::” in front of it.
“<<” is called as Insertion Operator, which insert the string “Hello World” inside cout object. The cout object is further connected to monitor via streams. It passes the string to monitor to print it.
“;” the semicolon ends all the statements in C++. It is used to indicate the end of a valid executable statement.

Consider,



Line No. 11: “return 0” statement returns 0 from main(). Operating System calls main() of each program, and in C++, main() must return an integer to Operating System and our return statement does the same.
The semicolon at end completes the return statement.

Consider,



“}” brace completes our main() and completes our Program too.

So now, let's Compile and Run our program to see results. The Output of Program Comes like:





So…Bingo, we have done with our Hello-World Program.

______________________________________________________
if any doubt or feedback please feel free to contact : 
    rahulrb15899@gmail.com

    +91 7775919753  (WhatsApp)

C++ Tokens


Today, we are going to learn about C++ Tokens. Let us first define Tokens.

Tokens: 
The smallest, individual, and meaningful units of a program are called Tokens. Here meaningful means that they are well understood by Compiler. They are generally separated using spaces.

Types of Tokens:  

There are 6 types of tokens in C++.
1. Keywords                        2. Identifiers                        3. Constants or Literals
4. Operators                        5. Punctuators        6. Separators
We will discuss them all in this article.


1. Keywords:

- Keywords are the reserved words in any language. These are the tokens whose meaning is already explained to the compiler.

- A keyword cannot be used as an Identifier, i.e., it cannot be used to give the name to variables, function’s, classes and etc.
- For example, int, double, if, switch, auto etc. are keywords in C++.
- According to C++17, there are 73 tokens in C++, some of them are listed below:


2. Identifiers:


- Identifiers are the name given to a part of the program. The part may be a variable, a constant, a function name, a class name, etc.
- The sub-part of the program is accessed in the program using that identifier.
- There are certain rules for identifiers. These are given below:

i) An identifier cannot be a C++ keyword.
  For example, ‘auto’ is not a valid identifier, because it is a keyword in C++.

ii) An identifier can contain small letters, alphabet letters, numbers; but it cannot contain special characters and spaces (only ‘_’ can be used).
For example, Rahul123 is valid, but Rahul#123 is not a valid identifier.

iii) An identifier cannot start with a number; it must start with a letter or underscore ‘_’.
For example, 123Rahul is not valid, but _123Rahul and Rahul_123 are valid identifiers.


3. Constants or Literals:

- These are the token, whose value cannot be changed.
- They are also called as Literals.
There are several types of constants:

i) Integer Constants, these are the numbers without a decimal point. They are further classified as:
  A. Decimal Integer Constants: This includes 0 to 9 digits (Ten digits system) with optional positive or negative sign. For example: 10, -99 etc.
   B. Octal Integer Constants: these includes 0 to 7 (Eight Digits System) and starts with a ‘0’. For example: 026, 054 etc.
   C. Hexadecimal Integer Constant: these includes 0 to 9 and A to F digits (16 in total) digits and starts with ‘0x’. For example: 0x32, 0x2F etc. Generally, memory addresses are in Hexadecimal numbers.

ii) Floating Point Constants: these are the numbers with a decimal point and an optional positive-negative sign. For example, 2.3, 6.899 etc.

iii) Character Constants: these are the single characters enclosed within single quotes. For example, ‘a’, ‘Z’, ‘9’ etc.

iv) String Constants: string constants are the multiple characters enclosed within double quotes. For example, “Rahul”, “Programming”, “c”, etc.


4. Operators

-Operators are the tokens which operate on operands.
- Depending on the number of operands they are classified as Unary Operators (Operate on single operands like ++, -- etc.), Binary Operators (Operate on two operands like +,-,*, / etc.) and Ternary Operators (operates on Three operands like Conditional Operator “:?”).
- They are further classified on the basis of their type, but we will cover them in a separate article Operators.
- These are some common Operators in C++.


5. Punctuators: 

These are the tokens used for special use in a programming language. For example {,}, [,], (,) etc.


6. Separators: 

These are the tokens used to increase readability of program and separating tokens. This includes white spaces, tab spaces etc.

So, these are the tokens in C++. We will need them frequently from here onward.

_________________________________________
if any doubt or feedback please feel free to contact : 
    rahulrb15899@gmail.com
    +91 7775919753  (WhatsApp)


Saturday, November 24, 2018

Structure of a C++ Program and IDE



In this article, we are going to cover: Program files system of C++, Structure of a C++ program and something about C++ IDE.
So, let’s move ahead…


1. Program files system of C++


- Basically, when we write code, the code is saved in a “title.cpp” file; the object code (binary code)    is stored in “title.o” file, and the application file is a “title.exe”. This all files and the arrangement    of these files in directories is C++ program files system.
- So, let’s learn the process of creating these files. The code we write is called as Source code, it is       written in a “title.cpp” file called Source code file. When we successfully compile source code, a     “title.o” file is generated which has all the binary code of our program and some references to the   external files associated with our program (like graphics libraries files) and this file is called Object   Code file. Concurrently a “title.exe” file is created which is nothing but our main executable file.

                                 

 That’s it.

2. Structure of a C++ program


- Every programming language has its own program structure; likewise, C++ also has a program     structure.
- A program structure is nothing but a Skelton, which all the codes of that particular language  follow. It specifies the way and area to do the correct stuff on the correct place (stuff like external files inclusion, variable declaration and etc.).
- So here is the structure of C++ program, a C++ program is divided into following sections:


Header File Declaration Section:

-In this section, we include the required header files for the program. Header files are nothing but the files which contain implementations of all the functions and the macros which we use in the program.

-also in this section, we declare macros, macros are nothing but constants which represent something statement or value. 

These codes are just for example, no need to understand. We will learn them ahead.

-we will understand both the things practically in next the article with code.

Global Declaration Section:


-this section has a lot of stuff, like the declaration of global variables, declaration and, definition of functions etc.

Class Declaration and method definition section:


-in this section we declare all the classes being used in the program and implement them by defining all the member function of each class.


Main Program section:


 - This section contains our main function. The Main function is the entry point for any C++ program.
- In the main function, we declare objects, variables, do calling other function. In short, all our program logic is in the main function.


3. C++ IDE


- To build a C++ application, we need following things: Text Editor to write Code, Compiler to compile code, Linker to link files, Debugger to debug the program and many more things. If we use them separately, it’s going to take much time of us. While developing software, we repeatedly need to test code by executing it. So it will consume a lot of our time to go through this applications separately.
-here IDE comes to rescue. IDE stands for Integrated Development Environment. It integrates text editor, compiler, debugger, and others all in a single software package, and make it easy for us to compile and run the program with just one-click. This is what an IDE is.
-C++ has a lot of IDE’s in the market, of which Visual Studio C++ is most famous and used for developing huge programs in Windows.
-Code::Blocks is a Cross-platform IDE and light and efficient too. For small-scale programming and practice, DevC++ is also a good choice.

-There are many more IDE available in the market for every platform (even Android like CppDroid) and there are Online IDE’s also (like C++ Shell). You can search for them, and choose as per  your choice. I personally recommend and use Code::Blocks.
                      

- I will request you guys to install an IDE in your device so you can practice as we go ahead. One cannot learn programming without practice.


So this much for this article, we are going to do Code in next article, Coding a "Hello World" Program in C++.

----------------------------------------------------------------------------
Below are some links given for you to download IDE's :

Download Sololearn and Programming with fun on Mobile on the way:


----------------------------------------------------------------------------

if any doubt or feedback please feel free to contact : 
    rahulrb15899@gmail.com
    +91 7775919753  (WhatsApp)

Friday, November 23, 2018

Some must to know Facts about C++



So, first of all, WELCOME to this C++ Tutorials. In this tutorial series, we are going to cover C++ concepts, from Basics to Advance.
First of all let us know something about the language we are going to learn, which everyone should know. We are going to discuss History of C++ (don't worry, it's not much boring), what is so special with C++? And the most important and most interesting thing, which every newbie is curious about, Where C++ is used?
If you don't understand some terms, don't worry, you will learn them all as we go ahead in course.
So let's move ahead…

1. History of C++
- C++ is developed by Bjarne Stroustrup in 1983.

Bjarne Stroustrup


- It is derived from the C language. Stroustrup took Language semantics of the C language and Object Oriented features from Simula 67 language and developed “C with Classes”, which was further named “C++” by Dennis Richie.

C + OOP = C++
.
2. What is special with C++?

- C++ is a General Purpose Language, means it can be used to create a variety of applications for the variety of platforms.



- C++ is a Middle-Level Language, i.e., it is close to Humans as well as computer also. It can be easily understood and learned by humans as well as it is close to computer architecture, hence easily converted to computer understandable format.


- It is a Procedure Oriented (POP) as well as Object Oriented (OOP) language. We will discuss this later on in future. 

- As C++ is close to computer architecture, programs made with C++ are very efficient and fast as compared to other programming languages.

- C++ is the best to understand all computer programming approaches. It is the best language to understand OOP.


- One who knows C++ can easily learn a new programming language because most of the programming languages are derived from C++ or use a similar structure like C++.

3. Where C++ is used?
C++ is used to develop a variety of application, which includes:

Games: as C++ is a memory and performance efficient language, most of the games on this planet are developed in C++. For example, GTA-V is totally based on C++.



Graphics Applications: Most of the code of Adobe applications are written in C++.

Operating System: C++ is widely used to write most of the code for Operating Systems, like Windows.

Graphics Libraries: many of the graphics libraries are developed in C and C++, like SDL, SFML etc.

Compiler: C++ is also used to write compilers.
Programming Languages: C++ is most widely used to develop new programming languages. For example, Java is developed in C++.



So this was all one should know about C++. I hope you enjoyed. This was only to cheer up you guys before we start real learning. In next articles, we gonna learn some real programming related stuff.

----------------------------------------------------------------------------
if any doubt or feedback please feel free to contact : 
    rahulrb15899@gmail.com
    +91 7775919753  (WhatsApp)

C++ Variables and Constants

Many times, we need to store some data in our program. How to accompany this? So, Hello guys, Welcome to this article. In this arti...