Chapter 9 Classes: A Deeper Look, Part I Part II.

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.
OOP Using Classes - I. Structures vs. Classes C-style structures –No “interface” If implementation changes, all programs using that struct must change.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 1.
 2006 Pearson Education, Inc. All rights reserved Midterm review Introduction to Classes and Objects.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 1.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 16: Classes and Data Abstraction Outline 16.1Introduction.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 16: Classes and Data Abstraction Outline 16.1Introduction 16.2Implementing a Time Abstract Data.
Classes: A Deeper Look Systems Programming.
Introduction to Classes and Data Abstraction
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.
More C++ Classes Systems Programming. Systems Programming: C++ Classes 2 Systems Programming: 2 C++ Classes  Preprocessor Wrapper  Time Class Case Study.
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
Shallow Versus Deep Copy and Pointers Shallow copy: when two or more pointers of the same types point to the same memory – They point to the same data.
1 Classes and Objects. 2 Outlines Class Definitions and Objects Member Functions Data Members –Get and Set functions –Constructors.
Review of C++ Programming Part II Sheng-Fang Huang.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look.
You gotta be cool. Access Functions and Utility Functions Preprocessor Wrapper Looking Ahead to Composition and Inheritance Object Size Class Scope and.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 1.
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.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 13 Introduction to Classes.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 7 Structured Data and Classes.
C++ Lecture 4 Tuesday, 15 July Struct & Classes l Structure in C++ l Classes and data abstraction l Class scope l Constructors and destructors l.
計算機程式語言 Lecture 09-1 國立台灣大學生物機電系 林達德 9 9 Classes: A Deeper Look, Part 1.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 2.
SMIE-121 Software Design II School of Mobile Information Engineering, Sun Yat-sen University Lecture.
Classes In C++ 1. What is a class Can make a new type in C++ by declaring a class. A class is an expanded concept of a data structure: instead of holding.
 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.
Lecture 3 Classes, Structs, Enums Passing by reference and value Arrays.
CPSC 252 The Big Three Page 1 The “Big Three” Every class that has data members pointing to dynamically allocated memory must implement these three methods:
1 Classes and Data Abstraction 2 Objectives Understand encapsulation and data hiding Understand data abstraction and Abstract Data Types (ADTs) Create.
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.
Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 1.
 2008 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 1.
Class 4 (L34) u Constructors u Default Constructor u Example of Default Constructors u Destructors u Constructors Are Called in Global Scope u Constructors.
Chapter 10: Classes and Data Abstraction. Objectives In this chapter, you will: Learn about classes Learn about private, protected, and public members.
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 Lecture 6 Classes and Data Abstraction: Part II.
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.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 1.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 11: Classes and Data Abstraction.
Chapter 10: Classes and Data Abstraction. Classes Object-oriented design (OOD): a problem solving methodology Objects: components of a solution Class:
Chapter 9 Classes: A Deeper Look, Part 1 Seventh Edition C++ How to Program © by Pearson Education, Inc. All Rights Reserved.
Object Access m1.write(44); m2.write(m2.read() +1); std::cout
Chapter 16: Classes and Data Abstraction
Pointer to an Object Can define a pointer to an object:
Chapter 16: Classes and Data Abstraction
Default Constructors A default constructor is a constructor that takes no arguments. If you write a class with no constructor at all, C++ will write a.
Memberwise Assignment / Initialization
Classes: A Deeper Look Outline
Chapter 9 Classes: A Deeper Look, Part 1
10.2 const (Constant) Objects and const Member Functions
Constructors and destructors
Classes: A Deeper Look, Part 1
9-10 Classes: A Deeper Look.
Recitation Course 0520 Speaker: Liu Yu-Jiun.
Classes: A Deeper Look, Part 1
A Deeper Look at Classes
Chapter 8 Classes and Objects: A Deeper Look
More C++ Classes Systems Programming.
9-10 Classes: A Deeper Look.
SPL – PS3 C++ Classes.
Presentation transcript:

Chapter 9 Classes: A Deeper Look, Part I Part II

9.5 Access Functions and Utility Functions Access functions ◦ To read or display data ◦ To test the truth or falsity of conditions  Also called predicate functions  For example, isEmpty function for a class capable of holding many objects Utility functions (also called helper functions) ◦ private member functions that support the operation of the public member functions ◦ Not part of a class ’ s public interface

Prototype for a private utility function

Calling a private utility function Definition of a private utility function

