Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Classes Provide Constructors Mechanism to specify object creation Client can use objects similar to native types Constructor is member function with.

Similar presentations


Presentation on theme: "1 Classes Provide Constructors Mechanism to specify object creation Client can use objects similar to native types Constructor is member function with."— Presentation transcript:

1 1 Classes Provide Constructors Mechanism to specify object creation Client can use objects similar to native types Constructor is member function with the same name as the class Constructs values of the class type Initializes data members Frequently, allocates free store using new

2 2 Getting Correct Memory for the Task “Where did I put that memory? I need more to get the job done.”

3 3 Built-in Operators new and delete new allocates memory from free store delete deal locates memory from free store Constructors often use new Destructors often use delete

4 4 Using new Initializer list Expressions used by the object's constructor or single value used for a native type p = new int(9); //pointer to int //initialized to 9 p = new int[9]; //pointer to 9 // element int array

5 5 Using Constructors and Destructors Constructors are more complicated May be overloaded and take arguments Constructor invoked Associated type used in definition Call-by-value used to pass value to a function Neither can use return statements Destructors implicitly invoked

6 6 Constructors & Destructors Form //can have multiple constructors class_name (parameter list); class_name (); //can have only 1 destructor ~class_name (); Constructors initializationstorage allocationconversions Destructors finalizationstorage reclamation

7 7 Constructors and the use of new Usually involve application of new new type-name optional-init-list new operator returns a pointer to base address of created object In the case of an array of objects, this is the address of its first element

8 8 Constructor Initialization (1 of 2) class mod_int { public: mod_int(int i) { v = i % modulus; } void assign(int i) { v = i % modulus; } void print() const { cout << v << '\t'; } const static int modulus; private: int v; }; const int mod_int::modulus = 60;

