First C++ program, hello world in C++

First C++ program : In this article we will learn to make our first C++ program. We will print ‘Hello world’ using C++ here. Although we can print ‘Hello world’ using C++ in various way, here we will see some most common way to print ‘Hello world’.

So, let’s see our first C++ program first. Don’t worry if you can’t understand the program. We will discuss everything of our program here.

Write the following code in your code editor. Save it giving any name with (.cpp) extension, for example hellow world.cpp, first program.cpp or your name.cpp etc. Now, compile and run this program.

// first C++ program hello world in C++

#include <iostream>

using namespace std;      // standard namespace

int main(){               // main function start here

    cout << "Hello World" << endl;

    return 0;
}

Output of this hello world program

first-c-program-hello-world-program-by-c

Wow! you have successfully done your first C++ program and able to print ‘Hello world’ using C++ language. Now, let’s try to discuss about our program that what each element has done here.

#include : This includes all the standard input output library functions of C++. The cin which is used for taking input and cout which is used for showing output is defined inside it.

using namespace std : This will import all the std namespace to your current namespace. However, it is not good to import the entire namespace as it is huge. For this we should use the scope operator (::) when we declare a type.

int main() : This is the main function and our program starts from here. The main function returns an integer data type. The opening and closing curly braces indicates the beginning and ending of main function respectively. We will learn about function later.

cout << : ‘cout <<‘ is used for showing output in C++. This is defined under which will print anything after it.

endl : ‘endl‘ is used to print a new line on the output screen. If we need 2 new line then we have to use endl in two times.

return 0 : Every function (except void) returns a value of different type. The return statement indicates the ending point of a function. return statement is used to return the expected data after executing all the code inside of that funcction.

Hello world program using a class in C++

Here, we will write a program which will print hello world in the screen using C++. Don’t worry if you have no idea about C++ classes. We will learn them later.

// hello world program using a class in C++

#include<iostream>

using namespace std;

class hello_world_printer{    // class creation in C++
  public:

    void display_hello(){
      cout << "Hello World" << endl;
    }
};

int main(){          // main program starts here

    hello_world_printer text;    // Creating an object named text
    text.display_hello();        // function calling

    return 0;
}

Output

first-c-program-hello-world-program-using-c

You may not understand many things in this page. Don’t be frustrated. This is only the first page. You will learn everything step by step later.

Previous pageC++ introduction

Next page:  C++ input/output