Class 3 (L33) u Client of a Class u Purpose of Public Members u Private Class Members u Controlling Access u Access Functions u Predicate Functions u Utility.

Slides:



Advertisements
Similar presentations
Chapter 19 - Inheritance Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
Advertisements

Esempi Ereditarietà1 // Definition of class Point #ifndef POINT_H #define POINT_H #include using std::ostream; class Point { friend ostream &operator
Approfondimento Classi - Esempi1 // Argomento: Oggetti membri di altre classi // Declaration of the Date class. // Member functions defined in date1.cpp.
CPSC 231 C++ Review1 Learning Objectives §Review of the object oriented design goals. §Review of C++ classes l data members l member functions (methods)
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Arrays Outline 4.1Introduction 4.2Arrays 4.3Declaring Arrays 4.4Examples Using Arrays 4.5Passing.
1 Lab Session-XII CSIT121 Fall 2000 b Namespaces b Will This Program Compile ? b Master of Deceit b Lab Exercise 12-A b First Taste of Classes b Lab Exercise.
1 Lab Session-XIV CSIT121 Spring 2002 b Namespaces b First Class Travel b Lab Exercise 14 (Demo) b Lab Exercise b Practice Problem.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 6: Classes and Data Abstraction Outline 6.1 Introduction 6.2 Structure Definitions 6.3 Accessing.
© 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.
Introduction to Classes and Data Abstraction
1 Review (Week2) - Control Structures C++ has only seven control structures: –Sequence structure Programs executed sequentially by default –Selection structures.
 2003 Prentice Hall, Inc. All rights reserved Introduction Object-oriented programming (OOP) –Encapsulates data (attributes) and functions (behavior)
Classi - Esempi1 // SalesPerson class definition // Member functions defined in salesp.cpp #ifndef SALESP_H #define SALESP_H class SalesPerson { public:
1 Classes and Data Abstraction Andrew Davison Noppadon Kamolvilassatian Department of Computer Engineering Prince of Songkla University.
You gotta be cool. Access Functions and Utility Functions Preprocessor Wrapper Looking Ahead to Composition and Inheritance Object Size Class Scope and.
More C++ Classes Systems Programming. C++ Classes  Preprocessor Wrapper  Time Class Case Study –Two versions (old and new)  Class Scope and Assessing.
1 Chapter 4 CLASSES AND OBJECTS. 2 Outlines Procedural vs. Object ‑ Oriented Programming C++ Structures versus C Structures Classes –Accessing Class Members.
Loop.  While Loop  Do-while Loop  For Loop Continue Statement Conclusion Loop Loop.
Programming Fundamentals1 Chapter 7 INTRODUCTION TO CLASSES.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 20 - C++ Virtual Functions and Polymorphism.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 19 – Inheritance Part 2 Outline 19.8Direct Base Classes and Indirect Base Classes 19.9Using Constructors.
1 Chapter 4 - Arrays Outline 4.1Introduction 4.2Arrays 4.3Declaring Arrays 4.4Examples Using Arrays 4.5Passing Arrays to Functions 4.6Sorting Arrays 4.7Case.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 4 - Arrays Outline 4.1Introduction 4.2Arrays 4.3Declaring Arrays 4.4Examples Using Arrays.
1 Classes and Data Abstraction Part I Introduction Object-oriented programming (OOP)  Encapsulates data (attributes) and functions (behavior)
1 Classes and Data Abstraction 2 Objectives Understand encapsulation and data hiding Understand data abstraction and Abstract Data Types (ADTs) Create.
1 C++ Classes: Access (II) Ying Wu Electrical Engineering & Computer Science Northwestern University ECE230 Lectures Series.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 6: Classes and Data Abstraction Outline 6.1Introduction 6.2Structure Definitions 6.3Accessing.
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.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 23 November 19, 2009.
Structures (L30) u Syntax of a struct Declaration u Structure Variables u Accessing Members of Structures u Initialize Structure Variables u Array of Structures.
CIS 270—Application Development II Chapter 8—Classes and Objects: A Deeper Look.
 2003 Prentice Hall, Inc. All rights reserved. 1 IS 0020 Program Design and Software Tools Introduction to C++ Programming Lecture 3: Classes May 24,
