Introduction to Programming session 24

Similar documents
Classes: A Deeper Look

Fundamentals of Programming Session 24

More C++ Classes. Systems Programming

IS 0020 Program Design and Software Tools

Chapter 6: Classes and Data Abstraction

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

Chapter 9 Classes : A Deeper Look, Part 1

57:017, Computers in Engineering C++ Classes

IS 0020 Program Design and Software Tools

Computer Programming with C++ (21)

Classes and Data Abstraction

Classes and Data Abstraction. Topic 5

IS 0020 Program Design and Software Tools

Chapter 16: Classes and Data Abstraction

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

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

A Deeper Look at Classes

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

OBJECT-ORIENTED PROGRAMMING CONCEPTS-CLASSES II

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

Classes: A Deeper Look. Systems Programming

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

Computer Programming Class Members 9 th Lecture

Classes and Data Abstraction. Topic 5

Object Oriented Design

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

Classes: A Deeper Look, Part 2

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

Fundamentals of Programming Session 12

C++ Classes, Constructor & Object Oriented Programming

Lecture 5. Function Pointers

Object Oriented Design

Lecture 18 Tao Wang 1

Chapter 17 - C++ Classes: Part II

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

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

OBJECT ORIENTED PROGRAMMING USING C++

OOPS Viva Questions. Object is termed as an instance of a class, and it has its own state, behavior and identity.

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

Chapter 10 Introduction to Classes


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

(5-1) Object-Oriented Programming (OOP) and C++ Instructor - Andrew S. O Fallon CptS 122 (February 4, 2019) Washington State University

Introduction to Programming

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

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

Fundamentals of Programming Session 13

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

Fundamentals of Programming Session 25

! 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

Functions and Recursion

Object-Oriented Design (OOD) and C++

Introduction to Classes

Fundamentals of Programming Session 24

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

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

C++ Programming: From Problem Analysis to Program Design, Fifth Edition. Chapter 12: Classes and Data Abstraction

AN OVERVIEW OF C++ 1

CS304 Object Oriented Programming Final Term

Fundamentals of Programming Session 23

CS313D: ADVANCED PROGRAMMING LANGUAGE

Ch02. True/False Indicate whether the statement is true or false.

EL2310 Scientific Programming

CS313D: ADVANCED PROGRAMMING LANGUAGE

Fast Introduction to Object Oriented Programming and C++

Chapter 15 - C++ As A "Better C"

CS313D: ADVANCED PROGRAMMING LANGUAGE

Short Notes of CS201

CS201 - Introduction to Programming Glossary By

CSCE 110 PROGRAMMING FUNDAMENTALS

Abstraction in Software Development

Lecture Contents CS313D: ADVANCED PROGRAMMING LANGUAGE

Introduction to Programming

Object-Based Programming (Deitel chapter 8)

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

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

Introduction Of Classes ( OOPS )

Jayaram college of Engineering and Technology, Pagalavadi. CS2203 Object Oriented Programming Question Bank Prepared By: S.Gopalakrishnan, Lecturer/IT

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

Chapter 18 - C++ Operator Overloading

Implementing an ADT with a Class

Chapter 3 - Functions

Fundamentals of Programming Session 20

Friends and Overloaded Operators

III. Classes (Chap. 3)

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

Fundamentals of Programming Session 25

Friends and Overloaded Operators

Function Overloading

Fundamentals of Programming Session 27

Classes: A Deeper Look, Part 1

Computer Programming C++ Classes and Objects 6 th Lecture

JAYARAM COLLEGE OF ENGINEERING AND TECHNOLOGY Pagalavadi, Tiruchirappalli (An approved by AICTE and Affiliated to Anna University)

Operator Overloading in C++ Systems Programming

Data Structures using OOP C++ Lecture 3

Polymorphism Part 1 1

Intermediate Programming, Spring 2017*

ADTs & Classes. An introduction

Transcription:

