410 likes | 542 Views
This presentation delves into advanced C++ class concepts, emphasizing constant objects and member functions. It outlines the principle of least privilege, where access to data is granted only when necessary. Key topics include the const keyword, its role in ensuring object immutability, and the implications for constructors and destructors. The presentation also covers default memberwise assignment and the significance of copy constructors in managing object data correctly. Understand how these concepts contribute to robust software engineering practices and effective memory management.
E N D
A Deeper Look at Classes CS-2303System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition, by Kernighan and Ritchie and from C: How to Program, 5th and 6th editions, by Deitel and Deitel) A Deeper Look at Classes
const (Constant) Objects andconst 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
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
const Example A promise that these functionsdo not modify the object at all A Deeper Look at Classes
Same here! const Example (continued) A Deeper Look at Classes
Notconst const Example (continued) A Deeper Look at Classes
constkeyword in function definition, as well as in function prototype const Example (continued) A Deeper Look at Classes
And here! Same here! const Example (continued) A Deeper Look at Classes
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
Questions? A Deeper Look at Classes
Deitel & Deitel, §20.10 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 A Deeper Look at Classes
Default Memberwise Assignment – Example Default initialization of data members A Deeper Look at Classes
Default Memberwise Assignment (continued) A Deeper Look at Classes
Default Memberwise Assignment (continued) memberwise assignment assigns data members of date1 to date2 date2now stores the same date as date1 A Deeper Look at Classes
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
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
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
Why do we need '&'? 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.) A Deeper Look at Classes
The string copy constructor 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 A Deeper Look at Classes
Questions? A Deeper Look at Classes
New Topics friends and this A Deeper Look at Classes
Ordinary Member Functions • Function can access the private members of the class • Function is in the scope of the class • Function must be invoked on a specific object of the class – e.g., • ptr -> func() • obj.func() A Deeper Look at Classes
static Member Function • Function can access the private members of the class • Function is in the scope of the class • 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
friend Function • Function can access the private members of the class • Function is in the scope of the class • Function must be invoked on a specific object of the class – e.g., • ptr -> func() • obj.func() A Deeper Look at Classes
Deitel & Deitel, §21.4 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. A Deeper Look at Classes
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
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 classC. • … A Deeper Look at Classes
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
friend Function Example friendfunction declaration (can appear anywhere in the class) A Deeper Look at Classes
friend Function Example (continued) friendfunction can modify Count’s private data Calling a friend function; note that we pass the Countobject to the function A Deeper Look at Classes
Questions? A Deeper Look at Classes
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
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
this Example A Deeper Look at Classes
this Example Implicitly using the thispointer to access member x Explicitly using the this pointer to access member x Using the dereferenced thispointer and the dot operator A Deeper Look at Classes
Another Example Using this class TreeNode { public: ... /* other methods */ TreeNode(const string &newWord, TreeNode *parent); //constructor private: const string word; int count; TreeNode *left, *rightTreeNode *const myParent; }; A Deeper Look at Classes
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
Contructor of TreeNode with pointerback to parent node! Example Using this (continued) TreeNode *TreeNode::AddNode(const string &newWord){if (newWord == word){ incr(); return this;}else if (newWord < word) { if (left) return left->AddNode(newWord); else return left = new TreeNode(newWord, this);} else { /* same for right */} } // AddNode A Deeper Look at Classes
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 ); Calls t.setMinute( 30 ); Then calls t.setSecond( 22 ); See Deitel & Deitel, Figures 21.18–21.20 A Deeper Look at Classes
Questions? New Programming Assignmentwill be posted this weekend! A Deeper Look at Classes
Next Week • Subclasses • Needed for next Programming Assignment • See Deitel & Deitel Chapter 23 • Operator Overloading • Deitel & Deitel Chapter 22 A Deeper Look at Classes