Class 4 (L34) u Constructors u Default Constructor u Example of Default Constructors u Destructors u Constructors Are Called in Global Scope u Constructors.
Classes and Data Abstraction Andrew Davison Noppadon Kamolvilassatian Department of Computer Engineering Prince of Songkla University 1
 2003 Prentice Hall, Inc. All rights reserved. 1 IS 0020 Program Design and Software Tools Introduction to C++ Programming Lecture 4: Classes Jan 27,
1 Lecture 6 Classes and Data Abstraction: Part II.
1 CMSC 202 ADTs and C++ Classes. 2 Announcements Project 1 due Sunday February 25 th at midnight – don’t be late! Notes and clarifications for Project.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 20 November 10, 2009.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 19 – Inheritance – Part 1 Outline 19.1Introduction 19.2Inheritance: Base Classes and Derived Classes.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 7: Classes Part II Outline 7.1 Introduction 7.2 const (Constant) Objects and const Member Functions.
Selection Control Structures 2 (L09) * Nested if Statements * The if-else Chain * Exercise: if, if-else, nested if, and if-else chain Selection Control.
C++ Programming Lecture 16 Arrays – Part III The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Arrays Outline 4.1Introduction 4.2Arrays 4.3Declaring Arrays 4.4Examples Using Arrays 4.5Passing.
 2000 Prentice Hall, Inc. All rights reserved. NHTI CP 107 Fall 2001 M. Saleem Yusuf Introduction Object-oriented programming (OOP) –Objects Encapsulates.
Programming Fundamentals1 Chapter 7 INTRODUCTION TO CLASSES.
 2000 Prentice Hall, Inc. All rights reserved Arrays Array –Consecutive group of memory locations –Same name and type To refer to an element, specify.
1 Chapter 9 Introduction to Classes and Objects Program Development and Design Using C++, Third Edition.
 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.
1 Example Original Array Array After 2 nd Pass Array After 1 st Pass Array After 3 rd Pass.
Object Access m1.write(44); m2.write(m2.read() +1); std::cout
Chapter 16: Classes and Data Abstraction
Chapter 16: Classes and Data Abstraction
Topic 7 Introduction to Classes and Objects
More on Classes Programming in C++ Fall 2008
Classes: A Deeper Look Outline
Lecture 5: Classes September 14, 2004
Chapter 6: Classes and Data Abstraction
Classes and Data Abstraction
C++ Programming Lecture 16 Arrays – Part III
Chapter 6: Classes and Data Abstraction
Chapter 20 - C++ Virtual Functions and Polymorphism
Object-oriented programming (OOP)
Chapter 6: Classes and Data Abstraction
CISC181 Introduction to Computer Science Dr
Capitolo 4 - Arrays Outline 4.1 Introduction 4.2 Arrays
Chapter 6: Classes and Data Abstraction
Chapter 6: Classes and Data Abstraction
More C++ Classes Systems Programming.
Presentation transcript:

Class 3 (L33) u Client of a Class u Purpose of Public Members u Private Class Members u Controlling Access u Access Functions u Predicate Functions u Utility Function (Helper Function) Dr. Ming Zhang

Client of a Class A client of a class may be u a member function of another class u or it may be a global function. Dr. Ming Zhang

Purpose of Public Members u The public members of a class may be accessed by any function in the program. u The primary purpose of public members is to present to the class’s clients a view of the services (behaviors) the class provides. u The set of services forms the public interface of the class. Dr. Ming Zhang

Private Class Members u The default access mode for the class is private. u A class’s private members can be accessed only by member functions (and friends) of that class. u Private class members are only accessible through the public class interface using public member functions. Dr. Ming Zhang

Controlling Access - Fig.6.6 (1) #ifndef TIME1_H #define TIME1_H // Time abstract data type definition class Time { public: Time(); // constructor void setTime( int, int, int ); // set hour, minute, second void printMilitary(); // print military time format void printStandard(); // print standard time format private: int hour; // int minute; // int second; // }; #endif Dr. Ming Zhang

Controlling Access - Fig.6.6 (2) #include using std::cout; #include "time1.h" int main() { Time t; // Error: 'Time::hour' is not accessible t.hour = 7; // Error: 'Time::minute' is not accessible cout << "minute = " << t.minute; return 0; } Dr. Ming Zhang

