View and vote on the article here: Your First C++ Program
Your First C++ Program| Category | | | Summary | | | Body | Your First C++ Program
C++ was developed in the early 1980's by Dr Bjarne Stroustrup at the AT&T Bell Labs in New Jersey. It was originally called "C with classes" as it derived its roots from C. From then on, C++ has become the most popular object oriented programming language in the world. This article will help you to understand and write your first C++ program.
For historical reasons, the first program to be developed in any programming language, should be the legendary "Hello, world!" program. This is a simple program that displays the words on the screen. Here I assume that you are using the Turbo C++ compiler by Borland, since it is one of the most popular compilers. Only minor changes will be necessary to run the program in other compilers.
Open the file TC.EXE in the BIN sub-directory of the default TC directory. Most probably the path would be:
C:\\TC\\BIN\\TC.EXE
Click on FILE and then NEW. Start writing the program code in the window which opens.
Program Code :-
#include<iostream.h>
#include<conio.h>
void main()
{ clrscr();
cout<<"Hello, world!";
getch();
}
Now save this by clicking on FILE and then SAVE. Enter the file name, say something like TRY.CPP. Now click on COMPILE and compile and run the program. On the output screen, you will see:
Hello, world!
A file called TRY.EXE would have been created and you can run this to see the output again and again.
Now let us try and understand the program.
The first two lines, that is,
#include<iostream.h>
#include<conio.h>
tell the compiler to include the Header files 'iostream' and 'conio'. The .h extension stands for 'header'. 'iostream.h' is used for 'cout' operation and the 'conio.h' is used for the functions 'clrscr()' and 'getch()'.
void main()
signifies where the compilation begins. Here 'void' means that the main() function does not have a return data type.
clrscr()
is a function used for clearing the screen.
cout<<"Hello, world!"
is used for outputting text on the console.
getch()
is a function used to get a character from the screen.
Every statement must be terminated by a ';' which signifies the end of the statement. The getch() function is used so that the user can see the output, as the program waits till the user enters any character. If this statement is not used, the program will run and then will immediately return to the editor window.
The curly braces { and } signify the starting and the ending of the main() function.
This article was just meant as a very basic introduction to the vast world of C++ programming. Hopefully you understood something, if not all (which I would be very surprised if you did!). Read whatever you can get your hands on and practice a lot, and you will, in a decent amount of time, become a proficient C++ programmer.
Written by Elastic Reality (8 July 2004)
Member of CAU Knowledge-Bank Tutorial Writers |
|
This article was imported from the CyberArmy University site. (original author: Elastic Reality)
There are no replies to this post yet.
|