Presentation is loading. Please wait.

Presentation is loading. Please wait.

A Deeper Look at Classes CS-2303, C-Term 20101 A Deeper Look at Classes CS-2303 System Programming Concepts (Slides include materials from The C Programming.

Similar presentations


Presentation on theme: "A Deeper Look at Classes CS-2303, C-Term 20101 A Deeper Look at Classes CS-2303 System Programming Concepts (Slides include materials from The C Programming."— Presentation transcript:

1 A Deeper Look at Classes CS-2303, C-Term 20101 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)

2 A Deeper Look at Classes CS-2303, C-Term 20102 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);

3 A Deeper Look at Classes CS-2303, C-Term 20103 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!

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

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

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

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

8 A Deeper Look at Classes CS-2303, C-Term 20108 const Example (continued) Same here! And here!

9 A Deeper Look at Classes CS-2303, C-Term 20109 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.

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

11 A Deeper Look at Classes CS-2303, C-Term 201011 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

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

13 A Deeper Look at Classes CS-2303, C-Term 201013 Default Memberwise Assignment (continued)

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

15 A Deeper Look at Classes CS-2303, C-Term 201015 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

16 A Deeper Look at Classes CS-2303, C-Term 201016 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; };

17 A Deeper Look at Classes CS-2303, C-Term 201017 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

18 A Deeper Look at Classes CS-2303, C-Term 201018 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 '&' ?

19 A Deeper Look at Classes CS-2303, C-Term 201019 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

20 A Deeper Look at Classes CS-2303, C-Term 201020 Questions?

21 A Deeper Look at Classes CS-2303, C-Term 201021 New Topics friends and this

22 A Deeper Look at Classes CS-2303, C-Term 201022 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()

23 A Deeper Look at Classes CS-2303, C-Term 201023 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

24 A Deeper Look at Classes CS-2303, C-Term 201024 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()

25 A Deeper Look at Classes CS-2303, C-Term 201025 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

26 A Deeper Look at Classes CS-2303, C-Term 201026 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

27 A Deeper Look at Classes CS-2303, C-Term 201027 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. …

28 A Deeper Look at Classes CS-2303, C-Term 201028 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.

29 A Deeper Look at Classes CS-2303, C-Term 201029 friend function declaration (can appear anywhere in the class) friend Function Example

30 A Deeper Look at Classes CS-2303, C-Term 201030 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)

31 A Deeper Look at Classes CS-2303, C-Term 201031 Questions?

32 A Deeper Look at Classes CS-2303, C-Term 201032 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

33 A Deeper Look at Classes CS-2303, C-Term 201033 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.

34 A Deeper Look at Classes CS-2303, C-Term 201034 this Example

35 A Deeper Look at Classes CS-2303, C-Term 201035 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

36 A Deeper Look at Classes CS-2303, C-Term 201036 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; };

37 A Deeper Look at Classes CS-2303, C-Term 201037 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

38 A Deeper Look at Classes CS-2303, C-Term 201038 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!

39 A Deeper Look at Classes CS-2303, C-Term 201039 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

40 A Deeper Look at Classes CS-2303, C-Term 201040 Questions? New Programming Assignment will be posted this weekend!

41 A Deeper Look at Classes CS-2303, C-Term 201041 Next Week Subclasses Needed for next Programming Assignment See Deitel & Deitel Chapter 23 Operator Overloading Deitel & Deitel Chapter 22


Download ppt "A Deeper Look at Classes CS-2303, C-Term 20101 A Deeper Look at Classes CS-2303 System Programming Concepts (Slides include materials from The C Programming."

Similar presentations


Ads by Google