Access Functions (1) u Just because class data is private does not necessarily mean that clients can not effect changes to that data. u The private data can be changed by member functions or friends of that class. u Access to a class’s private data should be carefully controlled by the use of member functions, called access functions (also called accessor methods). Dr. Ming Zhang

Access Functions (2) u To allow clients to read the value of private data, the class can provide a get function. u To enable clients to modify private data, the class can provide a set function. u Such modification would seem to violate the notion of private data. But a set member function can provide data validation capabilities (such as range checking) to ensure that the value is set properly. Dr. Ming Zhang

Predicate Functions u Access functions can read or display data. Another common use for access functions is to test the truth or falsity of conditions - such functions are often called predicate functions. u isEmpty: a program would test isEmpty before attempting to read another item from the container object. u isFull predicate function might test a container class object to determine if it has no addition room. Dr. Ming Zhang

Utility Function (Helper Function) u A utility function is not part of a class’s interface; rather, it is a private member function that supports the operation of the class’s publication member functions. u Utility functions are not intended to be used by clients of a class. Dr. Ming Zhang

Access Functions -Fig. 6.7 (1) #ifndef SALESP_H #define SALESP_H class SalesPerson { public: SalesPerson(); // constructor void getSalesFromUser(); //get sales figures from keyboard void setSales( int, double );// User supplies sales figures void printAnnualSales(); private: double totalAnnualSales(); // utility function double sales[ 12 ]; // 12 monthly sales figures }; #endif Dr. Ming Zhang

Access Functions -Fig. 6.7 (2) #include using std::cout; using std::cin; using std::endl; #include using std::setprecision; using std::setiosflags; using std::ios; #include "salesp.h" // Constructor function initializes array SalesPerson::SalesPerson() { for ( int i = 0; i < 12; i++ ) sales[ i ] = 0.0; } …... Dr. Ming Zhang

Access Functions -Fig. 6.7 (3) …... // Function to get 12 sales figures from the user // at the keyboard void SalesPerson::getSalesFromUser() { double salesFigure; for ( int i = 1; i <= 12; i++ ) { cout << "Enter sales amount for month " << i << ": "; cin >> salesFigure; setSales( i, salesFigure ); } …… Dr. Ming Zhang

Access Functions -Fig. 6.7 (4) …... // Function to set one of the 12 monthly sales figures. // Note that the month value must be from 0 to 11. void SalesPerson::setSales( int month, double amount ) { if ( month >= 1 && month 0 ) sales[ month - 1 ] = amount; // adjust for subscripts 0-11 else cout << "Invalid month or sales figure" << endl; } ……… Dr. Ming Zhang

Access Functions -Fig. 6.7 (5) …... // Print the total annual sales void SalesPerson::printAnnualSales() { cout << setprecision( 2 ) << setiosflags( ios::fixed | ios::showpoint ) << "\nThe total annual sales are: $" << totalAnnualSales() << endl; } ………. Dr. Ming Zhang

Access Functions -Fig. 6.7 (6) …... // Private utility function to total annual sales double SalesPerson::totalAnnualSales() { double total = 0.0; for ( int i = 0; i < 12; i++ ) total += sales[ i ]; return total; } Dr. Ming Zhang

Access Functions -Fig. 6.7 (7) #include "salesp.h" int main() { SalesPerson s; // create SalesPerson object s s.getSalesFromUser(); // note simple sequential code s.printAnnualSales(); // no control structures in main return 0; } Dr. Ming Zhang

Question 1 - Exercise/Home Work (1) Modify the Time class to include a tick member function that increments the time stored in a Time object by one second. The Time object should always remain in a consistent state. Write a driver program that tests the tick member function in a loop that prints the time in standard format during each iteration of the loop to illustrate that the tick member function works correctly. Be sure to test the following cases: a) Incrementing into the next minute b) Incrementing into the next hour. c) Incrementing into the next day (i.e., 11:59:59 PM to 12:00:00 AM). Dr. Ming Zhang

Question 1 - Exercise/Home Work (2) class Time { public: Time( ); void setTime( int, int, int ); void printMilitary( ) void printStandard( ); private: int hour; int minute; int second; }; Dr. Ming Zhang