C++ Program to Print Hello World

Write a simple C++ Program to Print Hello World with an example. Every C++ program starts with a #include statement to include the preprocessor directives. Here, #include<iostream> includes the iostream file that helps us to use the cout statement. Here, using namespace std helps to import the std namespace.

#include<iostream>

using namespace std;

int main()
{
	cout << "Hello World";
	
	return 0;
}
C++ Program to Print Hello World 1

If you don’t want to import namespace std (using namespace std;), then remove it and use C++ std::cout.

#include<iostream>

int main()
{
	std::cout << "Hello World";
	
	return 0;
}
Hello World

Simple C++ Program to Print Hello World using Functions

#include<iostream>

using namespace std;

void disply_helloworld() 
{
	cout << "Hello World";
}

int main()
{	
	disply_helloworld();
	
	return 0;
}
Hello World!

In this C++ hello world example, we created a separate class (Message) that contains a method (disply_helloworld()) to print hello world message. Next, we created an instance of that class and called that method from out main program.

#include<iostream>

using namespace std;

class Message
{
	public: void disply_helloworld() {
		cout << "Hello World";
	}
};

int main()
{
	Message msg;
	
	msg.disply_helloworld();
	
	return 0;
}
Hello World