Module 1: Introduction
- First read this page then start coding module and answering the exercise questions with the GitHub classroom link below.
- Github Classroom Link: https://classroom.github.com/a/MWOSfhYU
Objectives
The goal of this module is to compile and run your first C++ program.
Table of Contents
Hello World in C++
Here is the classic Hello World program in C++:
// This program outputs the message “Hello, World!”
#include <iostream>
using namespace std;
int main()
{
cout << "Hello, World!" << endl;
return 0;
}
The #include
is a preprocessor directive to load the iostream
library.
Execution starts in a function called main
. There are other signatures for main
that include arguments to the main
function as we will see. The return type here is int
, which specifies that an integer will be returned by the main function.
The name cout
refers to the standard output stream and along with its output operator <<
the string “Hello, World!” is put into the character output stream and will appear on the screen.
The name endl
is the newline character that will be also added to the character output.
Compiling and executing on Unix
The program above is a plain text file, as in any programming language.
The file extension needs to be .cpp
.
The file name need not be hello.
To compile:
g++ hello.cpp -o hello
This produces an executable called hello
which can be executed as:
./hello
The compiler options (switches):
- The simplest form on invoking the g++ compiler is:
g++ hello.cpp
- This produces an executable called a.out (by tradition). This is an abbreviated form of “assembler output”.
- The
-o
option lets you specify the name of the executable.
Exercise 1:
-
Remove the
<< endl
fromhello.cpp
but keep the;
semicolon at the end of the line and see what happens. Recompile and run. -
Describe what happened. Put your answer in the
README.md
file in your GitHub repository.
Exercise 2:
- The following program should not compile. Fix it and explain why.
using namespace std;
int main()
{
cout << "Why oh why does this program fail to compile?!?!" << endl;
return 0;
}
-
Describe the error and explain the fix. Put your answer in the
README.md
file in your GitHub repository. -
Fix the code in
fixme.cpp