Basic Structure
Every C++ program follows a basic structure that serves as its foundation. This includes adding comments to document your code, using preprocessor directives to set up the program, and writing the main function where the program’s execution begins.
Commenting
Section titled “Commenting”Comments are little notes about your code to remind yourself, or tell other developers what your code is doing. Comments are ignored by the compiler, so feel free to write as many as you need!
-
Single-line comment:
// Comment goes after -
Multi-line comment:
/* Comment goes in-between */
Main Function
Section titled “Main Function”The main function is the entry point of any C++ program. It’s where the program begins
execution. Every C++ program must have exactly one main function, and its structure
allows the programmer to control the flow of the program.
You’ll write your code inside
this function, making it the starting place for any task your program performs.
The main function will usually be found in a file called driver.cc.
int main() { return 0; }-
intis thereturntype of the function -
mainis the name of the function -
()means an empty parameter list for the main function
return 0;0 is treated asfalsein C++ and other programming languages, so if youreturn0 at the end of your program then you are saying that it is false that the program had any errors, this may remind you ofSystem.exit(0);in Java.
Printing To The Console
Section titled “Printing To The Console”Before you can print anything to the console, you need to include the <iostream> header file using a preprocessor directive.
Preprocessor Directives
Section titled “Preprocessor Directives”Preprocessor directives are instructions that are processed before the actual
compilation of your code begins. They typically start with a # symbol and are
used to include files or define constants. For example, the directive #include <iostream>
imports the tools needed to use objects like std::cout for output and
std::cin for input.
Namespaces
Section titled “Namespaces”In C++, libraries like <iostream> organize their functionality into a namespace called std.
By specifying std::cout, you’re telling the compiler you want to use the cout object from the
standard namespace. This avoids potential naming conflicts and keeps your code clear.
Operators
Section titled “Operators”In C++, operators are special symbols or keywords that perform operations on variables and values.
The first operator introduced is the scope resolution operator ::. It is used to access
members of a namespace or a class. For example, std::cout uses the scope resolution operator
to specify that cout belongs to the std (standard) namespace.
Another operator in C++ is the stream insertion
operator <<, which is used for output. It works alongside the cout object to
display information on the screen. It’s similar to using System.out.print() in CSCE145
and CSCE146 when you were learning Java.
Example
Section titled “Example”The following code shows how to bring each concept together and print a value to the console. Most files in this course will be structured this way:
// Copyright 2024 CSCE2024#include <iostream> // Preprocessor Directive
using std::cout; // Declaration for Namespace Scoping
int main() { // Main Function cout << "Hello USC!"; // Printing with cout
return 0;}Line 4 allows you to bring cout into the global namespace,
letting you use cout directly without needing to prepend it
with std::. This makes the code more concise, especially if
you use cout multiple times in your program.
However, if you only need to use cout once or prefer not to
declare it globally, you can reference it directly from the
std namespace, as shown below:
// Copyright 2024 CSCE2024#include <iostream> // Preprocessor Directive
int main() { // Main Function std::cout << "I can still use cout!";
return 0;}Escape Sequences
Section titled “Escape Sequences”In C++, escape sequences are special character combinations that
let you format output in specific ways. They start with a backslash \
and are used to insert things like new lines, tabs, or special characters
into your output.
The following is a list of some escape sequences:
\n: Go to the next line\r: Return to the beginning of the current line\t: Tab to the next default tab stop\a: Ring the system bell\\: Used to output a single backslash (”\“)
Endl Stream Manipulator
Section titled “Endl Stream Manipulator”The endl stream manipulator is used to insert a newline character
into the output and flush the output stream, ensuring that any
buffered output is immediately written to the console. You can think
of it like the System.out.println() of C++.
Since endl is part of the std namespace, you need to reference it with
std::endl unless you’ve declared it globally with using std::endl;. For example:
// Copyright 2024 CSCE2024#include <iostream>
using std::cout;using std::endl;
int main() { cout << "Make: Audi\nModel: RS5\nHP: 444\nTorque: 442" << endl; cout << "Which color would you like to see?" << endl; return 0;}g++ driver.cc./a.outMake: AudiModel: RS5HP: 444Torque: 442Which color would you like to see?