Chapter roadmap
Learn how C++ source becomes an executable, then write programs that store values, calculate results, and communicate through the console. Work through the concepts in order, type every example yourself, and keep a short debugging log that records predictions, errors, fixes, and what each fix taught you.
Learning objectives
- Explain source, compilation, linking, and execution
- Build a valid main function
- Choose useful fundamental data types
- Read and format console input and output
01.1
From source to executable
C++ is a compiled language. A compiler translates source files into object code, and a linker combines that code with libraries to make an executable. Compiler errors happen before the program runs, while runtime errors happen after execution begins. Reading the first useful error message is an essential skill.
Before running the example, trace each expression and write down the expected state or result. Then run it, compare the actual result, and change one input or rule to confirm that you understand which part controls the behavior.
Example#include <iostream>
int main() {
std::cout << "Hello, C++!\n";
return 0;
}ResultHello, C++!
Check your understandingExplain the example in plain language, identify one boundary or failure case, and revise it so the output changes in a predictable way.
01.2
The main function and statements
Execution begins in main. Statements normally end with semicolons, braces group statements into a block, and comments explain intent without affecting execution. Keeping one responsibility per statement makes early programs easier to trace and debug.
Before running the example, trace each expression and write down the expected state or result. Then run it, compare the actual result, and change one input or rule to confirm that you understand which part controls the behavior.
Exampleint main() {
// Each statement performs one clear action.
std::cout << "Room 310\n";
std::cout << "First program complete.\n";
return 0;
}ResultRoom 310
First program complete.
Check your understandingExplain the example in plain language, identify one boundary or failure case, and revise it so the output changes in a predictable way.
01.3
Variables and fundamental types
A variable pairs a name with a type and value. int stores whole numbers, double stores decimal values, char stores one character, bool stores true or false, and std::string stores text. Initialize variables when they are declared so the program never depends on an unknown value.
Before running the example, trace each expression and write down the expected state or result. Then run it, compare the actual result, and change one input or rule to confirm that you understand which part controls the behavior.
Example#include <string>
int students = 24;
double average = 91.5;
char section = 'A';
bool classOpen = true;
std::string course = "C++";
Resultstudents = 24
average = 91.5
section = A
classOpen = true
course = C++
Check your understandingExplain the example in plain language, identify one boundary or failure case, and revise it so the output changes in a predictable way.
01.4
Input, arithmetic, and formatting
std::cin reads typed values and the stream insertion operator sends values to std::cout. Arithmetic follows normal precedence, so parentheses should be used when they clarify a formula. The iomanip library controls decimal precision for money and measurements.
Before running the example, trace each expression and write down the expected state or result. Then run it, compare the actual result, and change one input or rule to confirm that you understand which part controls the behavior.
Example#include <iomanip>
#include <iostream>
int main() {
double width, height;
std::cout << "Width and height: ";
std::cin >> width >> height;
double area = width * height;
std::cout << std::fixed << std::setprecision(2)
<< "Area: " << area << "\n";
}ResultWidth and height: 4.5 3
Area: 13.50
Check your understandingExplain the example in plain language, identify one boundary or failure case, and revise it so the output changes in a predictable way.
Chapter assignments
Complete these in order. Later assignments assume that earlier skills are working. Do not only test the sample values; design tests that challenge boundaries, missing values, incorrect types, empty collections, and other likely failures.
- A1.1
Create a personal introduction that prints at least six labeled facts and uses comments to identify each section.
Submit readable source code, a brief design note, and evidence from at least three tests including one boundary or invalid case.
- A1.2
Ask for two decimal numbers and report their sum, difference, product, quotient, and remainder where appropriate.
Submit readable source code, a brief design note, and evidence from at least three tests including one boundary or invalid case.
- A1.3
Convert a Fahrenheit temperature to Celsius using a named variable for every part of the formula.
Submit readable source code, a brief design note, and evidence from at least three tests including one boundary or invalid case.
- A1.4
Build a receipt that accepts an item name, quantity, and unit price, then prints subtotal, tax, and total to two decimal places.
Submit readable source code, a brief design note, and evidence from at least three tests including one boundary or invalid case.
Chapter project
Console Toolkit
Console Toolkit โ Create a menu-free program that collects a user's name and three numeric measurements, calculates at least four useful results, and prints a carefully formatted report. Include a heading, explanatory comments, sensible variable names, and test evidence for normal and decimal input.
Required process
- Write a short specification listing inputs, outputs, rules, and failure cases.
- Break the work into small functions, queries, modules, or classes appropriate to the language.
- Build the smallest working version before adding optional features.
- Test normal, boundary, empty, and invalid cases and record the results.
- Refactor names and duplication, then write a concise user guide.
Key terms
- compiler
- linker
- executable
- statement
- variable
- initialization
- type
- stream