Skip to main content
Engineering LibreTexts

2.1: The "main" concern....

  • Page ID
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)

    \( \newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\)

    ( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\)

    \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)

    \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\)

    \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)

    \( \newcommand{\Span}{\mathrm{span}}\)

    \( \newcommand{\id}{\mathrm{id}}\)

    \( \newcommand{\Span}{\mathrm{span}}\)

    \( \newcommand{\kernel}{\mathrm{null}\,}\)

    \( \newcommand{\range}{\mathrm{range}\,}\)

    \( \newcommand{\RealPart}{\mathrm{Re}}\)

    \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)

    \( \newcommand{\Argument}{\mathrm{Arg}}\)

    \( \newcommand{\norm}[1]{\| #1 \|}\)

    \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)

    \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    \( \newcommand{\vectorA}[1]{\vec{#1}}      % arrow\)

    \( \newcommand{\vectorAt}[1]{\vec{\text{#1}}}      % arrow\)

    \( \newcommand{\vectorB}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \( \newcommand{\vectorC}[1]{\textbf{#1}} \)

    \( \newcommand{\vectorD}[1]{\overrightarrow{#1}} \)

    \( \newcommand{\vectorDt}[1]{\overrightarrow{\text{#1}}} \)

    \( \newcommand{\vectE}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash{\mathbf {#1}}}} \)

    \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)

    Basics of C++

    C++ is a general-purpose programming language and widely used nowadays for competitive programming. It has imperative, object-oriented and generic programming features. C++ runs on lots of platform like Windows, Linux, Unix, Mac, etc. C++ is  also an object-oriented programming – As the name suggests uses objects in programming. Object-oriented programming aims to implement real-world entities like inheritance, hiding, polymorphism, etc in programming. The main aim of OOP is to bind together the data and the functions that operate on them so that no other part of the code can access this data except that function. All of these pieces will be discussed in this course.

    Learning C++ programming can be simplified into a few steps:

    • Writing your program in a text-editor and saving it with correct extension(.CPP, .C, .CP)
    • Compiling your program using a compiler or online IDE
    • Understanding the basic terminologies.

    The “Hello World” program has become the traditional first program in many programming courses. All the code does is display the message “Hello World” on the screen.So, to help us begin our journey here is the code:

    // Simple C++ program to display "Hello World" 
    // Header file for input output functions 
    #include <iostream> 
    using namespace std;
    // main function - 
    // where the execution of program begins
    int main() 
        // prints hello world 
        cout << "Hello World" << endl; 
        return 0; 

    Lets take a look at this code and learn a bit about the terminology we will be using in this course:

    1. // Simple C++ program to display “Hello World” : This line is a comment line. A comment is used to display additional information about the program. A comment does not contain any programming logic. When a comment is encountered by a compiler, the compiler simply skips that line of code. Any line beginning with // is a C++ comment OR multi line comments can be in between /* and */  .
    2. #include: In C++,  all lines that start with pound (#) sign are called directives and are processed by preprocessor which is a program invoked by the compiler. We have discussed these concepts in the previous chapter. The #include directive tells the compiler to include a file and #include <iostream> . It tells the compiler to include the standard iostream file which contains declarations of all the standard input/output library functions.
    3. int main(): This line is used to declare a function named “main”, this function returns an integer data type. A function is a group of statements that are designed to perform a specific task. Execution of every C++ program begins with the main() function, no matter where the function is located in the program. So, every C++ program must have a main() function.
    4. { and }: The opening braces ‘{‘ indicates the beginning of the main function and the closing braces ‘}’ indicates the ending of the main function. Everything between these two comprises the body of the main function. Braces enclose a code block, we will see more of these as we go along.
    5. cout << “Hello World” << endl;:  This line tells the compiler to display the message “Hello World” on the screen. This line is called a statement in C++, specifically this is an output statement. Every statement is meant to perform some task. A semi-colon ‘;’ is used to end a statement. Semi-colon character at the end of statement is used to indicate that the statement is ending there. The std::cout is used to identify the standard cout function, we will talk a bit more about this shortly. Everything followed by the character “<<” is displayed to the output device. Notice there are multiple << in the statement. Users can build complex output statements in this manner.
    6. return 0; : This is also a statement. This statement is used to return a value from a function and indicates the finishing of a function. This statement is basically used in functions to return the results of the operations performed by a function.
    7. Indentation: As you can see the cout and the return statement have been indented or moved to the right side. This is done to make the code more readable. In a program as Hello World, it does not hold much relevance seems but as the programs become more complex, it makes the code more readable, less error-prone. Therefore, you must always use indentations and comments to make the code more readable.

    It is key that you get the fact that EVERY C++ program needs a main() function. Without main() your code won't even compile. Main should always be declared as returning an int, but it is allowable to return a different type, such as void.

    Adapted from:

    "Writing first C++ program : Hello World example" by Harsh Agarwal, Geeks for Geeks is licensed under CC BY-SA 4.0

    This page titled 2.1: The "main" concern.... is shared under a CC BY-SA license and was authored, remixed, and/or curated by Patrick McClanahan.