Struct C++, Program Structure

Struct C++, Program Structure

Learning Struct  C++ programming can be simplified into:

  • 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 best way to learn a Struct C++ programming language is by writing programs. Typically, the first program is a program called “Hello World”, which simply prints “Hello World” to your computer screen.

// 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";
     
    return 0;
}

Output:

Hello World

Terminology of Program

Let us now understand every line and the terminologies of the above program:

  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 ‘//’ without quotes OR in between /*…*/ in C++ is a comment.
  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. 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. using namespace std: This is used to import the entirety of the std namespace into the current namespace of the program. The statement using namespace std is generally considered a bad practice. When we import a namespace we are essentially pulling all type definitions into the current scope. The std namespace is huge. The alternative to this statement is to specify the namespace to which the identifier belongs using the scope operator(::) each time we declare a type.
  4. int main(): This line is used to declare a function named “main” which returns data of integer 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.
  5. { 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.
  6. std::cout<<“Hello World”;:  This line tells the compiler to display the message “Hello World” on the screen. This line is called a statement in C++. Every statement is meant to perform some task. A semi-colon ‘;’ is used to end a statement. Semi-colon character at the end of the statement is used to indicate that the statement is ending there. The std::cout is used to identify the standard character output device which is usually the desktop screen. Everything followed by the character “<<” is displayed to the output device.
  7. 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.
  8. 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.

Struct C++ Comments

As noted above, comments do not affect the operation of the program; however, they provide an important tool to document directly within the source code what the program does and how it operates.

Struct C++ supports two ways of commenting code:

// line comment
/* block comment */ 

The first of them, known as line comment, discard everything from where the pair of slash signs (//) are found up to the end of that same line. The second one, known as block comment, discards everything between the /* characters and the first appearance of the */ characters, with the possibility of including multiple lines.

Let’s add comments to our second program:

/* my second program in C++
   with more comments */

#include <iostream>

int main ()
{
  std::cout << "Hello World! ";     // prints Hello World!
  std::cout << "I'm a C++ program"; // prints I'm a C++ program
}
Output
Hello World! I’m a C++ program

If comments are included within the source code of a program without using the comment characters combinations ///* or */, the compiler takes them as if they were C++ expressions, most likely causing the compilation to fail with one, or several, error messages.

Using namespace stdIf you have seen C++ code before, you may have seen cout being used instead of std::cout. Both name the same object: the first one uses its unqualified name (cout), while the second qualifies it directly within the namespace std (as std::cout).cout is part of the standard library, and all the elements in the standard C++ library are declared within what is called a namespace: the namespace std.

In order to refer to the elements in the std namespace a program shall either qualify each and every use of elements of the library (as we have done by prefixing cout with std::), or introduce visibility of its components. The most typical way to introduce visibility of these components is by means of using declarations:

using namespace std;

The above declaration allows all elements in the std namespace to be accessed in an unqualified manner (without the std:: prefix).

With this in mind, the last example can be rewritten to make unqualified uses of cout as:

// my second program in C++
#include <iostream>
using namespace std;

int main ()
{
  cout << "Hello World! ";
  cout << "I'm a C++ program";
}

Output

Hello World! I'm a C++ program

Both ways of accessing the elements of the std namespace (explicit qualification and using declarations) are valid in C++ and produce the exact same behavior.

Generation of Computer, What is Computer

Follow This Link For Health And Beauty Tips

C++ syntax, C++ syntax PDF

Leave a Reply

Your email address will not be published. Required fields are marked *