DOC PREVIEW
IUPUI CS 265 - C++ Classes: Part II

This preview shows page 1-2-3-25-26-27-28-50-51-52 out of 52 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 52 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 52 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 52 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 52 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 52 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 52 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 52 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 52 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 52 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 52 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 52 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

PowerPoint Presentation17.1 IntroductionSlide 317.2 const (Constant) Objects and const Member Functions (II)Slide 5Slide 6Slide 7Slide 8Slide 9Slide 10Slide 11Slide 1217.3 Composition: Objects as Members of Classes (II)Slide 14Slide 15Slide 16Slide 17Slide 18Slide 19Slide 20Slide 21Slide 2217.4 friend Functions and friend Classes (II)Slide 24Slide 25Slide 26Slide 27Slide 2817.5 Using the this Pointer (II)17.5 Using the this Pointer (III)Slide 31Slide 32Slide 33Slide 34Slide 35Slide 36Slide 37Slide 3817.6 Dynamic Memory Allocation with Operators new and delete (II)Slide 4017.7 static Class Members (II)Slide 42Slide 43Slide 44Slide 45Slide 4617.8 Data Abstraction and Information Hiding17.8 Data Abstraction and Information Hiding (II)Slide 4917.8.2 Example: String Abstract Data TypeSlide 51Slide 52 2000 Prentice Hall, Inc. All rights reserved.Chapter 17 - C++ Classes: Part IIOutline17.1 Introduction17.2 const (Constant) Objects and const Member Functions17.3 Composition: Objects as Members of Classes17.4 friend Functions and friend Classes17.5 Using the this Pointer17.6 Dynamic Memory Allocation with Operators new and delete17.7 static Class Members17.8 Data Abstraction and Information Hiding17.8.1 Example: Array Abstract Data Type17.8.2 Example: String Abstract Data Type17.8.3 Example: Queue Abstract Data Type17.9 Container Classes and Iterators 2000 Prentice Hall, Inc. All rights reserved.17.1 Introduction•Chapters 16-18–Object-based programming•Chapter 19-20–Polymorphism and inheritance–Object-oriented programming 2000 Prentice Hall, Inc. All rights reserved.17.2 const (Constant) Objects and const Member Functions•Principle of least privilege•Only give objects permissions they need, no more •Keyword const•Specify that an object is not modifiable•Any attempt to modify the object is a syntax error•For example: const time noon( 12, 0, 0 ); •Declares a const object noon of class time and initializes it to 12 noon 2000 Prentice Hall, Inc. All rights reserved.17.2 const (Constant) Objects and const Member Functions (II)• const objects require const functions –Functions declared const cannot modify the object –const specified in function prototype and definitionPrototype: ReturnType FunctionName(param1,param2…) const; Definition: ReturnType FunctionName(param1,param2…) const { …};Example: int A::getValue() const {return privateDataMember};-Returns the value of a data member, and is appropriately declared const -Constructors / Destructors cannot be const-They need to initialize variables (therefore modifying them) 2000 Prentice Hall, Inc. All rights reserved.Outline1 // Fig. 17.1: time5.h2 // Declaration of the class Time.3 // Member functions defined in time5.cpp4 #ifndef TIME5_H5 #define TIME5_H67 class Time {8 public:9 Time( int = 0, int = 0, int = 0 ); // default constructor1011 // set functions12 void setTime( int, int, int ); // set time13 void setHour( int ); // set hour14 void setMinute( int ); // set minute15 void setSecond( int ); // set second1617 // get functions (normally declared const)18 int getHour() const; // return hour19 int getMinute() const; // return minute20 int getSecond() const; // return second2122 // print functions (normally declared const)23 void printMilitary() const; // print military time24 void printStandard(); // print standard time25 private:26 int hour; // 0 - 2327 int minute; // 0 - 5928 int second; // 0 - 5929 };3031 #endif1. Class definition1.1 Function prototypes1.2 Member variables 2000 Prentice Hall, Inc. All rights reserved.OutlineSource Code1. Load Header1.1 Function definitions32 // Fig. 17.1: time5.cpp33 // Member function definitions for Time class.34 #include <iostream>3536 using std::cout;3738 #include "time5.h"3940 // Constructor function to initialize private data.41 // Default values are 0 (see class definition).42 Time::Time( int hr, int min, int sec ) 43 { setTime( hr, min, sec ); }4445 // Set the values of hour, minute, and second.46 void Time::setTime( int h, int m, int s )47 {48 setHour( h );49 setMinute( m );50 setSecond( s );51 }5253 // Set the hour value54 void Time::setHour( int h ) 55 { hour = ( h >= 0 && h < 24 ) ? h : 0; }5657 // Set the minute value58 void Time::setMinute( int m ) 59 { minute = ( m >= 0 && m < 60 ) ? m : 0; }6061 // Set the second value62 void Time::setSecond( int s )63 { second = ( s >= 0 && s < 60 ) ? s : 0; } 2000 Prentice Hall, Inc. All rights reserved.Outline1.1 Function definitions1.2 Purposely leave out const keyword for printStandard6465 // Get the hour value66 int Time::getHour() const { return hour; }6768 // Get the minute value69 int Time::getMinute() const { return minute; }7071 // Get the second value72 int Time::getSecond() const { return second; }7374 // Display military format time: HH:MM75 void Time::printMilitary() const76 {77 cout << ( hour < 10 ? "0" : "" ) << hour << ":"78 << ( minute < 10 ? "0" : "" ) << minute;79 }8081 // Display standard format time: HH:MM:SS AM (or PM)82 void Time::printStandard() // should be const83 {84 cout << ( ( hour == 12 ) ? 12 : hour % 12 ) << ":"85 << ( minute < 10 ? "0" : "" ) << minute << ":"86 << ( second < 10 ? "0" : "" ) << second87 << ( hour < 12 ? " AM" : " PM" );88 } 2000 Prentice Hall, Inc. All rights reserved.Outline1. Initialize variables2. Attempt to use non-const functions with const objectsProgram Output89 // Fig. 17.1: fig17_01.cpp90 // Attempting to access a const object with91 // non-const member functions.92 #include "time5.h"9394 int main()95 {96 Time wakeUp( 6, 45, 0 ); // non-constant object97 const Time noon( 12, 0, 0 ); // constant object9899 // MEMBER FUNCTION OBJECT100 wakeUp.setHour( 18 ); // non-const non-const101102 noon.setHour( 12 ); // non-const const103104 wakeUp.getHour(); // const non-const105106 noon.getMinute(); // const const107 noon.printMilitary(); // const const108 noon.printStandard(); // non-const const109 return 0;110}Compiling...Fig07_01.cppd:fig07_01.cpp(14) : error C2662: 'setHour' : cannot convert 'this' pointer from 'const class Time' to 'class Time &'Conversion loses qualifiersd:\fig07_01.cpp(20) : error C2662: 'printStandard' : cannot convert 'this' pointer from 'const class


View Full Document

IUPUI CS 265 - C++ Classes: Part II

Download C++ Classes: Part II
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view C++ Classes: Part II and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view C++ Classes: Part II 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?