Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


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

1 Chapter 14 – Object Oriented Design

2 Copy Constructor u Called with an object as an argument u General declarator Class :: Class (Class& alias) –Class is class name –alias is alias for object being copied u Values directly copied can be listed in initialization section :member1 (alias.member1), member2 (alias.member2) Lesson 14.1

3 Destructor u Member function automatically called when object goes out of scope u Name of destructor function required to be class name preceded by tilde (~) u Cannot return a value or contain arguments u General destructor declaration and declarator Lesson 14.1 ~Class ( ); Class :: ~Class ( ) Declaration in class definition Declarator in function definition

4 static Storage Class Specifier u Gives permanent storage for variable –Persists through entire program execution u Memory accessibility –static used with local variable t Access allowed only from function where declared –static used with global variable t Access allowed from file with declaration –static used with data member of a class t access allowed by all objects of the class Lesson 14.2

5 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

6 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

7 const Special Qualifier u Data members and objects qualified by const cannot be modified after initialization u Function members qualified with const do not modify invoking object's data Lesson 14.3

8 Pointer Data Members with const u 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

9 const Reference Data Member u General form const type& ref; Class :: Class (const type& ref_var) : ref (ref_var) Class object (base_variable); Lesson 14.3 Class definition Constructor function Declaration of object

10 Constant Member Functions u Protects data members from being inadvertently modified u To declare function (with 2 arguments) const type function (type, type) const; u Form of declarator for above function type Class :: function (type arg1, type arg2) const Lesson 14.3

11 Constant Objects u Effectively makes all data members const –None of data members of const object can be changed after initialization u Form: declare const object with 3 arguments const Class object (arg1, arg2, arg3); u Can use const object to call function object.function (arg1b, arg2b); Lesson 14.3

12 Mutable Data Members u mutable used with non constant data members u Can be modified by constant member function –Can overrides the const of a function and change the non constant data member Lesson 14.3

13 friend Function u Indicates that function has special relationship with a class u friend specified for function that is not member of any class –Allows function to access private data members directly u Form for declaration with 2 arguments friend type function (type, type); Lesson 14.4

14 friend Functions u Defining friend functions –Keyword not used in function definition –Directly accesses private data members of object (where class declared friend) u Calling friend functions –No invoking object used since friend not a member function function (arg1, arg2); Lesson 14.4

15 Class That Grants Friendship Lesson 14.4 Granting class object 1 Granting class object 2 Private data members for object 1, granting class Private data members for object 2, granting class Public function members, granting class Friend function (nonmember)

16 Member Functions As Friends u Can be member of another class u General form (with 2 arguments) Lesson 14.4 class Class1 { friend Class2 :: function (type, type); … };

17 friend Class u All member functions of friend class can access all private data and function member of class granting friendship u General form for declaring friend class Name; u Common practice to list friend classes before public and private access specifiers Lesson 14.5

18 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

19 Calling Function of Friend Class u 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

20 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

21 Equivalent Function Calls u Generated by the operator expression u 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

22 Binary friend Functions Lesson 14.6 object1 + object2 operator+ (object1, object2); Expression Equivalent Function Call

23 Binary Member Functions u Different type call because have invoking objects Lesson 14.6 object1 = object2 object1. operator = (object2); Expression Equivalent Function Call Placeholder (any binary operator +, -, =,*, or /

24 Unary Member Functions u Additional complication of being either prefix or postfix Lesson 14.6 ++ object1 object1.operator++ ( ); Equivalent Function Call object1 ++ object1.operator++ (dummy); Expression

25 Binary friend Functions u 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; }

26 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

27 Operator Overloading u Table 14.5 shows many examples u Only member functions can return *this u Comparison operators return a bool u >> and << operators are commonly overloaded to simplify input and output u ( ) operator is called using an object name followed by an argument list enclosed in ( ) Lesson 14.6

28 UML u Unified Modeling Language u Developed by James Rumbaugh, Grady Booch, and Ivar Jacobson in mid 1990s u Create diagrams to develop program –Collaboration –Use case –State chart –Class –Many others Lesson 14.7

29 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

30 Composition u "Has a" relationship u Quite commonly in object- oriented programming u Objects contained within another object Lesson 14.7 Car class Engine class Wheel class

31 Association u "uses" or client-server relationship u Keeps server class independent of client class u Has simple interface between the two classes u Server class designer must create both interface and implementation Lesson 14.7

32 Summary u Use the special class specifiers static and const u Work with friend functions and classes u Overload operators u Read some UML and the difference between "has a" and "uses" relationships Learned how to:


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

Similar presentations


Ads by Google