C++ Basics And Inro To Mysterious Powers Of The Inventor Of C++


Hi. In this post i will teach you "How to make c++ talk to you".....or technically speaking - "How you can print a string in C++".
This post explains each and every basic things that will give you wings to code C++.

Now here is a very basic program which i created to explain you.




 

 

Program Features -

Now C++ is a collection of functions{if you don't know about it doesn't matter right now lets proceed.}.
Here in the above program main() is the function from where execution of the program starts.
So if you want to print anything then you are forced {by mysterious powers of Bjarne Stroustrup} to write it in main().
Note that C++ is a language free from exeptions, which means it ignores carriage returns and white spaces.
And C++ statements end with semicolons.

Comments -

In C++ there is a new symbol for comments - "//" which is for a single line comment and
                      - "/*..........*/" is for multiline comments.
Example-
    Single line
    // At start I know how much boring it is to learn C++

    Multiline   
    /* But later on
        you will realise
            how easy it to learn it.*/

The OutPut Operator -

The only statement in my program i have used is the output statement so that you may not press the "Go Back" button
from your browser.
The statement -
        cout<<"C++ rocks.";

causes the string{collection of words if i'm not wrong} in quotation marks to be displayed on the screen.
The identifier{name of the operator} cout is a predefined object that represents the standard output stream in C++.
Here the standard output stream represents the screen.
Note that it is possible to redirect the output to other device {I know what you are thinking but remember cYbEr cRiMe is a crime.}
Note that we can also use printf() defined for C to print the statements but it better to maintain the spirit of C++ and use "cout<< ;".

The Case With Iostream File -

In simple words all C++ programs starts with a #include directive that include the specified header file contents to the main program.
I have used the following directive in the program -
                            #include <iostream>
This directive causes the pre-processor to add the contents of the iostream file to the program.
It contains declarations or functioning of the identifier cout and the operator <<.
Some old compilers use the header file called iostream.h.
So again if you start writing anything in your program you are forced {by the mysterious powers of BJARNE STROUSTROUP} to include the iostream header file.

Namespace -

In this program the statement -
                using namespace std;
may hunt you several nights......it did to me and after 2 months i understood it's concept.
Now don't panic if you don't get it but we will talk about it later.
This is a new concept introduced by ANSI C++ standard committee which defines the scope of the identifiers that are used in the program.
Here std is the namespace where ANSI C++ standard class libraries are defined. The statement namespace will bring all the identifiers
defined in std to the current global scope.
Note that using and namespace are the keywords which you can't use as identifiers.

Return Type Of Main -

In C++, main() returns an integer type value to the platform {operating system}.
Therefore once again you are forced by......{by mysterious powers of no one!!} to include this statement at last of your program if you use - void main().
Unless it's necessary to include it if you use - int main().
Note that half of the year i kept on thinking why to use int and include one more statement but a good man told me it will cause no harm
and it's a good programming practice to include it.
From that day i started using it and suggest that you should also use it.

A Question For You All -

Write a program to find the average of the two numbers.
I know you can't do it {my fault} so i solved it to explain some concepts through example.
















Now in the program you noticed that i kept my words and did all the things that i explained you all till now.
So proceeding forward i have used many new statements here whose explanation is explained below.

Variables -

The program uses 4 variables num1, num2, sum, and avg. They are declared as type float {nums having decimal values are known as type float}
by the statement -
          float num1, num2, sum, avg;
Now you have to declare all the variables before using them in your programs.
The syntax for declaration is -
                data-type name-of-variables
Examples-
     int variable1;      //variable1 declared as integer type
     float variable2;    //variable2 declared as float type


Input Operator -

The statement -
        cin >> num1;

is an input statement and causes the program to wait for the user to type in a number.
The identifier cin is a predefined object in C++ that corresponds to the standard input stream.
Here the stream represents keyboard.

The operator >> is known as extraction or get from operator which takes the value from the keyboard and assigns it to the variable on it's right.
Now we can also use scanf() operation which was defined for C but again i would like to maintain the spirit of C++.

Few Words More -

So Now You All Know How To Create A Basic C++ program.
In The Next Post I Will Talk About Various Datatypes, Tokens, Keywords, and Many More Things.

Like, Comment and Subscribe To help me Survive.

{If there would have been a like button created by google for blogs.

And Don't Forget About "the mysterious powers of Bjarne Stroustrup"} 





Comments