9.6 Time Class Case Study: Constructors with Default Arguments Constructors can specify default arguments ◦ Can initialize data members e ven if no values are provided in a constructor call ◦ Constructor that defaults all its arguments is also a default constructor  Can be invoked with no arguments  Note:  Maximum of one default constructor per class

9 Prototype of a constructor with default arguments

Parameters could receive the default values

Initializing Time objects using 0, 1, 2 and 3 arguments

Invalid values passed to constructor, so object t5 contains all default data

Indicate Any Error class Time { public: Time(int=0, int=0, int=0); Time(int=0, int=0); …. };

Indicate Any Error //Time.h class Time { public: Time(int=0, int=0, int=0); …. private: int hour, minute, second; }; //Time.cpp Time::Time(int h=0, int m=0, int s=0) { hour = h; minute = m; second = s; }

Indicate Any Error //Time.h class Time { public: Time(int=0, int=0, int=0); …. private: int hour, minute, second; }; //Time.cpp Time::Time(int hour, int minute, int second) { hour = hour; minute = minute; second = second; }

Indicate Any Error //Time.h class Time { public: Time(int=0, int=0, int=0); …. private: int hour, minute, second; }; //Time.cpp Time::Time(int h, int m, int s) { int hour = h; int minute = m; int second = s; }

9.7 Destructors Destructor ◦ Name is the tilde character ( ~ ) followed by the class name, e.g., ~Time ◦ Called implicitly when an object is destroyed  For example, this occurs as an automatic object is destroyed when program execution leaves the scope in which that object was instantiated ◦ Does not actually release the object ’ s memory  It performs termination housekeeping  Then the system reclaims the object ’ s memory  So the memory may be reused to hold new objects

9.7 Destructors ◦ Receives no parameters and returns no value  May not specify a return type — not even void ◦ A class may have only one destructor ◦ If the programmer does not explicitly provide a destructor, the compiler creates an “ empty ” destructor

9.8 When Constructors and Destructors Are Called For an automatic local object ◦ Constructor is called when that object is defined. ◦ Corresponding destructor is called when execution leaves the object ’ s scope. ◦ Automatic object destructors are not called if the program terminates with an exit or abort function

When Constructors and Destructors Are Called For a static local object ◦ Constructor is called only once  When execution first reaches where the object is defined ◦ Destructor is called when main terminates or the program calls function exit  Destructor is not called if the program terminates with a call to function abort Global and static objects are destroyed in the reverse order of their creation

Prototype for destructor

Defining the class’s destructor

Object created outside of main Local automatic object created in main Local static object created in main Local automatic object created in main

Local automatic object created in create Local static object created in create Local automatic object created in create

9.9 Time Class Case Study: A Subtle Trap— Returning a Reference to a private Data Member Returning a reference to an object ◦ Alias for the name of an object  An acceptable lvalue that can receive a value  May be used on the left side of an assignment statement  If a function returns a const reference  That reference cannot be used as a modifiable lvalue

Prototype for function that returns a reference

Returning a reference to a private data member = DANGEROUS!

Modifying a private data member through a returned reference

Modifying private data by using a function call as an lvalue

9.9 Time Class Case Study: A Subtle Trap— Returning a Reference to a private Data Member One dangerous way to use this capability ◦ A public member function of a class returns a reference to a private data member of that class  Client code could alter private data  Same problem would occur if a pointer to private data were returned

Error-Prevention Tip 9.4 Returning a reference or a pointer to a private data member breaks the encapsulation of the class and makes the client code dependent on the representation of the class ’ s data. So, returning pointers or references to private data is a dangerous practice that should be avoided.

9.10 Default Memberwise Assignment 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  Can cause serious problems when data members contain pointers to dynamically allocated memory

Memberwise assignment assigns data members of date1 to date2 date2 now stores the same date as date1

9.10 Default Memberwise Assignment Copy constructor ◦ 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 ◦ 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) ◦ Also can cause serious problems when data members contain pointers to dynamically allocated memory

Performance Tip 9.3 Passing an object by value is good from a security standpoint, because the called function has no access to the original object in the caller, but pass-by-value can degrade performance when making a copy of a large object. Pass-by-reference offers good performance but is weaker from a security standpoint, because the called function is given access to the original object. Pass-by- const -reference is a safe, good-performing alternative ◦ this can be implemented with a const reference parameter or with a pointer-to- const -data parameter.