Title: CS240
1CS-240
- Classes (cont)
- Dick Steflik
2Member functions
- Member functions of a class include
- getters
- used to retrieve state data
- setters
- used to set state data
- mutators
- used to modify state data
- functions that model the classes behavior
- tend to be action words (verbs or verb phrases)
- take_off , reverse_direction, open ...
3Function prototypes
- Prototype are models of how a function is to be
called - are not necessarily part of the function
definition, but may be - float calculate_attack_angle (float, float)
- notice that in a prototype only the types are
required, parameter names are not
4Function signatures
- The signature of a function consists of
- returned type (if any)
- the function name
- the ordered set of types in the parameter list
- within its scope, a functions signature must be
unique even if its name is overloaded
5Parameters
- Parameters are required on a function definition
- parameter consists of a type and an identifier
- function formal parameters
- parameters in the function definition
- alias names for the actual arguments
- scope is local to the function
- parameters may be passed to a function
- by value
- by reference
ex. boolean exists(const point p)
6Value Parameters
- read only or input only parameters
- makes a local copy of the value for use in the
function copies value of argument before running
the function - pass by value
- slower than pass by reference
- use only for small objects
7Reference Parameters
- read/write parameter value of argument can be
changed - uses address of argument to manipulate it
- doesnt require local storage
- use to identify reference parameters
- remember is the address operator
ex. void rotate (point p)
8Reference Parameters (cont.)
- use const reference parameters instead of value
parameters (faster), less memory rqd. - const indicates a promise to not modify the
reference parameter exists
ex. double distance(const point p)
p is passed by reference as an input only
parameter
9Function Overloading
- The function name may be reused (overloaded)
within a class - each definition must have a unique signature
(pattern made up of function name and the
parameter types)
10Operator Overloading
- part of polymorphism
- for diadic (binary) functions i.e. functions
with only two operands - more convenient than naming special member
functions to do the same thing - general form left_op operator right_op
ex. boolean operator (const point lh, const
point rh) this overloads equality () for
objects of class point
11Friend Functions
- not a member function
- has access to private data
- member functions work with the current (named
object) friend functions work with multiple
objects of the same class - tag as a friend of the class
- as part of class definition identify, by
prototype, each friend of the class
12Friend Functions (cont.)
- Friend functions are needed in C due to Cs
flawed object model, Java has a better model
(all objects are derived from a single object). - define the prototype in the public section of the
class definition - precede the prototype with the keyword friend
13Friend Functions (more)
- define the friend implementation in the .cpp file
with the member functions - do not precede the function name with the class
name and the scoping operator (ex. classname)
14More Overloading Thoughts
- overload as a member function
- ! lt gt lt gt overload as a non-member (friend)
returning a boolean - gtgt ltlt (insertion and extraction) overload a
non-members (friends) returning type iostream - -/ (arithmetics) overload a non-members
- - ... overload same as and -
15Overloading
// in theClass definition... Time operator
(const Time ) // in the implementation... Tim
e Timeoperator(const Time oTime)
hour oTime.hour min oTime.min
am_pm oTime.am_pm miltime
oTime.miltime
16Time Class overloading
// in the class definition public
section... friend bool operator (const Time
, const Time ) // in the implementation
file..... bool TimeTime(const Time lhs ,
const Time rhs) return (lhs.miltime
rhs.miltime)
17Please note
- The only operators that cannot be overloaded are
- (scoping operator)
- . (dot operator)
- sizeof
- ? (three operands)