A Deeper Look at Classes CS-2303, C-Term A Deeper Look at Classes CS-2303 System Programming Concepts (Slides include materials from The C Programming Language, 2 nd edition, by Kernighan and Ritchie and from C: How to Program, 5 th and 6 th editions, by Deitel and Deitel)
A Deeper Look at Classes CS-2303, C-Term const (Constant) Objects and const Member Functions Principle of Least Privilege –“Allow access to data only when absolutely needed” –Fundamental principle of good software engineering const objects –Keyword const –Specifies that an object is not modifiable –Attempts to modify const object compilation errors Example –const Time noon (12, 0, 0);
A Deeper Look at Classes CS-2303, C-Term const Member Functions Only const member functions can be called for const objects Member functions declared const may not modify the object in any way A function is specified as const both prototype and definition. Constructors and destructors are not const –By definition!
A Deeper Look at Classes CS-2303, C-Term const Example A promise that these functions do not modify the object at all
A Deeper Look at Classes CS-2303, C-Term const Example (continued) Same here!
A Deeper Look at Classes CS-2303, C-Term const Example (continued) Not const
A Deeper Look at Classes CS-2303, C-Term const keyword in function definition, as well as in function prototype const Example (continued)
A Deeper Look at Classes CS-2303, C-Term const Example (continued) Same here! And here!
A Deeper Look at Classes CS-2303, C-Term Purpose of const To enlist the aid of the compiler in detecting unwanted changes to objects Widely used in large programming projects Helps maintain sanity, teamwork, etc.
A Deeper Look at Classes CS-2303, C-Term Questions?
A Deeper Look at Classes CS-2303, C-Term Default Memberwise Assignment Assignment operator ( = ) –Can be used to assign an object to another object of the same type. Each data member of the right object is assigned to the same data member in the left object. Not usually want you want to do! –Often causes serious problems when data members contain pointers to dynamically allocated memory !! Because pointers are simply copied Deitel & Deitel, §20.10
A Deeper Look at Classes CS-2303, C-Term Default initialization of data members Default Memberwise Assignment – Example
A Deeper Look at Classes CS-2303, C-Term Default Memberwise Assignment (continued)
A Deeper Look at Classes CS-2303, C-Term memberwise assignment assigns data members of date1 to date2 date2 now stores the same date as date1 Default Memberwise Assignment (continued)
A Deeper Look at Classes CS-2303, C-Term Default Memberwise Assignment (concluded) Many classes must provide their own assignment operator To intelligently assign one object to another More about assignment of objects to each other when we get to Operator Overloading The '=' operator can be overloaded, just like any other
A Deeper Look at Classes CS-2303, C-Term Copy Constructor A constructor that copies another object of the same type Example:– class TreeNode { public:.../* other methods */ TreeNode(const TreeNode &nodeToBeCopied); // Copy Constructor private: const string word; int count; TreeNode *left, *right; };
A Deeper Look at Classes CS-2303, C-Term Default Copy Constructor Compiler provides a default copy constructor –Copies each member of the original object into the corresponding member of the new object –i.e., memberwise assignment Enables pass-by-value for objects –Used to copy original object’s values into new object to be passed to a function or returned from a function Not usually want you want to do! –Often causes serious problems when data members contain pointers to dynamically allocated memory !! Just like default memberwise assignment
A Deeper Look at Classes CS-2303, C-Term Copy Constructor Many classes must provide an explicit Copy Constructor To intelligently copy one object to another Example:– string(const string &stringToBeCopied); Allocates new memory for the new string Copies characters from one to other Does not blindly copy members (include internal pointers, etc.) Why do we need '&' ?
A Deeper Look at Classes CS-2303, C-Term Usage of Copy Constructor In Initializer list Example:– TreeNode::TreeNode(const string &newWord) :word(newWord),//initialize word count(1),//initialize count left(NULL), right(NULL) { /* rest of constructor body */ }//TreeNode constructor The string copy constructor
A Deeper Look at Classes CS-2303, C-Term Questions?
A Deeper Look at Classes CS-2303, C-Term New Topics friends and this
A Deeper Look at Classes CS-2303, C-Term Ordinary Member Functions 1.Function can access the private members of the class 2.Function is in the scope of the class 3.Function must be invoked on a specific object of the class – e.g., ptr -> func() obj.func()
A Deeper Look at Classes CS-2303, C-Term static Member Function 1.Function can access the private members of the class 2.Function is in the scope of the class 3.Function must be invoked on a specific object of the class – e.g., ptr -> func() obj.func() But only the static members Members that exist independently of any objects
A Deeper Look at Classes CS-2303, C-Term friend Function 1.Function can access the private members of the class 2.Function is in the scope of the class 3.Function must be invoked on a specific object of the class – e.g., ptr -> func() obj.func()
A Deeper Look at Classes CS-2303, C-Term friend Function of a Class Defined outside that class’s scope. Not a member function of that class. Has right to access non-public and public members of that class. Often appropriate when a member function cannot be used for certain operations. Can enhance performance. Deitel & Deitel, §21.4
A Deeper Look at Classes CS-2303, C-Term friend Functions and friend Classes To declare a function as a friend of a class:– –Provide the function prototype in the class definition preceded by keyword friend To declare a class as a friend of another class: –Place a declaration of the form – friend class ClassTwo; in the definition of class ClassOne All member functions of class ClassTwo are friends of class ClassOne
A Deeper Look at Classes CS-2303, C-Term friend Functions and friend Classes (continued) Friendship is granted, not taken. –For class B to be a friend of class A, class A must explicitly declare that class B is its friend. Friendship relation is neither symmetric nor transitive –If class A is a friend of class B, and class B is a friend of class C, cannot infer that class B is a friend of class A, class C is a friend of class B, class A is a friend of class C. …
A Deeper Look at Classes CS-2303, C-Term friend Functions and friend Classes (continued) … It is possible to specify overloaded functions as friends of a class. –Each overloaded function intended to be a friend must be explicitly declared as a friend of the class.
A Deeper Look at Classes CS-2303, C-Term friend function declaration (can appear anywhere in the class) friend Function Example
A Deeper Look at Classes CS-2303, C-Term friend function can modify Count’s private data Calling a friend function; note that we pass the Count object to the function friend Function Example (continued)
A Deeper Look at Classes CS-2303, C-Term Questions?
A Deeper Look at Classes CS-2303, C-Term The this Pointer Member functions know which object’s data members to manipulate Every object has access to its own address through a pointer called this (a C++ keyword) An object’s this pointer is not part of the object itself The this pointer is passed (by the compiler) as an implicit argument to each of the object’s non- static member functions
A Deeper Look at Classes CS-2303, C-Term Using the this Pointer Objects may use the this pointer implicitly or explicitly. this is used implicitly when accessing members directly. It is used explicitly when using keyword this. Type of the this pointer depends on –type of the object, and –whether member function is declared const.
A Deeper Look at Classes CS-2303, C-Term this Example
A Deeper Look at Classes CS-2303, C-Term Implicitly using the this pointer to access member x Explicitly using the this pointer to access member x Using the dereferenced this pointer and the dot operator this Example
A Deeper Look at Classes CS-2303, C-Term Another Example Using this class TreeNode { public:.../* other methods */ TreeNode(const string &newWord, TreeNode *parent);//constructor private: const string word; int count; TreeNode *left, *right TreeNode *const myParent; };
A Deeper Look at Classes CS-2303, C-Term Example Using this (continued) TreeNode::TreeNode(const string &newWord, TreeNode *parent) :word(newWord),//initialize word count(1),//initialize count left(NULL), right(NULL), myParent(parent)//initialize myParent { /* rest of constructor body */ }//TreeNode constructor
A Deeper Look at Classes CS-2303, C-Term Example Using this (continued) TreeNode *TreeNode::AddNode(const string &newWord){ if (newWord == word){ incr(); return this; } else if (newWord AddNode(newWord); else return left = new TreeNode(newWord, this); } else { /* same for right */ } }// AddNode Contructor of TreeNode with pointer back to parent node!
A Deeper Look at Classes CS-2303, C-Term Cascaded member-function calls Multiple functions are invoked in the same statement. Enabled by member functions returning the dereferenced this pointer. Example –t.setMinute( 30 ).setSecond( 22 ); t.setMinute( 30 );Calls t.setMinute( 30 ); t.setSecond( 22 );Then calls t.setSecond( 22 ); See Deitel & Deitel, Figures 21.18–21.20
A Deeper Look at Classes CS-2303, C-Term Questions? New Programming Assignment will be posted this weekend!
A Deeper Look at Classes CS-2303, C-Term Next Week Subclasses Needed for next Programming Assignment See Deitel & Deitel Chapter 23 Operator Overloading Deitel & Deitel Chapter 22