Structure of a C++ Program

Structure of C++ Program

Structure of a C++ Program is divided into various sections, namely, headers, class definition, member functions definitions and main function.

The main purpose of C++ is to use the object oriented concepts to solve real world problems. Since C++ is improved version of C, we can also write traditional procedure oriented programs in it using the functions.

Structure of Object Oriented C++ Program

Structure of C++ Program
header files
definition of global constants and macros
class declarations
class member function definitions
main function

Example

Let's see a quick example.
// structure of c++ program example

#include<iostream>
using namespace std;


#define num 10

class Student
{
  string name; 
  int age;
  
  public:
    void accept()
    {
      cout << "Enter name of student: ";
      cin >> name;
      
      cout << "Enter age of student: ";
      cin >> age;
      
       age = age + num;
    }
    
    void display()
    {
      cout << endl << "Name: " << name << endl;
      
      cout << "Age: " << age;
    }
};

int main()
{
    const int c = 10; // this a constant
    Student s1;
    
    s1.accept();
    
    s1.display();
}

Output:

Enter name of student: John
Enter age of student: 20

Name: John
Age: 30

Header files in C++

#include is used to include any header file in the program. The statement #include<iostream> adds the code from header file named iostream.

What does the line using namespace std means?

std is an abbreviation for standard. “using namespace std" means we use the namespace named std.

What is namespace?

Namespaces are like containers which are used to organize code into logical groups to avoid name collisions. When you are dealing with multiple libraries then there can be many variable or functions of the same name, so there a chance that a collision can happen, but by using namespaces we can avoid such collisions.

So the statement using namespace std means that we use all the things (functions, varibale, constants etc) in std namespace. If we don't want to include this line of code in our program we need to use the things in this namespace like std::cout, std::endl.

What is the difference between header file and namespace?

The files which are stored actually in the file system and are included in your source program with the #include directive are known as header files.

Header files typically group pieces of code that are all interdependent parts of the same specific item together. For example, game might have header file for rendering all the graphic components.

On the other hand, namespaces are elements of the programming language. They don't exist as a file in the file system but rather as designation within the code which tells the compiler that certain things are within that namespace.

Namespaces normally group functions, classes/structs, user defined types of similar (but not necessarily interdependent) items.

For example, the std namespace in C++ contains all of the Standard Library functions and classes.

What are global constants?

#define is a preprocessor directive which is used to define things that are replaced throughout the whole file by preprocessor before compiling.

A preprocessor is a system software which replaces the things defined by #include and #define before compiling the program.

So in our program, the statement #define num 10 defines a string which is replaced by preprocessor. You can also define macros by using #define directive. We will see that later.

The statement const int c = 10 is used to declare a constant c with a value 10. c is a constant, its value cannot be changed. We can also achive the same using #define but the advantage of using const over #define is type checking is done for constants declared using keyword const. As it is like other normal variables so we can typecast it, also we can have a pointer to a constant. Although one disadvantage of constants is that they occupy extra memory but it immaterial because of the optimizations done by the compiler.

Class Declarations in C++

After global declarations, class template comes into picture. A class template can contain constants, variables and functions etc. The functions defined inside class template are called methodd because when we create an object of that class we call the method by using the object.

Look in the main() function we have declared an object s1 of class Student. With the statement s1.accept(); we are calling the method (function) which corresponds to that object. When we say we creating an object from a class template, we are creating a copy of that class template by actually allocating some memory.

For example, consider some text written in notepad file which tells us we need to store two strings and one integer and thats all, nothing else. It doesn't actually occupy any memory in your RAM because it is not the actual implementation but when you create an object from a class, memory is given to that object to store two strings and one integer. We will discuss about the member functions later.

main() function in C++

main() function is from where the execution of a program starts. Although there are some compiler which allow void as return type of main() function, it is recommended to be int returning 0 indicating successful execution of a program.

In main() function, all the core part of the program is written. Whatever you have written (classes, standalone functions, constants, macros etc), you utilize it here in main() function.