C++ Program
Before starting the abcd of C++ language, you need to learn how to write, compile and run the first C++ program.To write the first C++ program, open the C++ console and write the following code:
- #include <iostream>
- using namespace std;
- int main( ) {
- int age;
- cout << "Enter your age: ";
- cin >> age;
- cout << "Your age is: " << age << endl;
- }
#include includes the console input output library functions. The getch() function is defined in conio.h file.
Using namespace std, tells the compiler to use standard namespace. Namespace collects identifiers used for class, object and variables. NameSpace can be used by two ways in a program, either by the use ofusing statement at the beginning, like we did in above mentioned program or by using name of namespace as prefix before the identifier with scope resolution (::) operator.
Example : std::cout << "A";
void main() The main() function is the entry point of every program in C++ language. The void keyword specifies that it returns no value.
cout <<, is used to print anything on screen, same as printf in C language. cin and cout are same asscanf and printf, only difference is that you do not need to mention format specifiers like, %d for int etc, in cout & cin.
Standard end line (endl)
The endl is a predefined object of ostream class. It is used to insert a new line characters and flushes the stream.Let's see the simple example of standard end line (endl):
- #include <iostream>
- using namespace std;
- int main( ) {
- cout << "C++ Tutorial";
- cout << " Javatpoint"<<endl;
- cout << "End of line"<<endl;
- }
C++ Tutorial Javatpoint End of line
getch() The getch() function asks for a single character. Until you press any key, it blocks the screen.
No comments:
Post a Comment
Please write your view and suggestion....