
Learn C++ - Introduction
|
My First C++ Program
Let us start our tour of C++ with a simple program. Here is a simple program which outputs a line of text. It’s output is shown in the bottom.
2
3
4
5
6
7
8
9
10
11
12
13
/*
* File: main.cpp
* Author: Gautam
* Created on October 16, 2011, 4:09 PM
*/
#include <iostream>
int main() {
std::cout<<"This is my first c++ Program.";
std::cout<<endl<<"and its very easy";
}
Output:
When this program is run, it prints the following outputThis is my first c++ Program.
and its very easy
Structure of the C++ Program and C++ features
Above program demonstrates several important basic features of C++ language. The structure of the program above is as follows:-
/*
* File: main.cpp
* Author: Gautam
* Created on October 16, 2011, 4:09 PM
*/
are comments in C++ language.
Using comments
Comments are a way of explaining what a program does. They are put after // or between /* */. Comments are ignored by the compiler and are used by you and other people to understand your code. You should always put a comment at the top of a program that tells you what the program does because one day if you come back and look at a program you might not be able to understand what it does but the comment will tell you. You can also use comments in between your code to explain a piece of code that is very complex. Here is an example of how to comment the Hello World program:
# include <iostream>
is a preprocessor directive. It tells the preprocessor to include the contents of iostream header file in the program before compilation. This file is required for input output statements.
main Function
int main()is a function. C++ program is a collection of functions. Every program in C++ begins executing at the function main(). Every C++ program has exactly one main function and a collection of other functions.
Keyword int in int main()
specifies the return type of a function. int specifies that the function main returns an integer value. default return type of main function in c++ is int so defining return type int is optional.
{}
The 2 curly brackets are used to group all the commands together so it is known that the commands belong to main. These curly brackets are used very often in C to group things together.
std::cout<<"This is my first c++ Program";
The above line is a statement in C++. A statement must always terminate with a semicolon (;) otherwise it causes a syntax error. This statement introduces two new features of C++ language, cout and << operator.
You will also notice that the words are inside inverted commas because they are what is called a string. Each letter is called a character and a series of characters that is grouped together is called a string. Strings must always be put between inverted commas.
We used std:: before cout. This is required when we use # include <iostream> .
It specifies that we are using a name (cout) which belongs to namespace std. Namespace is a new concept introduced by ANSI C++ which defines the scope of identifiers which are used in the program. std is the namespace where C++ standard libraries are defined.
Operator << is the insertion stream operator. It sends contents of variable on its right to the object on its left. In our case, right operand is the string “This is my first c++ Program” and left operand is cout object. So it sends the string to the cout object and cout object then displays it on the output screen.

Manipulators
Manipulators are operators used in C++ for formatting output. The data is manipulated by the programmer's choice of display. Some of the more commonly used manipulators are provided here below:
endl Manipulator
endl is the line feed operator in C++. It acts as a stream manipulator whose purpose is to feed the whole line and then point the cursor to the beginning of the next line. We can use \n (\n is an escape sequence) instead of endl for the same purpose.
setw Manipulator
This manipulator sets the minimum field width on output.
The syntax is:
setw(x)
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/*
* File: main.cpp
* Author: Gautam
* Created on October 16, 2011, 12:58 PM
*/
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
float basic, ta,da,gs;
basic=10000; ta=800; da=5000;
gs=basic+ta+da;
cout<<setw(10)<<"Basic"<<setw(10)<<basic<<endl
<<setw(10)<<"TA"<<setw(10)<<ta<<endl
<<setw(10)<<"DA"<<setw(10)<<da<<endl
<<setw(10)<<"GS"<<setw(10)<<gs<<endl;
return 0;
}
Output:
| Basic | 10000 |
| TA | 800 |
| DA | 5000 |
| GS | 6800 |
setfill Manipulator:
This is used after setw manipulator. If a value does not entirely fill a field, then the character specified in the setfill argument of the manipulator is used for filling the fields.
2
3
4
5
6
7
8
9
10
11
12
13
/*
* File: main.cpp
* Author: Gautam
* Created on October 16, 2011, 12:58 PM
*/
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
cout << setw(20) << setfill(`*`) << "w3schools.in" << setw(20) << setfill(`*`)<<"Test"<< endl;
}
Output:
********w3schools.in******************Test
namespace
using namespace std;If you specify using namespace std then you don't have to put std:: throughout your code. The program will know to look in the std library to find the object. Namespace std contains all the classes, objects and functions of the standard C++ library.
Example:
2
3
4
5
6
7
8
9
10
11
12
13
14
/*
* File: main.cpp
* Author: Gautam
* Created on October 16, 2011, 4:09 PM
*/
#include <iostream>
using namespace std;
int main() {
cout<<"This is my first c++ Program.";
cout<<endl<<"and its very easy";
}
Return Statement
Last line in the body of our first program is
return 0;
The keyword return is a special statement which is used to exit a function and return a value. The int in int main() is short for integer which is another word for number. We need to use the return command to return the value 0 to the operating system to tell it that there were no errors while the program was running. Notice that it is a command so it also has to have a semi-colon after it.
it's really nice...
|
C++ is an object oriented programming language created by BJarne Stroustrup in 1983, C++ is an extension of C programming and the programs written in C language can run in C++ compilers.