Sign in to save

Bookmark this page so you can find it later.

Sign in to save

Bookmark this page so you can find it later.

Modern C++ syntax includes the rules for writing clear, safe, and efficient C++ programs using features from C++11 and later. Students need this cheat sheet to quickly remember how to declare variables, write functions, use containers, control program flow, and build simple classes. It is especially useful when moving from basic programming ideas to full C++ programs that compile and run correctly.

The most important ideas are type safety, scope, value versus reference behavior, and using standard library tools instead of manual memory management when possible. Common syntax includes int x = 5;, auto name = value;, for loops, range-based loops, std::vector, std::string, and function definitions like int add(int a, int b). Modern C++ encourages const for values that should not change, references for efficient parameter passing, and smart containers such as vector instead of raw arrays.

Key Facts

  • A variable declaration gives a type and a name, such as int count = 0; or double price = 12.99;.
  • Use auto when the initializer clearly shows the type, such as auto total = 42; or auto name = std::string("Ada");.
  • A function definition uses the form return_type function_name(parameter_list) { statements }, such as int square(int x) { return x * x; }.
  • An if statement runs code only when a condition is true, using syntax like if (x > 0) { cout << x; } else { cout << 0; }.
  • A for loop repeats code with initialization, condition, and update, such as for (int i = 0; i < 5; i++) { cout << i; }.
  • A range-based for loop visits each element in a container, such as for (const auto& item : items) { cout << item; }.
  • A std::vector stores a resizable list, and common operations include v.push_back(x), v.size(), and v[i].
  • Use const Type& parameter_name to pass large objects without copying them when the function should not change the object.

Vocabulary

Variable
A named storage location with a type, such as int score, that holds a value during program execution.
Function
A reusable block of code that can take input parameters, perform actions, and return a value.
Scope
The part of a program where a name, such as a variable or function, can be used.
Reference
An alias for an existing object, written with &, that lets code use the original object instead of a copy.
Vector
A standard library container, std::vector, that stores elements in order and can grow or shrink in size.
Class
A user-defined type that groups data and functions into one structure for modeling objects.

Common Mistakes to Avoid

  • Forgetting a semicolon after a statement is wrong because C++ uses semicolons to mark the end of most declarations and statements.
  • Using = instead of == in a condition is wrong because = assigns a value, while == compares two values for equality.
  • Accessing v[v.size()] is wrong because vector indexes start at 0, so the last valid index is v.size() - 1.
  • Passing a large vector by value is often wrong because it copies the whole vector; use const vector<int>& v when the function only needs to read it.
  • Changing a container while using a range-based loop can be wrong because adding or removing elements may invalidate the loop's internal position.

Practice Questions

  1. 1 What is printed by this code: int x = 3; x += 4; cout << x;?
  2. 2 How many times does this loop run: for (int i = 2; i < 10; i += 2) { cout << i; }?
  3. 3 Write a C++ function header and body for int maxOfTwo(int a, int b) that returns the larger value.
  4. 4 Explain why const string& name might be better than string name as a function parameter when the function only reads the string.

Understanding Modern C++ Syntax Quick Reference

C++ is a compiled language. Before a program runs, a compiler checks the source code and turns it into machine instructions. This makes small syntax details important.

A missing semicolon, unmatched brace, or incorrect type can stop the entire build. Read compiler messages from the first error upward.

Later messages are often consequences of one earlier mistake. Indentation does not change what C++ does, but consistent indentation makes braces and nested blocks much easier for people to inspect.

Types affect both the values a program can hold and the operations it can perform. An integer is useful for counts, while a floating point value is useful for measurements such as temperature or distance. A common beginner problem is integer division.

Dividing two integers produces an integer result, so a fraction may be discarded. Converting one value to a floating point type changes that behavior. Initialization matters too.

Give a variable a sensible value when it is created. Using an uninitialized local variable can produce unpredictable results that are difficult to trace.

Scope and lifetime explain many confusing bugs. A name created inside a pair of braces is normally available only inside that block. For example, a loop counter should not be expected to exist after its loop ends.

Lifetime is about how long the actual object remains valid. A reference or pointer must not be used after the object it refers to has been destroyed.

This can happen when a function returns a reference to one of its own local variables. The local variable disappears when the function finishes, leaving an invalid connection.

Pointers and references both provide access to an existing object, but they have different rules. A reference is usually used when a function needs to work with the caller's object directly. A const reference allows reading without copying a large object or changing it.

A pointer can hold no valid object, which is represented by nullptr, and it can later point somewhere else. Check a pointer before using it when nullptr is possible.

Modern C++ often avoids manually creating and deleting objects. Containers manage their own storage, which reduces memory leaks and accidental use of freed memory.

Classes help keep related data and actions together. A well-designed class protects its own rules. For a bank balance, a deposit function can reject a negative amount instead of allowing outside code to change the balance carelessly.

This idea is called encapsulation. In school projects, classes can represent students, game characters, sensors, books, or shapes.

Pay attention to whether a function changes an object, returns a new value, or only reads data. Use clear names, keep functions short, and test edge cases such as empty lists, zero values, and inputs at the limits of the allowed range.