0
0
C++programming~15 mins

Structure of a C++ program - Mini Project: Build & Apply

Choose your learning style9 modes available
Structure of a C++ program
📖 Scenario: You want to create a simple C++ program that prints a greeting message to the screen. This will help you understand the basic parts of a C++ program.
🎯 Goal: Build a complete C++ program that includes the necessary parts: header inclusion, main function, and output statement to display "Hello, World!" on the screen.
📋 What You'll Learn
Include the <iostream> header
Use the std namespace
Define the main function with return type int
Print Hello, World! using std::cout
Return 0 from main
💡 Why This Matters
🌍 Real World
Every C++ program starts with this basic structure. Knowing it helps you write any program, from simple to complex.
💼 Career
Understanding the program structure is essential for software development jobs that use C++.
Progress0 / 4 steps
1
Include the iostream header
Write the line #include <iostream> at the top of your program to include the input-output library.
C++
Need a hint?

This line allows you to use std::cout to print text.

2
Use the std namespace
Add the line using namespace std; below the include statement to avoid typing std:: before standard functions.
C++
Need a hint?

This saves you from writing std:: every time.

3
Define the main function
Write the main function header as int main() and open a block with curly braces { }.
C++
Need a hint?

The main function is where your program starts running.

4
Print message and return 0
Inside the main function, write cout << "Hello, World!" << endl; to print the message, then write return 0; to end the program.
C++
Need a hint?

The cout line prints the message, and return 0; signals the program ended successfully.