Presentation is loading. Please wait.

Presentation is loading. Please wait.

A Deeper Look at Classes

Similar presentations


Presentation on theme: "A Deeper Look at Classes"— Presentation transcript:

1 A Deeper Look at Classes
Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition, by Kernighan and Ritchie, Absolute C++, by Walter Savitch, The C++ Programming Language, Special Edition, by Bjarne Stroustrup, and from C: How to Program, 5th and 6th editions, by Deitel and Deitel) CS-2303, A-Term 2012 A Deeper Look at Classes

2 const Objects & 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); CS-2303, A-Term 2012 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! CS-2303, A-Term 2012 A Deeper Look at Classes

4 A Deeper Look at Classes
const Example A promise that these functions do not modify the object at all CS-2303, A-Term 2012 A Deeper Look at Classes

5 const Example (continued)
Same here! CS-2303, A-Term 2012 A Deeper Look at Classes

6 const Example (continued)
Not const CS-2303, A-Term 2012 A Deeper Look at Classes

7 const Example (continued)
const keyword in function definition, as well as in function prototype CS-2303, A-Term 2012 A Deeper Look at Classes

8 const Example (continued)
Same here! And here! CS-2303, A-Term 2012 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. Get used to using const everywhere! For methods, parameters, etc. CS-2303, A-Term 2012 A Deeper Look at Classes

10 A Deeper Look at Classes
Questions? CS-2303, A-Term 2012 A Deeper Look at Classes

11 Default Memberwise Assignment
Absolute C++, §8.3 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! Usually causes serious problems when data members contain pointers to dynamically allocated memory !! Because pointers are simply copied CS-2303, A-Term 2012 A Deeper Look at Classes

12 Default Memberwise Assignment – Example
Default initialization of data members CS-2303, A-Term 2012 A Deeper Look at Classes

13 Default Memberwise Assignment (continued)
memberwise assignment assigns data members of date1 to date2 date2 now stores the same date as date1 CS-2303, A-Term 2012 A Deeper Look at Classes

14 A Deeper Look at Classes
But … What happens if we use the default assignment operator on TreeNode? class TreeNode { public: ... private: const string word; int count; TreeNode *left, *right; }; // class TreeNode CS-2303, A-Term 2012 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 Need to overload the ‘=‘ operator CS-2303, A-Term 2012 A Deeper Look at Classes

16 A Deeper Look at Classes
Questions? CS-2303, A-Term 2012 A Deeper Look at Classes

17 A Deeper Look at Classes
Copy Constructor Definition A constructor that has a single parameter of the form ClassName(ClassName &objectToBeCopied); Normally A constructor that has a single constant parameter of the form ClassName(const ClassName &objectToBeCopied); CS-2303, A-Term 2012 A Deeper Look at Classes

18 Purpose of Copy Constructor
To make a new object just like the previous object i.e., to make a copy of the previous object Intelligently E.g., string S1("Now is the time …"); … string S2(S1); S1 contains an internal array of char S2 now contains its own, separate internal array of char CS-2303, A-Term 2012 A Deeper Look at Classes

19 A Deeper Look at Classes
Copy Constructors … are used widely in programs … are used implicitly by compiler whenever objects are passed by value! Passing objects to parameters Returning results from functions Initializing uninitialized memory from previous values CS-2303, A-Term 2012 A Deeper Look at Classes

20 A Deeper Look at Classes
Copy Constructor A constructor that copies another object of the same type — e.g.:– class TreeNode { public: ... /* other methods */ TreeNode(const TreeNode &nodeToBeCopied); // Copy Constructor private: const string word; int count; TreeNode *left, *right; }; CS-2303, A-Term 2012 A Deeper Look at Classes

21 Default Copy Constructor
Compiler provides a default copy constructor Copies each member of original object into corresponding member of 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 CS-2303, A-Term 2012 A Deeper Look at Classes

22 A Deeper Look at Classes
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 (including internal pointers, etc.) Why do we need '&'? CS-2303, A-Term 2012 A Deeper Look at Classes

23 Usage of Copy Constructor
In Initializer list — E.g.:– 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 CS-2303, A-Term 2012 A Deeper Look at Classes

24 A Deeper Look at Classes
Questions? CS-2303, A-Term 2012 A Deeper Look at Classes

25 A Deeper Look at Classes
The “this” Pointer Member functions know which object’s data members to manipulate Every object has access to own address through a pointer called this (a C++ keyword) Object’s this pointer is not part of object itself this pointer is passed (by the compiler) as an implicit argument to each of object’s non-static member functions CS-2303, A-Term 2010 A Deeper Look at Classes

26 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 whether member function is declared const CS-2303, A-Term 2010 A Deeper Look at Classes

27 A Deeper Look at Classes
Example using “this” CS-2303, A-Term 2010 A Deeper Look at Classes

28 Example using “this” (continued)
Directly accessing member x Explicitly using the this pointer to access member x Using the dereferenced this pointer and the dot operator CS-2303, A-Term 2010 A Deeper Look at Classes

29 Another Example Using “this”
class ExtendTreeNode { public: ... /* other methods */ ExtendTreeNode(const string &newWord, ExtendTreeNode *parent); //constructor private: const string word; int count; ExtendTreeNode *left, *right ExtendTreeNode *const myParent; }; CS-2303, A-Term 2010 A Deeper Look at Classes

30 Example Using “this” (continued)
ExtendTreeNode::ExtendTreeNode(const string &newWord, ExtendTreeNode *parent) :word(newWord), //initialize word count(1), //initialize count left(NULL), right(NULL), myParent(parent) //point back to myParent { /* rest of constructor body */ } // ExtendTreeNode constructor CS-2303, A-Term 2010 A Deeper Look at Classes

31 Example Using “this”(continued)
ExtendTreeNode *ExtendTreeNode::AddNode(const string &newWord){ if (newWord == word){ count++; return this; } else if (newWord < word) { if (left) return left->AddNode(newWord); else return left = new ExtendTreeNode(newWord, this); } else { /* same for right */ } } // AddNode Constructor of ExtendTreeNode with pointer back to parent node! CS-2303, A-Term 2010 A Deeper Look at Classes

32 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 ) and returns reference to t Then calls t.setSecond( 22 ); CS-2303, A-Term 2010 A Deeper Look at Classes

33 Cascaded calls (continued)
Absolute C++, §8.3 (end) Cascaded calls (continued) Instead of void setMinute (const int ); Use Time& setMinute (const int ); Implementation Time& Time::setMinute (const int m){ minute = m; return *this; } Note reference result CS-2303, A-Term 2010 A Deeper Look at Classes

34 A Deeper Look at Classes
Reference Result A function may return a reference to an object (instead of a value) Value  copy of an object Reference  object itself Reference result must not be automatic variable Compiler checks Reference result may be used wherever an object of same type/class may be used Subject to const rules Especially useful for overloaded operators CS-2303, A-Term 2010 A Deeper Look at Classes

35 A Deeper Look at Classes
Questions? CS-2303, A-Term 2010 A Deeper Look at Classes


Download ppt "A Deeper Look at Classes"

Similar presentations


Ads by Google