C++ Review: List of Concepts Covered (since Test 2)
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
also needs the keyword - operator
Example: Fraction operator+(Fraction f1, Fraction f2);
To define:
define as a normal function.
define in terms of the members of the class
General points:
some can be written as member functions, outside functions, or either
some operators must be one or the other (member function vs. outside function)
cannot change precedence, associativity, or arity of operators
cannot change built-in versions or define brand new operators
can define operators on mixed types (e.g. Fraction + int)
Operator overloads as member functions:
first operand is always the object being called through
for binary operators, second operand is passed as parameter
Operator overloads as outside functions:
common to declare as friend functions -- access to private data
any operands are passed as parameters in the function
Examples included overloads of arithmetic operators and comparison
operators
Overloading insertion << and extraction >> operators:
binary operators, for input and output
must be defined as outside functions (usually friend functions)
return type and first parameter are always ostream& for << operator
and istream& for >> operator
for >> operator, pass the object (2nd parameter) by reference, too
Automatics
Constructor
Destructor
Copy Constructor
Assignment operator =
If you don't build one explicitly for a class, a default version
is created by the compiler
Default version of constructor and destructor are empty
Default version of copy constructor and assignment operator make a "shallow
copy"
Deep copy vs. shallow copy
determining factor is usually whether object contains a pointer as member
data, pointing outside object.
Copy Constructor
it is a constructor; invoked automatically
Invoked when a new copy of an object is created:
1) when an object is defined to have the value of another object
(on its declaration statement)
2) when an object is passed into a function by value
3) when an object is returned from a function by value
Declaration Format:
Typename (const Typename &);
Passing the parameter by reference is essential
Should also be const reference, so that r-values can be
arguments
Assignment operator
Overload of the = operator
Invoked when an assignment statement is made (something assigned to an
object).
written as a member function
Declaration Format:
Typename& operator= (const Typename &);
Definition:
similar to the copy constructor (it usually copies one object to another)
Different in that the copy is not a brand new object -- may already have
some data to get rid of (dynamic)
Also different in that it returns the value of the assignment (the object
itself).
Keyword: this
From inside an object, this is a pointer to the object
itself
Can refer to the object (i.e. an effective name) by dereferencing the
pointer: *this
Inheritance
- "is-a" relationship
- Base classes and derived classes (derived class inherits from the
base class)
Declaring derived classes:
Format: class derivedName : public baseName
Protection levels:
- public and private have usual meanings
protected
-
new protection label for use in base/derived classes
-
members declared as protected can be accessed by the class itself and by
derived classes only
Constructors:
-
When a derived object is created, the base constructors run, too
-
constructor definitions run in top-to-bottom order. Base class constructor
first.
-
destructors run in reverse order (derived class first, base last)
Constructors with parameters:
-
parameters can be passed when declaring a derived object.
-
parameters are distributed up to the base constructors through initialization
list
-
Constructor bodies run in normal order (base class first)
Defining Derived classes: