Code setup and explanation
When you create your code - ensure you have the following:
Comment section:
Chapter, Lab Number, Programming Language
Your Name
Created/Modified date
Purpose of program
/*
Chapter and Lab Number: Module 1, Lab 2, C++
Author: John Doe
Date: July 12, 2023
Description: This file contains an example C++ program that demonstrates the usage of functions and arithmetic operations to calculate the sum of two numbers and display the result.
*/
Preprocessor directives - These are statements that begin with a
#
symbol and are used to include header files#include <iostream>
Namespace declaration - Namespaces are used to organize code and avoid naming conflicts between different libraries or modules. The
using namespace
directive allows you to use elements from a specified namespace without having to provide their fully-qualified names.using namespace std; // Allows you to use 'std' namespace elements without the 'std::' prefix
The
main
function: This is the entry point of your C++ program. The execution of your program starts from themain
function. It should have a return type ofint
(it can take command-line arguments)
Output
Code Breakdown:
Let's break down the code piece by piece:
These lines are comments in the code. In C++, comments that begin with //
are single-line comments. They're not executed as part of the code, but they provide information or context to readers. Here, the comments are giving information about the code's purpose, the author, the creation date, and the specific features demonstrated as well as the language the file is written in (it is also acceptable to use the file name and extension - Example main.cpp)
This line includes the iostream
library, which provides facilities for input-output operations. In this code, it's used to output text to the console.
This line tells the compiler to use the std
(standard) namespace for the functions and objects used in this code. This is why we can use cout
and endl
without prefixing them with std::
.
This is the declaration of the main function, which is the starting point for execution in a C++ program. Every C++ program needs a main
function.
Here, we're using cout
(short for "character output") to print a string to the console. The string is followed by endl
, which stands for "end line". This will both print a new line and flush the output buffer, ensuring that the text is immediately displayed on the console.
Again, we're using cout
to print a string. This time, instead of endl
, the string itself contains a newline escape sequence (\n
), which will move the cursor to the next line after printing.
Similar to the first line of output, we print a string followed by endl
to create a new line.
This line signals the successful completion of the main
function. In C++, returning 0 from main
typically indicates that the program finished without any errors.
This closing brace marks the end of the main
function's body. The comment simply provides additional clarity about what this brace corresponds to.
Each string is printed on a separate line, thanks to the use of endl
and \n
.
COSC-1336 / ITSE-1302 Computer Science - Author: Dr. Kevin Roark