Introduction
Welcome to LabEx! This is your first C++ lab, where you'll take your initial steps into the world of C++ programming. Don't worry if you're completely new to this - we'll guide you through every step.
In this lab, you'll learn how to:
- Write and run your first C++ program
- Use basic output in C++
- Work with a simple variable
These fundamental skills will start your journey into C++ programming. Let's begin!
Click the Continue button below to begin the lab.
Your First C++ Program
Let's start with the traditional first program for any programmer - "Hello, World!". This simple program will help you understand the basic structure of a C++ program.
First, we need to open our WebIDE. Look for the "WebIDE" button on your LabEx interface and click it. This will open a VS Code-like environment where we'll write our C++ code.
Once the WebIDE is open, follow these steps:
- In the left sidebar, you'll see a file explorer. Right-click in this area and select "New File".
- If you don't see the option to create a new file, look for a "..." or "More actions" button in the file explorer.
- Name the file
hello.cpp. The.cppextension tells the IDE that this is a C++ file.- Make sure to include the
.cppextension. If you accidentally name it justhello, you can rename it by right-clicking on the file.
- Make sure to include the
- Click on the new
hello.cppfile to open it in the editor.

Now, let's write our first C++ program. Copy and paste the following code into your hello.cpp file:
#include <iostream>
int main() {
std::cout << "Hello, World!" << std::endl;
return 0;
}
Let's break down this code:
#include <iostream>: This line tells the computer to use a pre-written code that allows us to input and output information. Think of it like importing a tool we need to use.int main() { ... }: This is where our program starts. Everything inside the curly braces{ }is what our program will do. Every C++ program needs amainfunction.std::cout << "Hello, World!" << std::endl;: This line tells the computer to print "Hello, World!" to the screen.std::coutis like a funnel that sends text to the screen.- The
<<symbols are like arrows pointing what we want to send into the funnel. std::endlmoves to a new line after printing, like pressing Enter on your keyboard.
return 0;: This tells the computer our program has finished successfully. It's like saying "We're done here!"
To run this program:
- Open the terminal in your WebIDE (usually at the bottom of the screen).
- If you don't see the terminal, look for a menu option like "Terminal" -> "New Terminal" or a terminal icon.
- In the terminal, type the following commands:
g++ hello.cpp -o hello
./hello

The first command (g++ hello.cpp -o hello) prepares your program to run. It's called "compiling" and it translates your C++ code into a language the computer can understand. The -o hello part names the resulting program "hello".
The second command (./hello) actually runs your program. The ./ tells the computer to look in the current folder for the program.
You should see "Hello, World!" printed in the terminal. If you don't, don't worry! Check these common issues:
- Make sure you saved your
hello.cppfile after pasting the code. - Check that you typed the commands exactly as shown, including the
./beforehello. - If you see any error messages, read them carefully. They often give clues about what went wrong.
Congratulations! You've just written and run your first C++ program.
Adding More Output
Now that we've got our first program running, let's expand it a bit. We'll modify our program to print multiple lines. This will show you how easy it is to add more output to your programs.
Open your hello.cpp file in the WebIDE. You can do this by clicking on the file name in the file explorer on the left side of the screen.
Replace the contents of hello.cpp with this new code:
#include <iostream>
int main() {
std::cout << "Hello, World!" << std::endl;
std::cout << "Welcome to C++ programming!" << std::endl;
std::cout << "This is your first C++ lab." << std::endl;
return 0;
}
This program uses std::cout three times to print three different lines. Let's break down what's new:
- We've added two more
std::coutlines, each followed by<< std::endl;. - Each
std::coutline ends withstd::endl;, which moves to a new line. This is why each message will appear on a separate line when we run the program. - Notice how each line ends with a semicolon (
;). In C++, we use semicolons to show where each instruction ends.
Compile and run the program using the same commands as before:
g++ hello.cpp -o hello
./hello

You should now see three lines of text printed in the terminal. If you don't see all three lines, here are some things to check:
- Make sure you saved the file after making changes.
- Check that each line ends with
<< std::endl;. - Look for any missing semicolons at the end of the
std::coutlines.
This demonstrates how you can use multiple std::cout statements to print several lines of text. You can add as many lines as you want using this method!
Using a Simple Variable
For our final step, let's introduce the concept of variables. A variable is like a labeled box where we can store information. In this case, we'll use a variable to store a name, which we'll then use in our greeting.
Create a new file in your WebIDE and name it greeting.cpp. Here's how:
- Right-click in the file explorer (left sidebar) and select "New File".
- Name the new file
greeting.cpp. - Click on
greeting.cppto open it in the editor.
Then, copy and paste the following code:
#include <iostream>
#include <string>
int main() {
std::string name = "LabEx";
std::cout << "Hello, " << name << "!" << std::endl;
std::cout << "Welcome to C++ programming, " << name << "!" << std::endl;
return 0;
}
This program introduces a few new concepts:
#include <string>: This allows us to use text variables. Think of it as importing another tool, this time for working with text.std::string name = "LabEx";: This creates a variable callednameand stores the text "LabEx" in it.std::stringtells C++ thatnamewill hold text.- You can change "LabEx" to any name you like!
- We use the
namevariable in ourcoutstatements to personalize our messages.- Notice how we use
<< name <<to insert the value ofnameinto our output.
- Notice how we use
Compile and run the program:
g++ greeting.cpp -o greeting
./greeting

You should see personalized greeting messages in the terminal. If you changed "LabEx" to a different name, you'll see that name in the output.
If you're not seeing the expected output, here are some things to check:
- Make sure you've saved the
greeting.cppfile after pasting the code. - Check that you've typed the compilation command correctly:
g++ greeting.cpp -o greeting(note that we're usinggreeting.cppandgreetinghere, nothello.cppandhello). - Ensure that the
namevariable is defined before it's used in thecoutstatements.
Try changing the name in the name = "LabEx"; line to your own name, then compile and run the program again. You'll see how variables allow us to easily change parts of our program!
Summary
Congratulations! You've completed your first C++ Lab and taken your first steps into the world of C++ programming. Let's recap what you've learned:
How to write and run a basic C++ program
- You learned about the structure of a C++ program, including the
main()function. - You used
g++to compile your code and./to run the resulting program.
- You learned about the structure of a C++ program, including the
How to use
coutto display output- You used
std::coutto print messages to the screen. - You learned about
std::endlto move to a new line after printing.
- You used
How to use multiple
coutstatements to print several lines- You saw how easy it is to add more output to your program.
How to use a simple string variable to store and display text
- You learned how to create a
stringvariable and give it a value. - You saw how to use a variable in your
coutstatements to personalize output.
- You learned how to create a
These are the building blocks of C++ programming. You've made an excellent start!
Remember, learning to program is a journey, and you've just taken your first steps. Here are some tips as you continue learning:
- Don't be afraid to experiment! Try changing the text in your
coutstatements or the value of yournamevariable. - If you encounter errors, read the error messages carefully. They often provide clues about what went wrong.
- Practice is key. Try writing small programs that use what you've learned here.
Your next adventure in C++ is just around the corner. Keep coding, and have fun exploring the world of C++!



