Write a simple C++ Program to Print Hello World with an example. Every 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.
In this C++ program, using namespace std helps to import the std namespace and print hello world.
#include<iostream> using namespace std; int main() { cout << "Hello World"; return 0; }
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 example, we created a separate class (Message) that contains a method (disply_helloworld()) to print messages. Next, we created an instance of that class and called that method from out of the 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