Categories: C++

Syntax and Structure of a C++ Program

A specific template structure is used to write the C++ program. Let us see an example of “Hello World” program.

Example:
#include <iostream>
using namespace std;
int main() {
    // Write C++ code here
    cout << "Hello world!";

    return 0;
}
Output:
Hello world!
Header Files:

The C++ programming language defines several headers, each of which contains information that is either required or useful to your program. The header <iostream> is required for this program.

using namespace std:

Using namespace std; instructs the compiler to use the std namespace. Namespaces are a relatively new feature of C++.

int main:

The line int main() is the main function where program execution begins.

Comments:

// Write C++ code, here is the single-line comment in this program that is not executed by the compiler.

cout:

The following line cout “Hello World”; displays the message “Hello World” on the screen.

return 0:

The following line return 0; terminates the main() function and causes it to return 0 to the calling process.

Creating Classes in C++:

In C++, a class is defined by using the keyword class followed by the class name. The curly brackets define the body of the class, which is followed by a semicolon. Let us take an example:

#include <iostream>
using namespace std;
class Coderz
{
    int i;           //data variable
    void print()         //Member Function
    { 
        cout << "Inside Member Function of coderz";
    }
}; // Class ends here

int main()
{
    Coderz obj;  // Creatig Coderz class's object
    obj.print();  //Calling member function using class object
}

This is how a class is defined; after defining a class, its object is created and its member functions are used.

Variables can be declared anywhere in the program, but they must be declared before they can be used. As a result, we don’t need to declare variables at the beginning of the program.

Note: also read about the OOPs Concepts in C++

Follow Me

If you like my post, please follow me to read my latest post on programming and technology.

https://www.instagram.com/coderz.py/

https://www.facebook.com/coderz.py

Share
Published by
Rabecca Fatima

Recent Posts

What is object oriented design patterns

A design pattern is a reusable solution to a commonly occurring problem in software design. They…

4 months ago

Factory Method Design Pattern in OODP

Factory Method is a creational design pattern that deals with the object creation. It separates…

4 months ago

Find Intersection of Two Singly Linked Lists

You are given two singly linked lists that intersect at some node. Your task is…

10 months ago

Minimum Cost to Paint Houses with K Colors

A builder plans to construct N houses in a row, where each house can be…

10 months ago

Longest Absolute Path in File System Representation

Find the length of the longest absolute path to a file within the abstracted file…

10 months ago

Efficient Order Log Storage

You manage an e-commerce website and need to keep track of the last N order…

11 months ago