A Deeper Look at Classes

Slides:



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

Constructor. 2 constructor The main use of constructors is to initialize objects. A constructor is a special member function, whose name is same as class.
Classes: A Deeper Look Systems Programming.  constconst  const objects and const member functions   Composition   Friendship  this  this pointer.
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.
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.
 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.
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.
More C++ Classes Systems Programming. C++ Classes  Preprocessor Wrapper  Time Class Case Study –Two versions (old and new)  Class Scope and Assessing.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 2.
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.
 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.
Chapter 9 Classes: A Deeper Look, Part I Part II.
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.
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.
Constructors and Destructors
Learning Objectives Pointers as dada members
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.
Andy Wang Object Oriented Programming in C++ COP 3330
Programming with ANSI C ++
Constructor & Destructor
Class: Special Topics Copy Constructors Static members Friends this
Memberwise Assignment / Initialization
Programming Assignment #4 Binary Trees in C++
Chapter 9 Classes: A Deeper Look, Part 1
10.2 const (Constant) Objects and const Member Functions
Dr. Bhargavi Dept of CS CHRIST
Constructors and destructors
Constructors and Destructors
“Under the Hood” of Polymorphism
Operator overloading Dr. Bhargavi Goswami
Classes, Constructors, etc., in C++
Containers and the Standard Template Library (STL)
Miscellaneous C++ Topics
Dynamic Memory Allocation (and Multi-Dimensional Arrays)
Derived Classes in C++ Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition,
Linked Lists in C and C++
Polymorphism Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition, by Kernighan.
Templates (again) Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition,
Symbolic Constants in C
Recursion and Implementation of Functions
Scope Rules and Storage Types
Programming Assignment #5
Constructors and Destructors
Iterators Professor Hugh C. Lauer CS-2303, System Programming Concepts
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.
Submitted By : Veenu Saini Lecturer (IT)
Recitation Course 0520 Speaker: Liu Yu-Jiun.
Operator Overloading; String and Array Objects
CS410 – Software Engineering Lecture #5: C++ Basics III
Classes: A Deeper Look, Part 1
More C++ Classes Systems Programming.
9-10 Classes: A Deeper Look.
Copy Constructors and Overloaded Assignment
Introduction to Classes and Objects
Presentation transcript:

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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