Chapter 9 Classes: A Deeper Look, Part 1 Seventh Edition C++ How to Program ©1992-2010 by Pearson Education, Inc. All Rights Reserved.

Slides:



Advertisements
Similar presentations
C++ How to Program, 7/e © by Pearson Education, Inc. All Rights Reserved.
Advertisements

Classes: A Deeper Look Systems Programming.  constconst  const objects and const member functions   Composition   Friendship  this  this pointer.
 2006 Pearson Education, Inc. All rights reserved Functions.
OOP Using Classes - I. Structures vs. Classes C-style structures –No “interface” If implementation changes, all programs using that struct must change.
Chapter 14: Overloading and Templates C++ Programming: Program Design Including Data Structures, Fifth Edition.
Chapter 14: Overloading and Templates
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
 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.
 2008 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 16: Classes and Data Abstraction Outline 16.1Introduction.
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.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 9 Objects and Classes.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Chapter Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
More C++ Classes Systems Programming. Systems Programming: C++ Classes 2 Systems Programming: 2 C++ Classes  Preprocessor Wrapper  Time Class Case Study.
Chapter 13: Overloading.
Chapter 15: Operator Overloading
Review of C++ Programming Part II Sheng-Fang Huang.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 14: Overloading and Templates.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 15: Overloading and Templates.
You gotta be cool. Access Functions and Utility Functions Preprocessor Wrapper Looking Ahead to Composition and Inheritance Object Size Class Scope and.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes.
C++ How to Program, 8/e © by Pearson Education, Inc. All Rights Reserved. Note: C How to Program, Chapter 22 is a copy of C++ How to Program Chapter.
 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.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 2.
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 13: Introduction to Classes.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 13 Introduction to Classes.
Learners Support Publications Classes and Objects.
Android How to Program, 2/e © Copyright by Pearson Education, Inc. All Rights Reserved.
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.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Classes: A Deeper Look Part.
Chapter 10 Introduction to Classes
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes.
Java™ How to Program, 10/e © Copyright by Pearson Education, Inc. All Rights Reserved.
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.
 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.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 15: Overloading and Templates.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 14: More About Classes.
Chapter 13: Overloading and Templates. Objectives In this chapter, you will – Learn about overloading – Become familiar with the restrictions on operator.
 2008 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
 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.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 13: Introduction to Classes.
Jozef Goetz Credits: Copyright  Pearson Education, Inc. All rights reserved. expanded by J. Goetz, 2016.
Programming Fundamentals1 Chapter 7 INTRODUCTION TO CLASSES.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 9 Introduction of Object Oriented Programming.
 2003 Prentice Hall, Inc. All rights reserved. ECE 2552 Dr. Këpuska Summer 2004 from Dr. S. Kozaitis Spring 2003 slides 1 Summary of Chapter 6: Classes.
Chapter 13: Overloading and Templates
Chapter 16: Classes and Data Abstraction
Introduction to Classes
Chapter 3 Introduction to Classes, Objects Methods and Strings
Classes: A Deeper Look Outline
Chapter 6 Methods: A Deeper Look
Chapter 9 Classes: A Deeper Look, Part 1
Classes and Objects.
Classes: A Deeper Look, Part 1
Classes: A Deeper Look, Part 1
More C++ Classes Systems Programming.
Presentation transcript:

Chapter 9 Classes: A Deeper Look, Part 1 Seventh Edition C++ How to Program © by Pearson Education, Inc. All Rights Reserved.

9.1 Introduction (cont.) Coverage includes (cont.): ◦ How default arguments can be used in a constructor. ◦ Destructors that perform “termination housekeeping” on objects before they are destroyed. ◦ The order in which constructors and destructors are called. ◦ The dangers of member functions that return references to private data. ◦ Default memberwise assignment for copying the data members in the object on the right side of an assignment into the corresponding data members of the object on the left side of the assignment. © by Pearson Education, Inc. All Rights Reserved.

9.2 Time Class Case Study © by Pearson Education, Inc. All Rights Reserved.

9.2 Time Class Case Study (cont.) ◦ Important note: you can define several overloaded constructors for a class. © by Pearson Education, Inc. All Rights Reserved.

9.2 Time Class Case Study (cont.) Parameterized stream manipulator setfill specifies the fill character that is displayed when an integer is output in a field wider than the number of digits in the value. By default, the fill characters appear to the left of the digits in the number. If the number being output fills the specified field, the fill character will not be displayed. Once the fill character is specified with setfill, it applies for all subsequent values that are displayed in fields wider than the value being displayed (setfill is a “sticky setting”). © by Pearson Education, Inc. All Rights Reserved.

9.2 Time Class Case Study (cont.) Parameterized stream manipulator setw(x) specifies that the next value output should appear in a field width of x i.e. using setw(2): cout prints the value with at least 2 character position ◦ If the value to be output is less than 2 characters positions wide, the value is right justified in the field by default. ◦ If the value to be output is more than 2 characters positions wide, the field width is extended to accommodate the entire value. setw is applied only to the next value displayed (setw is a “nonsticky” setting) © by Pearson Education, Inc. All Rights Reserved.

