C++
Review for Test 1: List of Topics Covered
This is a list of topics intended as a checklist to help you recall
what topics have been covered, both in lecture class and in the textbook.
Many examples have been viewed, as well -- most of these are in the
textbook or linked from the online notes. The specific code examples are
mostly not mentioned here -- just the topics. Be sure to refer to the
provided code examples when you review, as needed. Also note that much of
the material from chapters 1-4 is considered review from the pre-requisite
C (or intro C++) course.
C++ Basics
Atomic Data Types
- integer types -- char, short, int, long
- floating point types -- float, double, long double
- bool
- unsigned versions of integer types
Declaring Variables
- format -- typeName variable Name;
- const declarations
- declare before use
- naming rules
Literals:
- integer, float, character, string
Comments:
Operators:
- binary operator vs. Unary operator
- Basic arithmetic operators: + - / * %
- special case of division for integers: / and %
- order of operations
- increment and decrement: ++ --
- Post-increment vs. Pre-increment: x++ vs. ++x
- assignment operator: =(single equal sign)
- Other shortcut operators: += -= *= /= %=
- automatic type conversions and cast operators
Statements:
- Declaration statements
- Expression statements
- Compound statements
Input and Output:
- cin and cout objects
- insertion and extraction operators << and >>
- iostream.h
- namespace "std" (and using statements)
Control Structures (Chapter 2)
Logical Operators:
- testing equality and order == != > >= < <=
- Boolean operators:
&& (AND) , || (OR), ! (NOT)
- short-circuit evaluation of && and ||
Selection statements:
Repetition statements (loops):
Other special keywords:
Functions (Chapter 3)
Function Basics
- Declaration -- name, return type, parameter list
- Definition -- contains function body
- Call -- the execution of a function
- the keyword return -- returning values from functions
- header files
Other Function Topics
- Scope of function parameters
- Default parameters (new C++ feature, not available in C)
- Function overloading - functions with same name and different
parameter lists
- Pass by Reference
- Reference variables
- use & notation when declaring
- an alias for another variable (i.e. nickname)
- useful when variables are in different scopes (i.e. functions)
- used in parameter passing and return values
- Pass by Value - local copies of parameters are made, and copies of
returns are sent back
- Pass by Reference - Copies of parameters and returns are not
made. Local parameters are references to the originals
- Use of
const
with reference parameters to prevent a
function from changing the original (but avoid overhead of making a
copy - faster execution)
Classes and Objects (Chapter 6)
Object
- Name
- member data (attributes, or the state of the object)
- member functions (behavior)
Class
- a blueprint for building objects
- a user-defined type
DDU Design Model
- Declare -- Declaration of class in header file
- Define -- Definition of class members in implementation file
- Use -- Usage of class by building objects
Building Classes
- Separating Interface from implementation
- access levels: public and private
- Why to follow guidelines of private data and an interface of
public functions
- Constructor:
- same name as the class, no return type
- automatically invoked upon object creation
- can have parameters
- "default constructor" = no parameters
- Destructor:
- looks like default constructor, with ~ in front
- automatically invoked when an object is deallocated
- cannot have parameters
- usually used for cleanup tasks
- Syntax issues:
- scope resolution operator (in class definition file)
- dot operator (when calling members of an object)
Compilation and Debugging
Building a program
- Compile stage
- Syntax checking
- Checking "declare before use"
- Matching function calls to prototypes
- Type checking on variable usage
- Translation to object code
- Linking Stage
- Putting 1 or more object code files together
- Matches function calls to their definitions
- Usually result is an executable (but could be other target type, like
a DLL)
- g++ commands for compiling
- -c option for invoking compile stage only
- -o option for naming the output of a command (rather than accepting
default name, like a.out
- General
- Don't #include the .cpp files!
- #include header files to satisfy "declare before use"
- Libraries sometimes distributed in pre-compiled format (meaning
programmer only has header file and "object code", not full
implementation code)
Debugging
- Compilation errors (syntax)
- Linker errors (usually undefined functions or multiply defined
functions)
- Run-time errors: fatal (crash program) and logical (bad results)
Debugging tips given
More on Classes and Objects
const member functions (Chapter 7.2)