Introduction to C++

Azimkhan
The Startup
Published in
7 min readAug 24, 2020

C++ is a cross-platform language that can be used to create applications. It’s an extension of C language. It’s a middle-level language rendering its advantage of programming low-level and high-level applications. It’s one of the predominant languages for the development of all kinds of technical and commercial software.

C++ is an Object-Oriented Programming language and follows the OOPs concept of Data Abstraction, Data Hiding, Encapsulation, Polymorphism, and Inheritance. The language uses real-life concepts to develop a program. Being an Object-Oriented Language helps to make maintainable and extensible programs. The programs made from this language are easy to modify and easy to understand.

C++ has rich library support allowing easy access to functions that are very common for developers to use. It also has an extensive collection of header files necessary for a program to work. There is always a need of a “main()” function for the program’s main functional body which stores the commands to be first executed.

C++ isn’t a very hard language for as long as you approach it in a stepwise manner. The language uses easy to understand commands and the codes are kind of user-friendly, so it’s easy to work on a C++ application.

The very basic program of C++ language is to print “Hello World!”, for which the code is as follows :-

Now you all might be looking at this program and thinking,” what is this?”, well, let’s get to some explanations.

Header Files

In the starting 2 lines, there are two different “Header Files”. Here, “#include” is used to give a command to the computer to include the header files given in brackets and “<iostream.h>” and “<conio.h>” are those header files. Header files are necessary components of a program, without them, the program won’t work. Header files are a library of inbuilt (already built within) functions, when a header file is used in a program, all the functions of that header file’s library (used within the program) work. In the above program, “cout” and “endl” are inbuilt functions of “<iostream.h>” header file and “clrscr()” and “getch()” are inbuilt functions of “<conio.h>” header file.

~> The function “cout” performs printing of the text written within the symbols “ ” . It’s a function of “<iostream.h>” header file and is used with insertion operator “<<”.

Syntax for cout :- cout<<” “; (if any text were input in-between “ “, then the program will output the text)

~> The function “endl” is used to jump to another line after printing the text given in “ “ or just leave a line without printing any text. It’s also a function of “<iostream.h>” and is always used with “cout”. An alternative of “endl” is “\n” , which is written within symbols “ “ .

Syntax for endl :- cout<<endl; (This will leave a line without printing anything)

cout<<”Hello”<<endl; (This will jump to another line after printing “Hello”)

Syntax for \n :- cout<<”\n”; (This will leave a line without printing anything)

cout<<”Hello \n”; (This will jump to another line after printing “Hello”)

~> The function “clrscr()” clears the output last executed and provide an empty space for new execution. It’s the function of “<conio.h>” header file. It’s normally placed at the start of a program to get an empty space but it can be placed anywhere in the code as per user’s need to get the most favorable output.

Syntax for clrscr() :- clrscr(); (semicolon is must, otherwise there will be an error)

~> The function “getch()” kind of holds the output screen for the user to see the output. It’s the function of “<conio.h>” header file. If this function is not used then the output screen will appear only for a moment of milliseconds and then disappear, like a flash or blink. It’s normally placed at the end of the program but it can also be placed anywhere in the code as per user’s need to get favorable output.

Syntax for getch():- getch(); (semicolon is must, otherwise there will be an error)

Main Function

main() is a function that is must for a C++ program to work, without this function, the program won’t work at all because this function holds the main body of the program. The execution of a program starts and ends at the main(), without main() there won’t be any start for the execution of the program. A program can contain many functions but one of them has to be main(), but if a program has only one function then it must be main().

In front of the main(), the word “void” refers to the “Return type” of this function. The return type of main() can be int, float, double, and so on, depending on the user’s choice. “Return type” refers to the kind of value which the program will give back after the execution of the whole program. Here, “void” indicates that no value is needed to be returned while “int”, “float”, “double” and so on indicates that a value is needed to be returned and the returned value should be of the type specified in front of main().

Body of Program

Body of the program consists of working/executable part of the program, given by the user to get a favorable output. Within the body, declaration of variables is made, commands for printing and input is given, values are assigned, and so on. The body of the program is basically a user’s idea put in words and commands to get an output.

Semicolon (;)

Semicolon is one of the most important parts of C++ programming as it marks the end of a line or code. If not put at the end of required lines, then during compiling and execution there will be errors.

Starting and Ending Parenthesis

Starting parenthesis “ { “ and Ending parenthesis “ } “ marks the start and end of the body of the program or function. Within these parentheses, the written code will be executed and the code will be the part of only that particular function (if another function is made other than main() ).

Output of the code given above :-

Well, let’s summarize all the topics covered in this explanation :-

~> C++ :- A middle-level language used to program low-level and high-level user-friendly programs/applications.

~> Header Files :- A library of inbuilt functions used within the program.

~> Main Function :- main() is a necessity for the program, which contains the body and executable part of the program.

~> Body of Program :- The executable/working part of the program, which when executed gives an output.

~> Starting parenthesis “ { “ & Ending parenthesis “ } “ :- To mark start and end of the body of the program.

This sums up all the explanation for today.

If you have any questions or queries regarding any topic, then please do let me know in the comments. I will try my best to solve that problem of yours.

Be tuned for the next part which will cover a new program with some new topics.

--

--