1 / 41

A Deeper Look at Classes

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).

Download Presentation

A Deeper Look at Classes

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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

  2. 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

  3. 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

  4. const Example A promise that these functionsdo not modify the object at all A Deeper Look at Classes

  5. Same here! const Example (continued) A Deeper Look at Classes

  6. Notconst const Example (continued) A Deeper Look at Classes

  7. constkeyword in function definition, as well as in function prototype const Example (continued) A Deeper Look at Classes

  8. And here! Same here! const Example (continued) A Deeper Look at Classes

  9. 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

  10. Questions? A Deeper Look at Classes

  11. 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

  12. Default Memberwise Assignment – Example Default initialization of data members A Deeper Look at Classes

  13. Default Memberwise Assignment (continued) A Deeper Look at Classes

  14. 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

  15. 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

  16. 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

  17. 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

  18. 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

  19. 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

  20. Questions? A Deeper Look at Classes

  21. New Topics friends and this A Deeper Look at Classes

  22. 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

  23. 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

  24. 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

  25. 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

  26. 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

  27. 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

  28. 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

  29. friend Function Example friendfunction declaration (can appear anywhere in the class) A Deeper Look at Classes

  30. 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

  31. Questions? A Deeper Look at Classes

  32. 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

  33. 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

  34. this Example A Deeper Look at Classes

  35. 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

  36. 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

  37. 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

  38. 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

  39. 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

  40. Questions? New Programming Assignmentwill be posted this weekend! A Deeper Look at Classes

  41. Next Week • Subclasses • Needed for next Programming Assignment • See Deitel & Deitel Chapter 23 • Operator Overloading • Deitel & Deitel Chapter 22 A Deeper Look at Classes

More Related