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.
Chapter 14 – Object Oriented Design 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 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 ~Class ( ); Class :: ~Class ( ) Lesson 14.1 Declaration in class definition Declarator in function definition 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 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 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 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 Pointer 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 const Reference Data Member General form const type& ref; Class definition Class :: Class (const type& ref_var) Constructor : ref (ref_var) Class object (base_variable); Declaration of object Lesson 14.3 function 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 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 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 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 memberbymember addition within function body When client of class, adds two objects, operator+ ( ) function automatically called Lesson 14.6 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 Binary friend Functions object1 + object2 Expression operator+ (object1, object2); Equivalent Function Call Lesson 14.6 Binary Member Functions Different type call because have invoking objects Placeholder (any binary operator +, , =,*, or / object1 = object2 Expression object1 . operator = (object2); Equivalent Function Call Lesson 14.6 Unary Member Functions Additional complication of being either prefix or postfix object1 ++ ++ object1 Expression object1.operator++ (dummy); object1.operator++ ( ); Equivalent Function Call Lesson 14.6 Binary friend Functions General form example 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 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 Operator Overloading Table 14.5 shows many examples Only member functions can return *this Comparison operators return a bool >> and