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

Generate Parenthesis | Intuition + Code | Recursion Tree | Backtracking | Java

Problem Statement: Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. Example…

3 months ago

Square Root of Integer

Given an integer A. Compute and return the square root of A. If A is…

1 year ago

Build Array From Permutation

Given a zero-based permutation nums (0-indexed), build an array ans of the same length where…

1 year ago

DSA: Heap

A heap is a specialized tree-based data structure that satisfies the heap property. It is…

2 years ago

DSA: Trie

What is a Trie in DSA? A trie, often known as a prefix tree, is…

2 years ago

Trees: Lowest Common Ancestor

What is the Lowest Common Ancestor? In a tree, the lowest common ancestor (LCA) of…

2 years ago