C++ programming language tutorial
Hello students,
Today we will learn about C++ Programming Language. Do you want to learn C++
programming language online? If yes, you are here at right place.
First of all
do you know what C++ Programming is?
C++ is a
programming Language. What is a Programming Language? A language which is used
to make programs. What is a Program? A program is a set of instructions. What
are the set of instructions? The set of instructions are used to guide computer
to do a specific task.
Now
understand these Definitions:
“A set of
instructions is called a program”
“A set of
programs combined together make up a software”
Let us
understand with this example.
We use many
software in our daily life say MS WORD.
In MS Word
We can type text and make it beautiful for applying different formatting
effects like BOLD, ITALICS,UNDERLINED,FONT COLOR,FONT STYLE etc. so each of
these effects have its own program(set of instruction to apply the effect
chosen) and all these programs combined together make up MS WORD.
So C++ is a
programming language which is used to create programs and software.
C++ was
developed by Bjarne Stroustrup.
Let us
create our first program to display “Hello World” on output device.
(I have used Turbo C++ compiler here)
Program 1.1
Line
1.
#include<iostream.h>
Line
2.
void main()
Line
3.
{
Line
4.
cout<<”Hello World”;
Line
5.
}
![]() |
Program 1.1 CODE |
![]() |
Program 1.1 output |
As we can
see that Our Output “Hello World” is present at Line 4 only then what other
things are doing here?
In C++ Programming
if we want to display any message on screen, we should use cout followed by inserter <<
followed by Message in double quotes in this case “Hello World” followed by ; (a semi colon known as statement
terminator or instruction terminator)
Fine then
what other lines are doing here??
In C++ Programming
to display message we need cout and the working of cout is defined in a header
file “iostream.h” so we need to include this “iostream.h” in our program to
make cout work. The syntax to include header files is
#include<headerfilename>
For our
program As on Line 1 #include<iostream.h>
We can
relate it with games where some special power of player doesn’t work until the
player makes a purchase or earns sufficient coins. As the player makes a
purchase or gets sufficient coins, the special power works. Actually the
program to make that special power work was not included earlier and as the
player makes a purchase or gets sufficient coins, the program to make special
power work is included in the main code of the game.
Ok then what
about Line 2 , 3 & 5?
In C++ Programming, every program starts with main
function() and ends with it. The syntax of
using main() function is
void main()
{
Different
programming statements go here.
}
What is main
function and what is void? We will discuss it later. For now Just Remember that
every C++ program has the following common structure:
Line
1.
#include<iostream.h>
Line
2.
void main()
Line
3.
{
Line
4.
Different programming statements go here.
Line
5.
}
Now we will
create another program to accept 2 numbers from user and display sum of the
numbers on screen
Program 1.2
Line
1.
#include<iostream.h>
Line
2.
void main()
Line
3.
{
Line
4.
int a,b;
Line
5.
cout<< “Enter two Numbers”;
Line
6.
cin>>a>>b;
Line
7.
cout<<a+b;
Line
8.
}
In the above
program refer Line 4.
In C++ Programming
int is a data type which is used to declare variables of Integer
type(Integers hold only whole numbers without decimal)
So we have
two integer variables declared a and b.
Refer Line 5.
Displays a message to the user on screen.
Refer Line
6. In C++ Programming to take input from user, we use cin followed by >>
(extractors) followed by variable which takes value. We can also take input on
two different statements like cin>>a; cin>>b; Using >> or
<< multiple times in a single statement is known as cascading.
Refer Line
7. We have not used “ ” (Quotes) because when we display values of variables or
calculations we do not use “ “. Had we used cout<<”a+b”; We would have
got the output as a+b not the sum of a and b. Though we can refine the
statement like this cout<<”a+b=”<<a+b; which gives output as:
a+b=15 [suppose
a has 10 and b has 5]
Comments
Post a Comment