Programming Features of C++
To understand the features used in a C++ Program, let us consider a example to print a string.
#include<iostream> //header file
using namespace std;
int main( )
{
cout<<"C++ Program"; //C++ statement
return 0;
} //end of program
In the above program, we can see various features used in a program. These features as follows:-
Comments:-
- Comments includes the lines which we want to ignore or do not want to consider in our program.
- Comment may start anywhere in the line.
- The double slash // comment is used for a single line comment.
- For multiline comments /*___*/ is used.
Output Operator:-
- In the above program, the ouput statement is,
cout<<"C++ Program";
- Here, "cout" is an identifier which is a predefined object that represents the standard output stream in C++.
- The standard output stream represents the screen.
- Operator << is called the insertion or put to operator.
- It inserts the contents of the variable on its right to the object on its left.
The iostream file:-
- A C++ program typically contains the pre-processor directory statements at the beginning.
- All C++ Programs begin with a #include directive that includes the specified header file contents into the main program.
- The header file iostream should be included at the beginning of all programs that use input/output statements.
- Using the appropriate header files depending on the contents of the program and implementation.
Namespace:-
- The namespace concept was introduced by ANSI C++ committee.
- For using the identifier defined in the namespace scope using a directive files is must.
- std is the namespace where ANSI C++ standard class libraries are defined.
Return type of main( ):-
- In C++, main( ) returns an integer type value to the operating system. Hence, every main( ) in C++ should end with a return(0) statement, otherwise an error might occur.
- The default return type for all functions in C++ is int.
For understanding more features, we are taking another program, which is as follows:
#include<iostream>
using namespace std;
int main( )
{
int no1, no2, sum, average;
cout<<"Enter two numbers";
cin>>no1;
cin>>no2;
sum=no1+no2;
average=sum/2;
cout<<"sum="<<sum<<endl;
cout<<"average="<<average<<endl;
return 0;
}
We can see that there are some other features too, they are as follows:
Variables:-
- In our program, we have used four variables which are no1, no2, sum, average.
- Before the variables are used in program, they must be declared.
Input Operator:-
- Here, input statements are
cin>>no1;
cin>>no2;
- It is an input statment and causes the program to wait for the user to type in a number.
- cin is also an identifier which is a predefined object in C++ that represents the standard input stream.
- The operator >> is known as extraction or get from operator.
- It takes the value from the keyboard and assigns it to the variable on its right.
Cascading of input/output operators:-
- In the above program,we have two statements as,
cout<<"sum="<<sum<<endl;
cout<<"average="<<average<<endl;
- Here, the insertion operator << is repeatedly used to display the result.
- First it sends the string "sum=" and "average=" to the cout and then it sends the value of sum and average.
- Atlast, it sends the newline character, so that the next output will be in the newline.
- Hence, multiple use of << or >> in one statement is called Cascading of input/output operator.
Awsome✌✌
ReplyDeleteThank you...!!!
Delete