Introduction to Programming session 24 Instructor: Reza Entezari-Maleki Email: entezari@ce.sharif.edu 1 Fall 2010 These slides are created using Deitel sslides Sharif Universityof Technology

Outlines Introduction to Classes Implementing a Time Abstract Data Type with a class Class Scope and Accessing Class Members Separating Interface from Implementation 2

Introduction to Classes 3 Object-oriented programming (OOP) Encapsulates data (attributes) and functions (behavior) into packages called classes Information hiding Class objects communicate across well-defined interfaces Implementation details hidden within classes themselves User-defined (programmer-defined) types: classes Data (data members) Functions (member functions or methods) Similar to blueprints reusable Class instance: object

Introduction to Classes 4 Classes Model objects Attributes (data members) Behaviors (member functions) Defined using keyword class Member functions Methods Invoked in response to messages Member access specifiers public: Accessible wherever object of class in scope private: Accessible only to member functions of class protected:

Implementing a Time Abstract Data Type with a class Constructor function Special member function Initializes data members Same name as class Called when object instantiated Several constructors Function overloading No return type 5

1 classtime { 2 3 public: 4 Time(); // constructor 5 void settime( int, int, int ); // set hour, minute, second 6 void printuniversal(); // print universal-time format 7 void printstandard(); // print standard-time format 8 9 private: 10 int hour; // 0-23 (24-hour clock format) 11 int minute; // 0-59 12 int second; // 0-59 13 14 }; // end class Time 6

Implementing a Time Abstract Data Type with a class Objects of class After class definition Class name new type specifier C++ extensible language Object, array, pointer and reference declarations Example: Class name becomes new type specifier. Time sunset; Time arrayoftimes[ 5 ]; Time *pointertotime; Time &dinnertime = sunset; // object of type Time // array of Time objects // pointer to a Time // reference to a Time 7

Implementing a Time Abstract Data Type with a class 8 Member functions defined outside class Binary scope resolution operator (::) Ties member name to class name Uniquely identify functions of particular class Different classes can have member functions with same name Format for defining member functions ReturnType ClassName::MemberFunctionName( ){ } Does not change whether function public or private Member functions defined inside class Do not need scope resolution operator, class name Compiler attempts inline Outside class, inline explicitly with keyword inline

1 // Time class. 2 #include <iostream> 3 using std::cout; 4 using std::endl; 5 #include <iomanip> 6 using std::setfill; 7 using std::setw; 8 // Time abstract data type (ADT) definition 9 classtime { 10 public: 11 Time(); // constructor 12 void settime( int, int, int ); // set hour, minute, second 13 void printuniversal(); // print universal-time format 14 void printstandard(); // print standard-time format 9

10 15 private: 16 int hour; // 0-23 (24-hour clock format) 17 int minute; // 0-59 18 int second; // 0-59 19 }; // end class Time 20 // Time constructor initializes each data member to zero and 21 // ensures all Time objects start in a consistent state 22 Time::Time() 23 { 24 hour = minute = second = 0; 25 } // end Time constructor 26 // set new Time value using universal time, perform validity 27 // checks on the data values and set invalid values to zero 28 voidtime::settime( int h, int m, int s ) 29 { 30 hour = ( h >= 0 && h < 24 )? h : 0; 31 minute = ( m >= 0 && m < 60 )? m : 0; 32 second = ( s >= 0 && s < 60 )? s : 0; 33 } // end function settime

11 47 // print Time in universal format 48 void Time::printUniversal() 49 { 50 cout << setfill( '0' ) << setw( 2 ) << hour << ":" 51 << setw( 2 ) << minute << ":" 52 << setw( 2 ) << second; 54 } // end function printuniversal 56 // print Time in standard format 57 void Time::printStandard() 58 { 59 cout << ( ( hour == 0 hour == 12 )? 12 : hour % 12 ) 60 << ":" << setfill( '0' ) << setw( 2 ) << minute 61 << ":" << setw( 2 ) << second 62 << ( hour < 12? " AM" : " PM" ); 64 } // end function printstandard 66 int main() 67 { 68 Time t; // instantiate object t of class Time

12 70 // output Time object t's initial values 71 cout<< "The initial universal time is "; 72 t.printuniversal(); // 00:00:00 74 cout<< "\ntheinitial standard time is "; 75 t.printstandard(); // 12:00:00 AM 77 t.settime( 13, 27, 6 ); // change time 79 // output Time object t's new values 80 cout<< "\n\nuniversaltime after settimeis "; 81 t.printuniversal(); // 13:27:06 83 cout<< "\nstandardtime after settimeis "; 84 t.printstandard(); // 1:27:06 PM 86 t.settime( 99, 99, 99 ); // attempt invalid settings 88 // output t's values after specifying invalid values 89 cout<< "\n\nafterattempting invalid settings:" 90 << "\nuniversaltime: "; 91 t.printuniversal(); // 00:00:00

93 cout << "\nstandard time: "; 94 t.printstandard(); // 12:00:00 AM 95 cout << endl; 97 return 0; 99 } // end main The initial universal time is 00:00:00 The initial standard time is 12:00:00 AM Universal time after settime is 13:27:06 Standard time after settime is 1:27:06 PM 13 After attempting invalid settings: Universal time: 00:00:00 Standard time: 12:00:00 AM

Implementing a Time Abstract Data Type with a class Destructors Same name as class Preceded with tilde (~) No arguments Cannot be overloaded Performs termination housekeeping 14

Implementing a Time Abstract Data Type with a class Advantages of using classes Simplify programming Interfaces Hide implementation Software reuse Composition (aggregation) Class objects included as members of other classes Inheritance New classes derived from old 15

Class Scope and Accessing Class Members Class scope Data members, member functions Within class scope Class members Immediately accessible by all member functions Referenced by name Outside class scope Referenced through handles Object name, reference to object, pointer to object File scope Nonmember functions 16

Class Scope and Accessing Class Members Function scope Variables declared in member function Only known to function Variables with same name as class-scope variables Class-scope variable hidden Access with scope resolution operator (::) ClassName::classVariableName Variables only known to function they are defined in Variables are destroyed after function completion 17

Class Scope and Accessing Class Members Operators to access class members Identical to those for structs Dot member selection operator (.) Object Reference to object Arrow member selection operator (->) Pointers 18

1 // Demonstrating the class member access operators. and -> 2 // CAUTION: IN FUTURE EXAMPLES WE AVOID PUBLIC DATA! 3 #include <iostream> 4 using std::cout; 5 using std::endl; 6 // class Count definition 7 class Count { 8 public: 9 int x; 10 void print() 11 { 12 cout<< x << endl; 13 } 14 }; // end class Count 19

15 int main() 16 { 17 Count counter; // create counter object 18 Count *counterptr= &counter; // create pointer to counter 19 Count &counterref = counter; // create reference to counter 20 cout << "Assign 1 to x and print using the object's name: "; 21 counter.x= 1; // assign 1 to data member x 22 counter.print(); // call member function print 23 cout << "Assign 2 to x and print using a reference: "; 24 counterref.x = 2; // assign 2 to data member x 25 counterref.print(); // call member function print 26 cout << "Assign 3 to x and print using a pointer: "; 27 counterptr->x = 3; // assign 3 to data member x 28 counterptr->print(); // call member function print 29 return 0; 30 } // end main 20 Assign 1 to x and print using the object's name: 1 Assign 2 to x and print using a reference: 2 Assign 3 to x and print using a pointer: 3

Separating Interface from Implementation Separating interface from implementation Advantage Easier to modify programs Disadvantage Header files Portions of implementation o Inline member functions Hints about other implementation o private members Can hide more with proxy class 21

Separating Interface from Implementation Header files Class definitions and function prototypes Included in each file using class #include File extension.h Source-code files Member function definitions Same base name Convention Compiled and linked 22

23 1 // Declaration of class Time. 2 // Member functions are defined in time1.cpp 3 // prevent multiple inclusions of header file 4 #ifndef TIME1_H 5 #define TIME1_H 6 // Time abstract data type definition 7 classtime { 8 public: 9 Time(); // constructor 10 void settime( int, int, int ); // set hour, minute, second 11 void printuniversal(); // print universal-time format 12 void printstandard(); // print standard-time format 13 private: 14 int hour; // 0-23 (24-hour clock format) 15 int minute; // 0-59 16 int second; // 0-59 17 }; // end class Time 18 #endif

1 // Member-function definitions for class Time. 2 #include <iostream> 3 using std::cout; 4 #include <iomanip> 5 using std::setfill; 6 using std::setw; 7 // include definition of class Time from time1.h 8 #include "time1.h" 9 // Time constructor initializes each data member to zero. 10 // Ensures all Time objects start in a consistent state. 11 Time::Time() 12 { 13 hour = minute = second = 0; 14 } // end Time constructor 24

25 15 // Set new Time value using universal time. Perform validity 16 // checks on the data values. Set invalid values to zero. 17 voidtime::settime( int h, int m, int s ) 18 { 19 hour = ( h >= 0 && h < 24 )? h : 0; 20 minute = ( m >= 0 && m < 60 )? m : 0; 21 second = ( s >= 0 && s < 60 )? s : 0; 22 } // end function settime 23 // print Time in universal format 24 void Time::printUniversal() 25 { 26 cout<< setfill( '0' ) << setw( 2 ) << hour << ":" 27 << setw( 2 ) << minute << ":" 28 << setw( 2 ) << second; 29 } // end function printuniversal

26 30 // Set new Time value using universal time. Perform validity 31 // checks on the data values. Set invalid values to zero. 32 voidtime::settime( int h, int m, int s ) 33 { 34 hour = ( h >= 0 && h < 24 )? h : 0; 35 minute = ( m >= 0 && m < 60 )? m : 0; 36 second = ( s >= 0 && s < 60 )? s : 0; 37 } // end function settime 38 // print Time in universal format 39 void Time::printUniversal() 40 { 41 cout<< setfill( '0' ) << setw( 2 ) << hour << ":" 42 << setw( 2 ) << minute << ":" 43 << setw( 2 ) << second; 44 } // end function printuniversal

45 // print Time in standard format 46 void Time::printStandard() 47 { 48 cout<< ( ( hour == 0 hour == 12 )? 12 : hour % 12 ) 49 << ":" << setfill( '0' ) << setw( 2 ) << minute 50 << ":" << setw( 2 ) << second 51 << ( hour < 12? " AM" : " PM" ); 52 53 } // end function printstandard 27

1 // Program to test class Time. 2 // NOTE: This file must be compiled with time1.cpp. 3 #include <iostream> 4 using std::cout; 5 using std::endl; 6 // include definition of class Time from time1.h 7 #include "time1.h" 8 int main() 9 { 10 Time t; // instantiate object t of class Time 11 // output Time object t's initial values 12 cout<< "The initial universal time is "; 13 t.printuniversal(); // 00:00:00 14 cout<< "\ntheinitial standard time is "; 15 t.printstandard(); // 12:00:00 AM 16 t.settime( 13, 27, 6 ); // change time 28

17 // output Time object t'snew values 18 cout<< "\n\nuniversaltime after settimeis "; 19 t.printuniversal(); // 13:27:06 20 cout<< "\nstandardtime after settimeis "; 21 t.printstandard(); // 1:27:06 PM 22 t.settime( 99, 99, 99 ); // attempt invalid settings 23 // output t's values after specifying invalid values 24 cout<< "\n\nafterattempting invalid settings:" 25 << "\nuniversal time: "; 26 t.printuniversal(); // 00:00:00 27 cout<< "\nstandardtime: "; 28 t.printstandard(); // 12:00:00 AM 29 cout<< endl; 30 return 0; 31 } // end main The initial universal time is 00:00:00 The initial standard time is 12:00:00 AM Universal time after settime is 13:27:06 Standard time after settime is 1:27:06 PM 29