C++ tutorial №1: First program on C++

   Hi guys. Today I am going to meet you with C++ and we will write a short and simple program. So let's start from basic information about C++.    C++ is one of the most popular programming languages and is implemented on a wide variety of hardware and OS platforms. C++ is an object-oriented language. It was created by Bjarne Stroustrup in 1983 as a next step after C. The difference between C and C++ is that C++ is provided with new functional like virtual functions, creating objects, multiple inheritance, operator overloading, templates and exception handling and other.    Now I show you a simple greeting program and explain how it works. Below you can see the source code.
  1. #include "iostream" // in this part of code we include libraries which we need in our program
  2. #include "string" //library to describe type "string"
  3. #include "conio.h" //library for _getch() function
  4.  
  5. using namespace std; // we use namespace "std"
  6.  
  7. int main() //main function of the program
  8. {
  9. string name; //variable with our name
  10. cout<<"Type here your name:"<<endl; //output
  11. cin>>name; //input of name
  12. cout<<"\nHi, "<<name<<". I am your first C++ program!"; //output
  13. _getch(); //wait for pressing the button
  14.  
  15. return 0; //return the result of function main
  16. }
   Here you can see main function of the program which is called main. This function should be in every C++ program. Here it returns result of int type. (Type int contains ineger numbers. More about types we talk later.) In our example it takes no arguments, because the brackets after word main are empty.Block of every function is seperated by pair of such brackets"{}".    Beside the main function program also can have an area where we include lybraries,header files and other parts of the project we need in our program. Our example needs such libraries as iostream (standard input/otput library), conio.h (describes function _getch()) and string.h (describes string type).    In line "string name;" we create a new variable of type string to keep the name. Cout and cin are basic operators for output and input respectively. We use namespace std to prevent writing "std::cout", "std::cin" and "std::endl"(go to new line).    Function _getch() stops a program until we press some button.    Operator return returns the result of our function main. Now we can see how it should work:
  1. Enter the name:
    Input of the name
  2. Result of the program
    Input of the name
   If you have some questions please write. See you later.

Add new comment