Chapter 9 Classes : A Deeper Look, Part 1

Similar documents
Object Oriented Design

More C++ Classes. Systems Programming

Introduction to Programming session 24

Classes: A Deeper Look

Computer Programming with C++ (21)

OBJECT-ORIENTED PROGRAMMING CONCEPTS-CLASSES II

A Deeper Look at Classes

IS 0020 Program Design and Software Tools

Pointer Assignments. CISC181 Introduction to Computer Science. Dr. McCoy. Lecture 18 October 29, The new Operator

1 // Fig. 6.13: time2.cpp 2 // Member-function definitions for class Time. 3 #include <iostream> Outline. 4 5 using std::cout; 6 7 #include <iomanip>

9.1 Introduction. Integrated Time class case study Preprocessor wrapper Three types of handles on an object. Class functions

57:017, Computers in Engineering C++ Classes

Fundamentals of Programming Session 24

Chapter 6: Classes and Data Abstraction

Control Structures of C++ Programming (2)

! Data is stored in variables. - Perhaps using arrays and structs. ! Program is a collection of functions that perform

l A class in C++ is similar to a structure. - It allows you to define a new (composite) data type. l A class contains the following: - variables AND

! Data is stored in variables. - Perhaps using arrays and structs. ! Program is a collection of functions that perform

Deitel Dive-Into Series: Dive-Into Cygwin and GNU C++

! Data is stored in variables. - Perhaps using arrays and structs. ! Program is a collection of functions that perform

! Data is stored in variables. - Perhaps using arrays and structs. ! Program is a collection of functions that perform

C How to Program, 6/e by Pearson Education, Inc. All Rights Reserved. 1

IS 0020 Program Design and Software Tools

Lecture 5. Function Pointers

! Data is stored in variables. - Perhaps using arrays and structs. ! Program is a collection of functions that perform

Computer Programming Class Members 9 th Lecture

