Lecture Programming in C++ - Chapter 14: Object oriented design. In this chapter, you will learn how to: Use the special class specifiers static and const, work with friend functions and classes, read some UML and the difference between has a and uses relationships.
Trang 1Design
Trang 3Member function automatically called when object goes out of scope
Trang 4Access allowed from file with declaration – static used with data member of a class
access allowed by all objects of the class Lesson 14.2
Trang 5 Accessible and modifiable by invoking static member function on class
Specified as public or private in accessibility Lesson 14.2
Trang 6 Allowed to be called even when no class objects
exist in program
Lesson 14.2
Trang 7Data 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
Trang 8– Using const before and after the data
type
Neither value pointed to nor address stored can be modified
Lesson 14.3
Trang 10Protects data members from being
inadvertently modified
To declare function (with 2 arguments) const type function (type, type) const;
Form of declarator for above function
type Class :: function (type arg1, type arg2) const
Lesson 14.3
Trang 11Effectively 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
Trang 12Lesson 14.3
Trang 13friend type function (type, type);
Lesson 14.4
Trang 14Defining 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
Trang 15granting class Friend function(nonmember)
Trang 16};
Trang 17All member functions of friend class can access all private data and function member
Trang 19– objectg is the granting class object
Lesson 14.5
Trang 20Lesson 14.6
Trang 21Generated 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
Trang 23Placeholder (any binary operator +, , =,*, or /
Trang 24Additional complication of being either prefix or postfix
Trang 26 Equivalent function call for each operator cannot
be modified
Lesson 14.6
Trang 27Lesson 14.6
Trang 28Unified Modeling Language
Developed by James Rumbaugh, Grady Booch, and Ivar Jacobson in mid 1990sCreate diagrams to develop program
Trang 29 Rectangles used to enclose classes
Lines used to connect classes that interact
Arrows and descriptors used to indicate type and direction of interaction
Trang 30class
Trang 31"uses" or clientserver relationship
Keeps server class independent of client class Has simple interface between the two classesServer class designer must create both
interface and implementation
Lesson 14.7
Trang 32Use the special class specifiers static and const