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.

Slides:



Advertisements
Similar presentations
Chapter 4 Constructors and Destructors. Objectives Constructors – introduction and features The zero-argument constructor Parameterized constructors Creating.
Advertisements

 2008 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 2.
Classes: A Deeper Look Systems Programming.  constconst  const objects and const member functions   Composition   Friendship  this  this pointer.
Class and Objects.
Derived Classes in C++CS-2303, C-Term Derived Classes in C++ CS-2303 System Programming Concepts (Slides include materials from The C Programming.
Chapter 14: Overloading and Templates
Function Overloading Can enables several function Of same name Of different sets of parameters (at least as far as their types are concerned) Used to create.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 1.
More on Operator Overloading CS-2303, C-Term More on Operator Overloading CS-2303 System Programming Concepts (Slides include materials from The.
Classes: A Deeper Look Systems Programming.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 2.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 2.
 2006 Pearson Education, Inc. All rights reserved. Templates (again)CS-2303, C-Term Templates (again) CS-2303 System Programming Concepts (Slides.
Introduction to Classes and Objects CS-2303, C-Term Introduction to Classes and Objects CS-2303 System Programming Concepts (Slides include materials.
CS-2303 System Programming Concepts
1 Classes and Objects. 2 Outlines Class Definitions and Objects Member Functions Data Members –Get and Set functions –Constructors.
Operator OverloadingCS-2303, C-Term Operator Overloading CS-2303 System Programming Concepts (Slides include materials from The C Programming Language,
Review of C++ Programming Part II Sheng-Fang Huang.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 15: Overloading and Templates.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 2.
You gotta be cool. Access Functions and Utility Functions Preprocessor Wrapper Looking Ahead to Composition and Inheritance Object Size Class Scope and.
Chapter 10 Classes: A Deeper Look, Part II Part I.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Classes: A Deeper Look Part.
More C++ Classes Systems Programming. C++ Classes  Preprocessor Wrapper  Time Class Case Study –Two versions (old and new)  Class Scope and Assessing.
CS212: Object Oriented Analysis and Design Lecture 6: Friends, Constructor and destructors.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 2.
Classes: A Deeper Look, Part But what, to serve our private ends, Forbids the cheating of our friends? ◦ Charles Churchill Instead of this absurd.
CS212: Object Oriented Analysis and Design Lecture 7: Arrays, Pointers and Dynamic Memory Allocation.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Classes: A Deeper Look Part.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 2.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 2.
 Classes in c++ Presentation Topic  A collection of objects with same properties and functions is known as class. A class is used to define the characteristics.
Visual C# 2012 for Programmers © by Pearson Education, Inc. All Rights Reserved.
CLASSES : A DEEPER LOOK Chapter 9 Part I 1. 2 OBJECTIVES In this chapter you will learn: How to use a preprocessor wrapper to prevent multiple definition.
Chapter 9 Classes: A Deeper Look, Part I Part II.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 17 - C++ Classes: Part II Outline 17.1Introduction.
Chapter 17 - C++ Classes: Part II Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
CS-1030 Dr. Mark L. Hornick 1 Basic C++ State the difference between a function/class declaration and a function/class definition. Explain the purpose.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 15: Overloading and Templates.
C++ Lecture 5 Monday, 18 July Chapter 7 Classes, continued l const objects and const member functions l Composition: objects as members of classes.
1 CSC241: Object Oriented Programming Lecture No 05.
Programming II Array of objects. this Using the this Pointer this Objects use the this pointer implicitly or explicitly. – this is – this is used implicitly.
1 Classes II Chapter 7 2 Introduction Continued study of –classes –data abstraction Prepare for operator overloading in next chapter Work with strings.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 1.
Programming Techniques Classes II Important Class Features Spring 2009.
Chapter 9 Classes: A Deeper Look, Part 1 Seventh Edition C++ How to Program © by Pearson Education, Inc. All Rights Reserved.
Object Based Programming Chapter 8. 2 Contrast ____________________ Languages –Action oriented –Concentrate on writing ________________ –Data supports.
Learning Objectives Pointers as dada members
Function Overloading Can enables several function Of same name
CSC241: Object Oriented Programming
Friend Class Friend Class A friend class can access private and protected members of other class in which it is declared as friend. It is sometimes useful.
Class: Special Topics Copy Constructors Static members Friends this
Chapter 17 - C++ Classes: Part II
CS212: Object Oriented Analysis and Design
10.2 const (Constant) Objects and const Member Functions
Classes, Constructors, etc., in C++
Derived Classes in C++ Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition,
Templates (again) Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition,
Classes: A Deeper Look, Part 2
Operator Overloading Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition,
9-10 Classes: A Deeper Look.
Recitation Course 0520 Speaker: Liu Yu-Jiun.
Classes: A Deeper Look, Part 2
Classes: A Deeper Look, Part 1
A Deeper Look at Classes
More C++ Classes Systems Programming.
9-10 Classes: A Deeper Look.
Introduction to Classes and Objects
Presentation transcript:

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