clarity. In the first form, the access specifier public : is needed to {

Classes: A Deeper Look. Systems Programming

IS 0020 Program Design and Software Tools

Preview 9/20/2017. Object Oriented Programing with C++ Object Oriented Programing with C++ Object Oriented Programing with C++

Classes and Data Abstraction. Topic 5

Classes and Data Abstraction

W8.1 Continuing Classes friend Functions and friend Classes Using the this Pointer Cascading Function Calls

C++, How to Program. Spring 2016 CISC1600 Yanjun Li 1

Chapter 16: Classes and Data Abstraction

Chapter 7 Array. Array. C++, How to Program

l A class in C++ is similar to a structure. - It allows you to define a new (composite) data type. l A class contains the following: - variables AND

C++ Classes, Constructor & Object Oriented Programming

Classes: A Deeper Look, Part 2

The Class. Classes and Objects. Example class: Time class declaration with functions defined inline. Using Time class in a driver.

! A class in C++ is similar to a structure. ! A class contains members: - variables AND. - public: accessible outside the class.

CS 1337 Computer Science II Page 1

l A class in C++ is similar to a structure. l A class contains members: - variables AND - public: accessible outside the class.

this Pointer, Constant Functions, Static Data Members, and Static Member Functions this Pointer (11.1) Example of this pointer

! A class in C++ is similar to a structure. ! A class contains members: - variables AND. - public: accessible outside the class.

More About Classes. Gaddis Ch. 14, CS 2308 :: Fall 2015 Molly O'Neil

2. It is possible for a structure variable to be a member of another structure variable.

Classes and Objects. Types and Classes. Example. Object Oriented Programming

III. Classes (Chap. 3)

Function Overloading

Lecture 18 Tao Wang 1

Classes and Data Abstraction. Topic 5

Chapter 10 Introduction to Classes

Come and join us at WebLyceum

CSCI 123 Introduction to Programming Concepts in C++

Implementing an ADT with a Class

Object Oriented Design

3.Constructors and Destructors. Develop cpp program to implement constructor and destructor.

Introduction to Classes

Introduction Of Classes ( OOPS )

Come and join us at WebLyceum

CSCE 110 PROGRAMMING FUNDAMENTALS

Operator Overloading in C++ Systems Programming

Chapter 13: Introduction to Classes Procedural and Object-Oriented Programming

C How to Program, 6/e, 7/e

Classes and Pointers: Some Peculiarities

(5 2) Introduction to Classes in C++ Instructor - Andrew S. O Fallon CptS 122 (February 7, 2018) Washington State University

IS0020 Program Design and Software Tools Midterm, Fall, 2004

Lab 2: ADT Design & Implementation

COMP322 - Introduction to C++

Angela Chih-Wei Tang Visual Communications Lab Department of Communication Engineering National Central University JhongLi, Taiwan.

Constructor - example

Chapter 18 - C++ Operator Overloading

Engineering Problem Solving with C++, 3e Chapter 2 Test Bank

Preview 11/1/2017. Constant Objects and Member Functions. Constant Objects and Member Functions. Constant Objects and Member Functions

I/O Streams and Standard I/O Devices (cont d.)

Classes and Objects. Class scope: - private members are only accessible by the class methods.

INHERITANCE: CONSTRUCTORS,

G205 Fundamentals of Computer Engineering. CLASS 3, Wed. Sept Stefano Basagni Fall 2004 M-W, 1:30pm-3:10pm

Cpt S 122 Data Structures. Introduction to C++ Part II

Slide Set 14. for ENCM 339 Fall Steve Norman, PhD, PEng. Electrical & Computer Engineering Schulich School of Engineering University of Calgary

Scope. Scope is such an important thing that we ll review what we know about scope now:

Outline. Introduction. Arrays declarations and initialization. Const variables. Character arrays. Static arrays. Examples.

OBJECT-ORIENTED PROGRAMMING CONCEPTS-CLASSES IV

Fig. 7.1 Fig. 7.2 Fig. 7.3 Fig. 7.4 Fig. 7.5 Fig. 7.6 Fig. 7.7 Fig. 7.8 Fig. 7.9 Fig. 7.10

University of Maryland Baltimore County. CMSC 202 Computer Science II. Fall Mid-Term Exam. Sections

Lecture 7. Log into Linux New documents posted to course webpage

Class and Function Templates

CS313D: ADVANCED PROGRAMMING LANGUAGE

Motivation for Templates. Class and Function Templates. One Way to Look at Templates...

SFU CMPT Topic: Classes

Short Notes of CS201

Page. No. 1/15 CS201 Introduction to Programmming Solved Subjective Questions From spring 2010 Final Term Papers By vuzs Team

1. Match each of the following data types with literal constants of that data type. A data type can be used more than once. A.

CSC1322 Object-Oriented Programming Concepts

Abstraction in Software Development

CS201 - Introduction to Programming Glossary By

KOM3191 Object Oriented Programming Dr Muharrem Mercimek OPERATOR OVERLOADING. KOM3191 Object-Oriented Programming

CE221 Programming in C++ Part 1 Introduction

Operator overloading

Transcription:

Chapter 9 Classes : A Deeper Look, Part 1 C++, How to Program Deitel & Deitel Fall 2016 CISC1600 Yanjun Li 1 Time Class Case Study Time Class Definition: private data members: int hour; int minute; int second; public member functions: Time( ); //default constructor Time(int, int, int); //constructor setting h., m., s. void settime(int, int, int); set h., m., s. void printuniversal();//print time in universal format void printstandard(); //print time in standard format Fall 2016 CISC1600 Yanjun Li 2 1

Preprocessor Wrappers Prevents code from being included more than once #ifndef if not defined Skip this code if it has been included already #define Define a unique Name so this code will not be included again #endif If the header has been included previously Name is defined already and the header file is not included again Prevents multiple-definition errors Fall 2016 CISC1600 Yanjun Li 3 1 // Fig. 9.1: Time.h 2 // Declaration of class Time. 3 // Member functions are defined in Time.cpp 4 5 // prevent multiple inclusions of header file 6 #ifndef TIME_H 7 #define TIME_H 8 9 // Time class definition 10 class Time 11 { 12 public: 13 Time(); // constructor 14 void settime( int, int, int ); // set hour, minute and second 15 void printuniversal(); // print time in universal-time format 16 void printstandard(); // print time in standard-time format 17 private: 18 int hour; // 0-23 (24-hour clock format) 19 int minute; // 0-59 20 int second; // 0-59 21 }; // end class Time 22 23 #endif Preprocessor directive #ifndef determines whether a name is defined Preprocessor directive #define defines a name (e.g., TIME_H) Preprocessor directive #endif marks the end of the code that should not be included multiple times Fall 2016 CISC1600 Yanjun Li 4 2

Time Class Constructors Default constructor Time (); Every data member is initialized to zero. Overloaded constructor Time(int, int, int); The data members of a class could not be initialized where they are declared in the class body. One constructor is called when the Time object is created. Time t1 ; //default constructor is called Time t2 (1, 34, 20); //second constructor is called. Fall 2016 CISC1600 Yanjun Li 5 Time Class Member Functions Set Functions void sethour(int); void setminute(int); void setsecond(int); void Time::setHour(int h)//[0,24) { if (h>=0 && h<24) hour = h; else hour = 0; } Fall 2016 CISC1600 Yanjun Li 6 3

Formatting numeric output (1) Stream manipulator setw(width) Requires header file <iomanip> Sets field width and applies only to the next output value Example: cout<< setw(12) << numberone <<endl; If numberone is less than 12 character positions wide, the output is Right justified by default and padded with fill characters. If numberone is more than 12 character positions wide, the field width is extended to accommodate the entire value. Fall 2016 CISC1600 Yanjun Li 7 Formatting numeric output (2) Stream manipulator left to left-justify, <iostream> Stream manipulator right to right-justify, <iostream> Stream manipulator setfill(c), <iomanip> Example: cout << setfill( x ) << left << setw(12) << numberone << endl; Fall 2016 CISC1600 Yanjun Li 8 4

Time Class Member Functions void printuniversal(); void Time::printUniversal() { cout<<setfill('0') << setw(2) << hour<<":" << setw(2) << minute <<":" << setw(2) << second; } Fall 2016 CISC1600 Yanjun Li 9 Time Class Member Functions void printstandard(); void Time::printStandard() { int sh = converthour(); cout << setfill('0') << setw(2) << sh <<":" << setw(2) << minute << ":" << setw(2) << second; } if (hour < 12) cout<<" AM"; else cout<<" PM"; Fall 2016 CISC1600 Yanjun Li 10 5

Class Scope and Accessing Class Members Class scope contains Data members Variables declared in the class definition Member functions Functions declared in the class definition Within a class s scope Class members are accessible by all member functions Fall 2016 CISC1600 Yanjun Li 11 Class Scope and Accessing Class Members (Cont.) Outside a class s scope public class members are referenced through a handle An object name A pointer to an object private class members could not be accessed by anybody. Fall 2016 CISC1600 Yanjun Li 12 6

Using Class Time Once class Time has been defined, it can be used in declarations Time sunset(18,5,0); //object of type Time Time arrayoftimes[ 5 ]; //array of 5 Time objects Fall 2016 CISC1600 Yanjun Li 13 Use Class Member Functions Member function is within the class s scope Known only to other members of the class unless referred to via Dot member selection operator (.) Object of the class sunset.printuniversal( ); Fall 2016 CISC1600 Yanjun Li 14 7

Access Functions Access functions Can read or display data void printstandard( ); void printuniversal( ); Can test the truth or falsity of conditions Such functions are often called predicate functions For example, isam( ) function for Time Class. Fall 2016 CISC1600 Yanjun Li 15 Utility Functions Utility functions (also called helper functions) private member functions that support the operation of the class s public member functions Not part of a class s public interface Not intended to be used by clients of a class Example: int converthour(); Fall 2016 CISC1600 Yanjun Li 16 8

Software Engineering Observation Avoid repeating code: If a member function of a class already provides all or part of the functionality required by a constructor (or other member function) of the class, call that member function from the constructor (or other member function). This simplifies the maintenance of the code and reduces the likelihood of an error if the implementation of the code is modified. Fall 2016 CISC1600 Yanjun Li 17 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 Fall 2016 CISC1600 Yanjun Li 18 9

Destructors A special member function 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 The destructor itself 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 Fall 2016 CISC1600 Yanjun Li 19 Destructors (Cont.) Receives no parameters and returns no value May not specify a return type not even void A class may have only one destructor Destructor overloading is not allowed If the programmer does not explicitly provide a destructor, the compiler creates an empty destructor Fall 2016 CISC1600 Yanjun Li 20 10

When Constructors and Destructors Are Called Constructors and destructors Called implicitly by the compiler Order of these function calls depends on the order in which execution enters and leaves the scopes where the objects are instantiated Fall 2016 CISC1600 Yanjun Li 21 Reference Reproduced from the Cyber Classroom for C++, How to Program, 5/e by Deitel & Deitel. Reproduced by permission of Pearson Education, Inc. Fall 2016 CISC1600 Yanjun Li 22 11