Module 1: Introduction | CMSC 240 Software Systems Development - Fall 2024

Module 1: Introduction

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):

	g++ hello.cpp 

Exercise 1:





Exercise 2:

using namespace std;

int main() 
{
	cout << "Why oh why does this program fail to compile?!?!" << endl;
    
	return 0;
}