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.
Classes and Objects
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
- used for cleanup tasks
- Syntax issues:
- scope resolution operator (when defining class members outside
class header block)
- 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 About Building Classes
- friend functions
- non-member functions that are given access to a class' private
section
- keyword friend
- Know how to write them and where to declare them
- Know how to write member functions that work on more than one object
(calling object, and other object(s) as parameter(s))
- Conversion Constructor
- A constructor with one parameter -- allows automatic type conversions
from parameter type to class type
- Will be invoked in all places where automatic type conversion
applies
- Can suppress automatic conversions with keyword
explicit
- Know how to create one, and when it will be invoked
- Using const with classes and objects. Know all of the usages
of const discussed, what they protect from change, how and when
to use:
- const reference parameters in member functions and friend
functions
- const member functions (const after the
prototype). Function cannot change object (member data)
- const objects -- declare an object as constant, like a
variable. Can only call const member functions
- const member data -- know how to initialize (with special
initialization list section on the constructor)
Operator Overloading
- new versions of common operator symbols
- write them for use with new classes or types (user-defined types)
- To declare
- it is a function. Needs name, return type, parameter list
- name is a conjunction of the keyword operator and the
operator symbol
- Rules
- cannot change precedence, associativity, or arity of operators
- cannot change built-in versions of operators (for the built-in
types)
- cannot make up new operator symbols
- However, it is legal to mix and match types (like a Fraction object
+ a Mixed object)
Members vs. Non-members
- some operators can be written as member functions, outside functions,
or either
- some operators must be one or the other (member function vs.
outside function)
- If written as a stand-alone function, it's common to make them friends
of a class
- For binary operators
- Written as a stand-alone function, both operands are passed as
paramters
- Written as a member function, first operand is calling object, second
is received as a parameter
- For unary operators
- A stand-alone, the operand is received as a parameter
- As a member function, the operand is the calling object
- There are a few small exceptions, like post-increment
(++)
Operators discussed so far
- Arithmetic operators ( + - * / % )
- Comparison operators ( < > <= >= == != )
- Insertion and extraction operators, for input and output (
<< >> )
- These had to be done as stand-alone functions, if they are to work
like the built-in versions. (If they were members, they would have to
be inside classes ostream and istream)
- Return type and first parameter are stream objects, which must
be passed by reference
- for >> operator, pass the object (2nd parameter) by reference, too
- for << operator, pass the object by value or by const reference
(better)
- Increment and decrement operators (++ -- )
- For post-increment (or post-decrement), use an extra dummy parameter
of type int, so the compiler can distinguish from
pre-increment (or pre-decrement)
Aggregation/Composition Relationship
- 'Has-a" relationship between objects
- an object of one type embedded as member data inside of an object of
another type
- Good for representing objects that are components of larger
objects
- Also good for embedding objects into a "manager class", to manage
communication between components
- Class examples: Timer and Display example,
SodaMachine example
- Understand usage of initialization list on the constructor in this
context (when embedded objects have constructors with parameters)
Arrays and Classes
Arrays of Objects
Arrays as member data
- Arrays can be member data of a class
- If the array isn't always full, use a tracking variable (to track how
many slots used)
- Good for building types that use arrays, but also add in safeties,
like boundary checking, in the member functions
Card game example and Array Techniques
- Array of Card objects embedded inside Deck class -- "has-a"
relationshipArray of Card objects embedded in Player class -- "has-a"
relationship
- arrays of objects (and use of dot-operator)
- tracking variable numCards (in Player class) to track
how many cards currently in hand
- tracking variable topCard (in Deck class) to index the deal
position