9.2 Time Class Case Study (cont.) Even though a member function declared in a class definition may be defined outside that class definition, that member function is still within that class’s scope. If a member function is defined in the body of a class definition, the compiler attempts to inline calls to the member function. © by Pearson Education, Inc. All Rights Reserved.

9.2 Time Class Case Study (cont.) C++ provides inline functions to help reduce function call overhead –especially for small functions. Placing the qualifier inline before a function’s return type in the function definition advises the compiler to generate a copy of the function’s code in place to avoid a function call. © by Pearson Education, Inc. All Rights Reserved.

9.2 Time Class Case Study (cont.) Once class Time has been defined, it can be used as a type in object, array, pointer and reference declarations as follows: Time sunset; // object of type Time Time arrayOfTimes[ 5 ]; // array of 5 Time objects Time &dinnerTime = sunset; // reference to a Time object Time *timePtr = &dinnerTime; // pointer to a Time object © by Pearson Education, Inc. All Rights Reserved.

9.3 Class Scope and Accessing Class Members A class’s data members and member functions belong to that class’s scope. Nonmember functions are defined at global namespace scope. Within a class’s scope, class members are immediately accessible by all of that class’s member functions and can be referenced by name. Outside a class’s scope, public class members are referenced through one of the handles on an object—an object name, a reference to an object or a pointer to an object. © by Pearson Education, Inc. All Rights Reserved.

9.3 Class Scope and Accessing Class Members (cont.) Member functions of a class can be overloaded only by other member functions of that class. Variable declared in a member function have a local scope and are known only to the function. If a member function defines a variable with the same name as a variable with class scope, the class-scope variable is hidden by the block- scope variable in the local scope. ◦ Such a hidden variable can be accessed by preceding the variable name with the class name followed by the scope resolution operator ( :: ). © by Pearson Education, Inc. All Rights Reserved.

9.3 Class Scope and Accessing Class Members (cont.) The dot member selection operator (. ) is preceded by an object’s name or with a reference to an object to access the object’s members. The arrow member selection operator ( -> ) is preceded by a pointer to an object to access the object’s members. © by Pearson Education, Inc. All Rights Reserved.

9.4 Separating Interface from Implementation Separating classes into two files—a header file for the class definition (i.e., the class’s interface) and a source code file for the class’s member-function definitions (i.e., the class’s implementation) makes it easier to modify programs. © by Pearson Education, Inc. All Rights Reserved.

9.5 Access Functions and Utility Functions Access functions can read or display data. A common use for access functions is to test the truth or falsity of conditions— such functions are often called predicate functions. A utility function is a private member function that supports the operation of the class’s public member functions. © by Pearson Education, Inc. All Rights Reserved.

9.6 Time Class Case Study: Constructors with Default Arguments Like other functions, constructors can specify default arguments. The default arguments to the constructor ensure that, even if no values are provided in a constructor call, the constructor still initializes the data members to maintain the Time object in a consistent state. A constructor that defaults all its arguments is also a default constructor—i.e., a constructor that can be invoked with no arguments. There can be at most one default constructor per class. © by Pearson Education, Inc. All Rights Reserved.

9.6 Time Class Case Study: Constructors with Default Arguments (cont.) Calling setHour, setMinute and setSecond from the constructor may be slightly more efficient because the extra call to setTime would be eliminated. Similarly, copying the code from lines 27, 33 and 39 into constructor would eliminate the overhead of calling setTime, setHour, setMinute and setSecond. This would make maintenance of this class more difficult. ◦ If the implementations of setHour, setMinute and setSecond were to change, the implementation of any member function that duplicates lines 27, 33 and 39 would have to change accordingly. Calling setTime and having setTime call setHour, setMinute and setSecond enables us to limit the changes to the corresponding set function. ◦ Reduces the likelihood of errors when altering the implementation. © by Pearson Education, Inc. All Rights Reserved.

9.6 Time Class Case Study: Constructors with Default Arguments (cont.) Time ’s set and get functions are called throughout the class’s body. In each case, these functions could have accessed the class’s private data directly. Consider changing the representation of the time from three int values (requiring 12 bytes of memory) to a single int value representing the total number of seconds that have elapsed since midnight (requiring only four bytes of memory). If we made such a change, only the bodies of the functions that access the private data directly would need to change. ◦ No need to modify the bodies of the other functions. © by Pearson Education, Inc. All Rights Reserved.

9.6 Time Class Case Study: Constructors with Default Arguments (cont.) Designing the class in this manner reduces the likelihood of programming errors when altering the class’s implementation. Duplicating statements in multiple functions or constructors makes changing the class’s internal data representation more difficult. © by Pearson Education, Inc. All Rights Reserved.