9 9 Constructor Initialization (2 of 2) int main() { int seconds = 400; mod_int z(seconds); //mod_int(int) cout << seconds << " seconds equals " << seconds / 60 << " minutes "; z.print(); cout << " seconds" << endl; return 0; }

10 10 Comments on the mod_int Program Integer v is restricted in value to 0, 1, 2,...modulus - 1 Programmer's responsibility to enforce this mod_int::mod_int() is constructor Invoked when objects of type mod_int are declared Function of one argument Requires expression assignment-compatible with int parameter to be passed to it Creates and initializes declared variable

11 11 Other mod_int Declarations mod_int a(0); // a.v = 0; mod_int b(61); // b.v = 1; but not mod_int a; // no parameter list mod_int c = 49; //construct mod_int(49) //and copy into c

12 12 The Default Constructor Constructor requiring no arguments Empty argument list or all arguments have default values Special purpose of initializing arrays of objects of its class

13 13 Multiple Constructors Convenient to overload constructor with several function declarations If no constructor, then arrays of objects automatically allocated by system If class has constructors but does not a default constructor, array allocation is syntactic error

14 14 Default Constructor with mod_int mod_int() { v = 0; } Possible to have the following declarations mod_int s1, s2; //init private v = 0 mod_int d[5]; //arrays initialized Empty parameter list constructor is invoked Could have one constructor serve as both general initializer and default constructor inline mod_int::mod_int(int i = 0) { v = i % modulus; }

15 15 Conversion Constructors Constructors of single parameter Used automatically for conversion unless declared with the keyword explicit Metal::Metal(Ore) provides code used to convert an Ore object to a Metal object Printable program prints invisible characters with their ASCII designation Code 07 (octal) is alarm or bel

16 16 Printable program (1 of 3) // ASCII printable characters // conversion constructor used class pr_char { public: pr_char(int i = 0) : c(i % 128) { } void print() const { cout << rep[c]; } private: int c; static const char* rep[128]; };

17 17 Printable program (2 of 3) const char* pr_char::rep[128] = { "nul", "soh", "stx", ·····// filled in with table of //ASCII characters "w", "x", "y", "z","{", "|", "}", "~", "del" };

18 18 Printable program (3 of 3) int main() { pr_char c; for (int i = 0; i (i); c.print(); cout << endl; } }

19 19 A Stack Whose Size can Vary

20 20 Constructing A Dynamically Sized Stack Constructor can allocate space from free store ch_stack’s length initialized by constructor ch_stack has hidden implementation detail Data members placed in private access region Public interface provides ch_stack abstraction Public member functions, such as push() and pop()

21 21 The Stack Functions Accessor functions do not change ch_stack object, such as top_of() and empty() Usual to make these const member functions Mutator functions change ch_stack object, such as push() and pop() Constructor member functions create and initialize ch_stack objects

22 22 Constructor Initializer (1 of 2) Special syntax for initializing sub-elements of objects with constructors Initializers for class members in comma-separated list following constructor parameter list Initialization replaces assignment Individual members must be initializable as member-name (expression list)

23 23 Constructor Initializer (2 of 2) When members are themselves classes with constructors, expression list is matched to appropriate constructor signature When non-static member is either const or reference, this form of member initialization is required

24 24 ch_stack Constructors (1 of 2) ch_stack::ch_stack() : max_len (100), top (EMPTY) { //default constructor s = new char[100]; assert(s); } ch_stack::ch_stack(int size) : max_len (size), top(EMPTY) { //specific size request assert(size > 0); s = new char[size]; assert (s); }

25 25 ch_stack Constructors (2 of 2) //domain transfer ch_stack::ch_stack(int size, const char str[]) : max_len(size) { int i; assert (size > 0); s = new char[size]; assert(s); for (i=0; i < max_len && str[i]!=0; ++i) s[i] = str[i]; top = — —i; }

26 26 Classes With Destructors Member function whose name is the same as class name preceded by a tilde Called implicitly When exiting block in which object was declared When delete operator called on pointer to object having destructor When deleting a subobject of an object

27 27 Using delete Destructors usually involve delete delete expression delete [] expression Pointer variable used in assignment from a new expression Brackets used when allocation involves a bracketed expression Array of objects are allocated off of free store Destructors called on each object stored in array

28 28 Problems with delete Deletion of zero-valued pointer is harmless Multiple deletions of same object is error System-dependent behavior results Deletion of pointer value not gotten from new expression is an error System-dependent behavior results

29 29 Destructor Details Implicitly invoked when object out of scope Block and function exit my_string sub_str(char c, string b) { //friend of my_string my_string substring; for (int i = 0; i <= b.len; ++i) if (c == s[i]) break; substring.assign(s + i); return substring; }

30 30 Comments on sub_str() b is call-by-value argument of type my_string Copy constructor invoked to create a local copy Destructor is called on function exit substring constructed upon block entry and destructor invoked on block exit Return argument constructed and passed back into calling environment Whether corresponding destructor is invoked depends on scope of object

31 31 A Destructor for ch_stack class ch_stack { public:... ~ch_stack() { delete [] s; }... };

32 32 Comments on ch_stack with Destructor External class interface remains the same Destructor invoked implicitly on block and function exit to clean up storage that is no longer accessible Good programming practice Programs execute with less memory

33 33 Using the Constructors //default creates s[100] ch_stack data; //creates N 100 element ch_stacks ch_stack d[N]; //N is 100 elsewhere //construct from my_string //w.s[0]='A'...w.s[3]='D' w.s[4] = 'A' ch_stack w(5, "ABCDA"); //constructs from existing ch_stack ch_stack x(w);

34 34 The Copy Constructor and call-by-value Examine ch_stack and count number of occurrences of a given character Repeatedly pop ch_stack, testing each element To preserve ch_stack, call-by-value int cnt_char(char c, ch_stack s) { int count = 0; while (!s.empty()) count += (c == s.pop()); return count; }

35 35 ch_stack and Copy Constructor OriginalCopyDuring Original ch_stackof OriginalPop ch_stack ch_stackOperations Preserved

36 36 The Default Copy Constructor Call-by-value requires that local copy of argument type be created and initialized from value of expression passed as actual argument Compiler provides a default copy constructor ch_stack::ch_stack(const ch_stack&); Compiler copies by memberwise initialization Default copy constructor may not work in all circumstances for complicated aggregates with members who are themselves pointers

37 37 Scope & Deletion in Copy Constructors In many cases, pointer is address of object deleted when going out of scope Duplicating pointer value but not object pointed at can lead to anomalous code Deletion affects other instances that still expect object to exist object

38 38 Making a Copy Constructor for ch_stack ch_stack::ch_stack(const ch_stack& str): max_len(str.max_len),top(str.top) { s = new char[str.max_len]; assert(s); memcpy(s, str.s, max_len); }

39 39 Garbage Collection for Reclaiming Memory list example shows system reclaiming memory by traversing each list and disposing of each element Reclamation is a form of garbage collection In LISP, Java, and C#, system itself is responsible for reclamation Systems periodically invoke garbage collector to identify all cells currently accessible and reclaim remaining inaccessible cells

40 40 Cheshire Cat Data Hiding Indirection provides data hiding Use of separate class for underlying data representation Cheshire Cat technique, in honor of Lewis Carroll’s cat that disappeared leaving only a smile This is a Design Pattern

41 41 Cheshire Cat Version of ch_stack class ch_stack { public: void reset() { ptr -> reset(); } void push(char c) { ptr->push(c); } char pop() { return ptr->pop(); } char top_of() const { return ptr->top_of(); } bool empty() const { return ptr -> empty(); } bool full() const { return ptr -> full(); } private: ch_stk_rep* ptr; // opaque pointer };

42 42 Cheshire Cat Comments All data and underlying operations handled through ch_stk pointer ch_stack is known as wrapper class Relationship between wrapper class and underlying representation class is called handle design pattern

43 43 Reference Counts “Don’t shut me off, fool, there are still 3 users attached!”

44 44 Reference Counting Each object tracks its active references On object creation, reference count set to one When object newly referenced, reference count incremented Each time object loses a reference, count decremented Delete object when reference count is zero

45 45 Reference Counting Semantics Underlying, frequently hidden, representation defined to include reference count Wrapper publicly delivers objects and tracks reference count Attention is paid to construction, destruction and assignment of object values

46 46 Reference Counted String Class (1 of 4) #include //check compiler #include class str_obj { public: int len, ref_cnt; char* s; str_obj() : len(0), ref_cnt(1) { s = new char[1]; s[0] = 0; } str_obj(const char* p) : ref_cnt(1) { len = strlen(p); s = new char[len + 1];strcpy(s, p); } ~str_obj() { delete []s; } };

47 47 Reference Counted String Class (2 of 4) class my_string { public: my_string() { st = new str_obj; } my_string(const char* p) { st = new str_obj(p); } my_string(const my_string& str) { st = str.st; st -> ref_cnt++; } ~my_string(); void assign(const my_string& str); void print() const { cout s; } private: str_obj* st; };

48 48 Reference Counted String Class (3 of 4) void my_string::assign(const my_string& str) { if (str.st != st) { if (--st -> ref_cnt == 0) delete st; st = str.st; st -> ref_cnt++; } This could be replaced by overloaded =

49 49 Reference Counted String Class (4 of 4) my_string::~my_string() { if (--st -> ref_cnt == 0) delete st; }

50 50 Comments on Reference Count (1 of 2) Client uses objects of type my_string Objects implemented as pointers st to values of type str_obj Copy constructor uses reference semantics

51 51 Comments on Reference Count (2 of 2) Anytime object's reference count is decremented, test for deletion Large aggregate copied by reference, with a small, fixed number of operations Reference counter uses small storage Change to pointer adds to reference count Destructor must test reference count

52 52 Assign() Semantics Semantics of assign() show subtleties Assignment occurs if my_string not being assigned its same value Assignment causes assigned variable to lose its previous value Equivalent to decrementing reference count of pointed-at str_obj value When reference count decremented, it gets tested for deletion

53 53 Advantages Large aggregate is copied by reference, using few operations and small amount of storage for reference counter Each possible change to pointer adds reference- count operation

54 54 Summary Class constructors and destructors manage storage Constructors can be used for copy and conversion Code destructors to get rid of memory if you use new to create object Reference counting semantics can help with large aggregates


Download ppt "1 Classes Provide Constructors Mechanism to specify object creation Client can use objects similar to native types Constructor is member function with."

Similar presentations


Ads by Google