32
Chapter 14 – Object Oriented Design

Chapter 14 – Object Oriented Design. Copy Constructor u Called with an object as an argument u General declarator Class :: Class (Class& alias) –Class

Embed Size (px)

Citation preview

Page 1: Chapter 14 – Object Oriented Design. Copy Constructor u Called with an object as an argument u General declarator Class :: Class (Class& alias) –Class

Chapter 14 – Object Oriented Design

Page 2: Chapter 14 – Object Oriented Design. Copy Constructor u Called with an object as an argument u General declarator Class :: Class (Class& alias) –Class

Copy 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

Page 3: Chapter 14 – Object Oriented Design. Copy Constructor u Called with an object as an argument u General declarator Class :: Class (Class& alias) –Class

Destructor

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

Lesson 14.1

~Class ( );Class :: ~Class ( )

Declaration in class definition

Declarator in function definition

Page 4: Chapter 14 – Object Oriented Design. Copy Constructor u Called with an object as an argument u General declarator Class :: Class (Class& alias) –Class

static 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

Page 5: Chapter 14 – Object Oriented Design. Copy Constructor u Called with an object as an argument u General declarator Class :: Class (Class& alias) –Class

static 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

Page 6: Chapter 14 – Object Oriented Design. Copy Constructor u Called with an object as an argument u General declarator Class :: Class (Class& alias) –Class

static 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

Page 7: Chapter 14 – Object Oriented Design. Copy Constructor u Called with an object as an argument u General declarator Class :: Class (Class& alias) –Class

const 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

Page 8: Chapter 14 – Object Oriented Design. Copy Constructor u Called with an object as an argument u General declarator Class :: Class (Class& alias) –Class

Pointer Data Members with const Three options

– Using const before the data typevalue pointed to cannot be modified

– Using const after the data typeAddress stored by pointer variable cannot be

modified

– Using const before and after the data typeNeither value pointed to nor address stored

can be modified

Lesson 14.3

Page 9: Chapter 14 – Object Oriented Design. Copy Constructor u Called with an object as an argument u General declarator Class :: Class (Class& alias) –Class

const Reference Data Member

General form

const type& ref;

Class :: Class (const type& ref_var) : ref (ref_var)

Class object (base_variable);

Lesson 14.3

Class definition

Constructorfunction

Declaration of object

Page 10: Chapter 14 – Object Oriented Design. Copy Constructor u Called with an object as an argument u General declarator Class :: Class (Class& alias) –Class

Constant 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 function

type Class :: function (type arg1, type arg2) const

Lesson 14.3

Page 11: Chapter 14 – Object Oriented Design. Copy Constructor u Called with an object as an argument u General declarator Class :: Class (Class& alias) –Class

Constant 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

Page 12: Chapter 14 – Object Oriented Design. Copy Constructor u Called with an object as an argument u General declarator Class :: Class (Class& alias) –Class

Mutable 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

Page 13: Chapter 14 – Object Oriented Design. Copy Constructor u Called with an object as an argument u General declarator Class :: Class (Class& alias) –Class

friend 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

Page 14: Chapter 14 – Object Oriented Design. Copy Constructor u Called with an object as an argument u General declarator Class :: Class (Class& alias) –Class

friend 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

Page 15: Chapter 14 – Object Oriented Design. Copy Constructor u Called with an object as an argument u General declarator Class :: Class (Class& alias) –Class

Class That Grants Friendship

Lesson 14.4

Granting classobject 1

Granting classobject 2

Private datamembers for

object 1, granting class

Private datamembers for

object 2, granting class

Public function members,granting class

Friend function(nonmember)

Page 16: Chapter 14 – Object Oriented Design. Copy Constructor u Called with an object as an argument u General declarator Class :: Class (Class& alias) –Class

Member Functions As Friends

Can be member of another class General form (with 2 arguments)

Lesson 14.4

class Class1{ friend Class2 :: function (type, type); … };

Page 17: Chapter 14 – Object Oriented Design. Copy Constructor u Called with an object as an argument u General declarator Class :: Class (Class& alias) –Class

friend 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

Page 18: Chapter 14 – Object Oriented Design. Copy Constructor u Called with an object as an argument u General declarator Class :: Class (Class& alias) –Class

Defining 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

Page 19: Chapter 14 – Object Oriented Design. Copy Constructor u Called with an object as an argument u General declarator Class :: Class (Class& alias) –Class

Calling 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

Page 20: Chapter 14 – Object Oriented Design. Copy Constructor u Called with an object as an argument u General declarator Class :: Class (Class& alias) –Class

Operator 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

Page 21: Chapter 14 – Object Oriented Design. Copy Constructor u Called with an object as an argument u General declarator Class :: Class (Class& alias) –Class

Equivalent 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

Page 22: Chapter 14 – Object Oriented Design. Copy Constructor u Called with an object as an argument u General declarator Class :: Class (Class& alias) –Class

Binary friend Functions

Lesson 14.6

object1 + object2

operator+ (object1, object2);

Expression

Equivalent Function Call

Page 23: Chapter 14 – Object Oriented Design. Copy Constructor u Called with an object as an argument u General declarator Class :: Class (Class& alias) –Class

Binary Member Functions

Different type call because have invoking objects

Lesson 14.6

object1 = object2

object1 . operator = (object2);

Expression

Equivalent Function Call

Placeholder (any binary operator +, -, =,*, or /

Page 24: Chapter 14 – Object Oriented Design. Copy Constructor u Called with an object as an argument u General declarator Class :: Class (Class& alias) –Class

Unary Member Functions

Additional complication of being either prefix or postfix

Lesson 14.6

++ object1

object1.operator++ ( );

Equivalent Function Call

object1 ++

object1.operator++ (dummy);

Expression

Page 25: Chapter 14 – Object Oriented Design. Copy Constructor u Called with an object as an argument u General declarator Class :: Class (Class& alias) –Class

Binary friend Functions

General form example

Lesson 14.6

Class1 operator- (const Class1& objecta, const Class1& objectb){ Class1 temp; temp.member1 = objecta.member1 - objectb.member1; temp.member2 = objecta.member2 - objectb.member2; return temp;}

Page 26: Chapter 14 – Object Oriented Design. Copy Constructor u Called with an object as an argument u General declarator Class :: Class (Class& alias) –Class

Rules 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

Page 27: Chapter 14 – Object Oriented Design. Copy Constructor u Called with an object as an argument u General declarator Class :: Class (Class& alias) –Class

Operator Overloading

Table 14.5 shows many examples Only member functions can return *this Comparison operators return a bool >> and << 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

Page 28: Chapter 14 – Object Oriented Design. Copy Constructor u Called with an object as an argument u General declarator Class :: Class (Class& alias) –Class

UML

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

Page 29: Chapter 14 – Object Oriented Design. Copy Constructor u Called with an object as an argument u General declarator Class :: Class (Class& alias) –Class

UML 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

Page 30: Chapter 14 – Object Oriented Design. Copy Constructor u Called with an object as an argument u General declarator Class :: Class (Class& alias) –Class

Composition

"Has a" relationship Quite commonly in object-

oriented programming Objects contained within

another object

Lesson 14.7

Car class

EngineclassWheel

class

Page 31: Chapter 14 – Object Oriented Design. Copy Constructor u Called with an object as an argument u General declarator Class :: Class (Class& alias) –Class

Association

"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

Page 32: Chapter 14 – Object Oriented Design. Copy Constructor u Called with an object as an argument u General declarator Class :: Class (Class& alias) –Class

Summary

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

Learned how to: