Structure of C++ Program
Basic Structure of a C++ Code
Here, in this page we will discuss the basic structure of C++ Code.
In C++, a program is divided into the following three sections: Standard Libraries Section, Main Function Section and Function Body Section.
C++ Code Representing the Structure
using namespace std;
cout << "Hello, World!";
In the program above, #include is an example of a header file.
Definition – Header files contain definitions of Functions and Variables, which is imported or used into any C++program by using the pre-processor #include statement. Header file have an extension “.h” which contains C++ function declaration and macro definition.
Here iostream contains the definitions of input output statements, that are used to print or scan at run time.
Namespace is a feature added in C++ and not present in C. A namespace is a declarative region that provides a scope to the identifiers (names of the types, function, variables etc) inside it. Multiple namespace blocks with the same name are allowed. All declarations within those blocks are declared in the named scope.
Just remember, name spaces are used to allow re-declaration of variables with same name in a scope.
using namespace std; – enables this.
By design, every C++ program must be enclosed in a function int main() by default is the first function that is called automatically at run time.
Here the function main() is of return type int, thus return 0; at the very end causes the program to execute without any error.
cout <<“Hello, World!”;
Here, << which is called as insertion operator, it basically inserts the object on its right to its left and cout is predefined function to print value on the screen.
Keywords in C++
Keywords are predefined reserved identifiers that have special meanings. They cannot be used as identifiers in your program. The following keywords are reserved for C++. Names with leading underscores.
A C++ identifier is a name used to identify a variable, function, class, module, or any other user-defined item. An identifier starts with a letter A to Z or a to z or an underscore (_) followed by zero or more letters, underscores, and digits (0 to 9).
- Name can’t start with a digit, only underscore(_) and alphabets and no whitespaces
- Identifiers are case sensitive, value and Value are two distinct identifiers.
- You can’t use a keyword as an identifier.
Comments in C++
- Single line Comments – // I will only extent to single line
- Double line comments – /* i will span to various ……………… different lines*/
Comments are used to increase the understandability enhancing readability of the code. Generally, comments are written to help other coders, who are working on same application and want to understand the code written by someone else.
Introduction to Classes and OOPs Concept
- OOPS Concept in C++
- Class v/s Object
- Structure of a C++ Program
- Access Modifiers
- Constructors and Destructors
OOPs Advanced – 1
- Types of Inheritance in c++
- Upcasting and Downcasting
- Operator Overloading (detailed)
- Constructor Overloading