Title: Chapter 14 Object Oriented Design
1Chapter 14 Object Oriented Design
2Copy Constructor
- Called with an object as an argument
- General declarator Class Class (Class
alias) - Class is class name
- alias is alias for object being copied
- Values directly copied can be listed in
initialization section - member1 (alias.member1), member2 (alias.member2)
Lesson 14.1
3Destructor
- Member function automatically called when object
goes out of scope - Name of destructor function required to be class
name preceded by tilde () - Cannot return a value or contain arguments
- General destructor declaration and declarator
Class ( ) Class Class ( )
Declarator in function definition
Lesson 14.1
4static Storage Class Specifier
- Gives permanent storage for variable
- Persists through entire program execution
- Memory accessibility
- static used with local variable
- Access allowed only from function where declared
- static used with global variable
- Access allowed from file with declaration
- static used with data member of a class
- access allowed by all objects of the class
Lesson 14.2
5static Data Members
- Shared by all objects of a class
- Declared by keyword static in class definition
- Initialized outside any member functions
- type Class variable value
- Accessible and modifiable by invoking ordinary
member function on any object - Accessible and modifiable by invoking static
member function on class - Specified as public or private in accessibility
Lesson 14.2
6static Function Members
- Declared static by preceding declaration with
keyword static - Not necessarily invoked using object and dot
operator - Without keyword static in function declarator
- Not allowed to access to nonstatic data and
function members - Allowed to be called even when no class objects
exist in program
Lesson 14.2
7const Special Qualifier
- Data members and objects qualified by const
cannot be modified after initialization - Function members qualified with const do not
modify invoking object's data
Lesson 14.3
8Pointer Data Members with const
- Three options
- Using const before the data type
- value pointed to cannot be modified
- Using const after the data type
- Address stored by pointer variable cannot be
modified - Using const before and after the data type
- Neither value pointed to nor address stored can
be modified
Lesson 14.3
9const Reference Data Member
- General form const type ref
Class Class (const type ref_var)
ref (ref_var) Class object
(base_variable)
Class definition
Constructor function
Declaration of object
Lesson 14.3
10Constant Member Functions
- Protects data members from being inadvertently
modified - To declare function (with 2 arguments) const
type function (type, type) const - Form of declarator for above functiontype Class
function (type arg1, type arg2) const
Lesson 14.3
11Constant Objects
- Effectively makes all data members const
- None of data members of const object can be
changed after initialization - Form declare const object with 3 arguments
const Class object (arg1, arg2, arg3) - Can use const object to call function
object.function (arg1b, arg2b)
Lesson 14.3
12Mutable Data Members
- mutable used with non constant data members
- Can be modified by constant member function
- Can overrides the const of a function and change
the non constant data member
Lesson 14.3
13friend Function
- Indicates that function has special relationship
with a class - friend specified for function that is not member
of any class - Allows function to access private data members
directly - Form for declaration with 2 arguments
friend type function (type, type)
Lesson 14.4
14friend Functions
- Defining friend functions
- Keyword not used in function definition
- Directly accesses private data members of object
(where class declared friend) - Calling friend functions
- No invoking object used since friend not a member
function function (arg1, arg2)
Lesson 14.4
15Class That Grants Friendship
Granting class object 1
Granting class object 2
Private data members for object 1, granting class
Private data members for object 2, granting class
Friend function (nonmember)
Public function members, granting class
Lesson 14.4
16Member Functions As Friends
- Can be member of another class
- General form (with 2 arguments)
class Class1 friend Class2
function (type, type)
Lesson 14.4
17friend Class
- All member functions of friend class can access
all private data and function member of class
granting friendship - General form for declaring friend
class Name - Common practice to list friend classes before
public and private access specifiers
Lesson 14.5
18Defining a Friend Class
- Basic general form for defining class
Friend public
type functionf (Granting) - Basic general form for member function type
Friend functionf (Granting objectga)
objectga.functiong ( )
objectga.datag -
Lesson 14.5
19Calling Function of Friend Class
- Basic form for calling friend class objectf
. functionf (objectg) - objectf is the friend class object
- functionf is the friend class member function
name - objectg is the granting class object
Lesson 14.5
20Operator Overloading
- Creates new definitions of operators for use with
objects - Create function for a class called operator
- operator is the keyword
- Follow by math operator of your choice
- Write code to perform member-by-member addition
within function body - When client of class, adds two objects, operator
( ) function automatically called
Lesson 14.6
21Equivalent Function Calls
- Generated by the operator expression
- Dependent upon classification of the operator
function being friend or member, binary or unary - Binary friend functions
- Binary member functions
- Unary member functions
- Unary friend functions (not commonly used)
Lesson 14.6
22Binary friend Functions
object1 object2 operator (object1, object2)
Expression
Equivalent Function Call
Lesson 14.6
23Binary Member Functions
- Different type call because have invoking objects
Expression
object1 object2 object1 . operator
(object2)
Equivalent Function Call
Lesson 14.6
24Unary Member Functions
- Additional complication of being either prefix or
postfix
object1object1.operator ( )
object1 object1.operator (dummy)
Expression
Equivalent Function Call
Lesson 14.6
25Binary friend Functions
Class1 operator- (const Class1 objecta, const
Class1 objectb) Class1 temp
temp.member1 objecta.member1 -
objectb.member1 temp.member2
objecta.member2 - objectb.member2
return temp
Lesson 14.6
26Rules for Operator Overloading
- Set of available and unavailable operators
- Table 14.3 in text
- Cannot change classification of operator
- Operator precedence rules still apply
- Each friend or freestanding operator function
must take at least one object as argument - Equivalent function call for each operator cannot
be modified
Lesson 14.6
27Operator Overloading
- Table 14.5 shows many examples
- Only member functions can return this
- Comparison operators return a bool
- gtgt and ltlt operators are commonly overloaded to
simplify input and output - ( ) operator is called using an object name
followed by an argument list enclosed in ( )
Lesson 14.6
28UML
- Unified Modeling Language
- Developed by James Rumbaugh, Grady Booch, and
Ivar Jacobson in mid 1990s - Create diagrams to develop program
- Collaboration
- Use case
- State chart
- Class
- Many others
Lesson 14.7
29UML Class Diagram Symbols
- Rectangles used to enclose classes
- Lines used to connect classes that interact
- Arrows and descriptors used to indicate type and
direction of interaction - Small filled diamond heads used to indicate
objects of one class are members of another class - Numbers next to rectangles used to indicate
number of objects of a class
Lesson 14.7
30Composition
- "Has a" relationship
- Quite commonly in object-oriented programming
- Objects contained within another object
Car class
Engine class
Wheel class
Lesson 14.7
31Association
- "uses" or client-server relationship
- Keeps server class independent of client class
- Has simple interface between the two classes
- Server class designer must create both interface
and implementation
Lesson 14.7
32Summary
Learned how to
- Use the special class specifiers static and const
- Work with friend functions and classes
- Overload operators
- Read some UML and the difference between "has a"
and "uses" relationships