1 classes provide constructors mechanism to specify object creation client can use objects similar...

54
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

Upload: caitlin-swalwell

Post on 14-Dec-2015

216 views

Category:

Documents


0 download

TRANSCRIPT

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

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

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

2

Getting Correct Memory for the Task

“Where did I put that memory? I need more to get the job done.”

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

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

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

4

Using new

Initializer list

Expressions used by the object's constructor orsingle 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

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

5

Using Constructors and Destructors

Constructors are more complicatedMay be overloaded and take

arguments

Constructor invokedAssociated type used in definition

Call-by-value used to pass value to a function

Neither can use return statements

Destructors implicitly invoked

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

6

Constructors & Destructors

Form //can have multiple constructors class_name (parameter list); class_name (); //can have only 1 destructor ~class_name ();

Constructorsinitialization storage allocation

conversions

Destructorsfinalization storage reclamation

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

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

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

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;

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

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;}

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

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

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

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 listmod_int c = 49; //construct mod_int(49) //and copy into c

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

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

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

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

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

14

Default Constructor with mod_int

mod_int() { v = 0; }

Possible to have the following declarationsmod_int s1, s2; //init private v = 0mod_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; }

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

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

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

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];};

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

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" };

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

18

Printable program (3 of 3)

int main(){   pr_char  c;   for (int i = 0; i < 128; ++i) {      c = i;  // or c = static_cast<pr_char>(i);      c.print();      cout << endl;   }}

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

19

A Stack Whose Size can Vary

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

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 detailData members placed in private access

region

Public interface provides ch_stack abstraction

Public member functions, such as push() and pop()

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

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 functionscreate and initialize ch_stackobjects

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

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)

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

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

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

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);}

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

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;}

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

26

Classes With Destructors

Member function whose name is the same as class name preceded by a tilde

Called implicitlyWhen exiting block in

which object was declared

When delete operator called on pointer to object having destructor

When deleting a subobjectof an object

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

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

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

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 gottenfrom new expression is an error

System-dependent behavior results

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

29

Destructor Details

Implicitly invoked when object out of scopeBlock 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;}

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

30

Comments on sub_str()

b is call-by-value argument of type my_stringCopy 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

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

31

A Destructor for ch_stack

class ch_stack {public: . . . ~ch_stack() { delete [] s; } . . .};

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

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

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

33

Using the Constructors

//default creates s[100]ch_stack data;

//creates N 100 element ch_stacksch_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_stackch_stack x(w);

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

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;}

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

35

ch_stack and Copy Constructor

Original Copy During Originalch_stack of Original Pop

ch_stackch_stack Operations Preserved

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

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

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

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

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

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);}

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

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

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

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

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

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};

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

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

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

43

Reference Counts

“Don’t shut me off,

fool, there are still 3

users attached!”

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

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

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

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

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

46

Reference Counted String Class (1 of 4)

#include <string.h> //check compiler#include <iostream.h>

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; }};

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

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 << st -> s; }private: str_obj* st;};

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

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 =

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

49

Reference Counted String Class (4 of 4)

my_string::~my_string(){ if (--st -> ref_cnt == 0) delete st;}

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

50

Comments on Reference Count (1 of 2)

Client uses objects of type my_stringObjects implemented as pointers st

to values of type str_obj

Copy constructor uses reference semantics

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

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

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

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

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

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

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

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