Classes and Data Abstraction. Topic 5

Similar documents
Classes: A Deeper Look

Introduction to Programming session 24

More C++ Classes. Systems Programming

IS 0020 Program Design and Software Tools

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

Computer Programming with C++ (21)

Chapter 6: 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>

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

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

Fundamentals of Programming Session 24

A Deeper Look at Classes

IS 0020 Program Design and Software Tools

IS 0020 Program Design and Software Tools

Classes and Data Abstraction. Topic 5

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

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

Dot and Scope Resolution Operator

Classes: A Deeper Look. Systems Programming

57:017, Computers in Engineering C++ Classes

Chapter 9 Classes : A Deeper Look, Part 1

OBJECT-ORIENTED PROGRAMMING CONCEPTS-CLASSES II

Chapter 6 Structures and Classes. GEDB030 Computer Programming for Engineers Fall 2017 Euiseong Seo

Chapter 10 Defining Classes

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

Chapter 16: Classes and Data Abstraction

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

Ch 6. Structures and Classes

Classes and Data Abstraction

Classes: A Deeper Look, Part 2

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

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

Friends and Overloaded Operators

Friends and Overloaded Operators

Object Oriented Design

OBJECT ORIENTED PROGRAMMING USING C++

Implementing an ADT with a Class

Chapter 17 - C++ Classes: Part II

Starting Savitch Chapter 10. A class is a data type whose variables are objects. Some pre-defined classes in C++ include int,

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

Computer Programming C++ Classes and Objects 6 th Lecture

Kingdom of Saudi Arabia Princes Nora bint Abdul Rahman University College of Computer Since and Information System CS242 ARRAYS

Object-Based Programming (Deitel chapter 8)

How to engineer a class to separate its interface from its implementation and encourage reuse.

Pointers and Strings Prentice Hall, Inc. All rights reserved.

Abstract Data Types (ADTs) 1. Legal Values. Client Code for Rational ADT. ADT Design. CS 247: Software Engineering Principles

An inline function is one in which the function code replaces the function call directly. Inline class member functions

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

CS 247: Software Engineering Principles. ADT Design

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

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

Classes. Logical method to organise data and functions in a same structure. Also known as abstract data type (ADT).

C++ As A "Better C" Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan.

Chapter 18 - C++ Operator Overloading

Chapter 9 Objects and Classes. Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved.

Arrays. Week 4. Assylbek Jumagaliyev

PIC 10A. Lecture 15: User Defined Classes

Pointers and Strings. Adhi Harmoko S, M.Komp

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

The American University in Cairo Department of Computer Science & Engineering CSCI &09 Dr. KHALIL Exam-I Fall 2011

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

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

Fundamentals of Programming. Lecture 19 Hamed Rasifard

CSC 330 Object Oriented Programming. Operator Overloading Friend Functions & Forms

Implementing Abstract Data Types (ADT) using Classes

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

Structures and Classes CS 16: Solving Problems with Computers I Lecture #15

Class 15. Object-Oriented Development from Structs to Classes. Laura Marik Spring 2012 C++ Course Notes (Provided by Jason Minski)

! 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


CMSC 202 Midterm Exam 1 Fall 2015

Classes - 2. Data Processing Course, I. Hrivnacova, IPN Orsay

Operator Overloading in C++ Systems Programming

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

EL2310 Scientific Programming

Arrays. Outline. Multidimensional Arrays Case Study: Computing Mean, Median and Mode Using Arrays Prentice Hall, Inc. All rights reserved.

Lab Instructor : Jean Lai

Software Design and Analysis for Engineers

Preview 8/28/2018. Review for COSC 120 (File Processing: Reading Data From a File)

Functions and Recursion

Functions, Arrays & Structs

Classes: A Deeper Look, Part 1

Functions, Arrays & Structs

Object Oriented Design

Functions and Recursion

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

III. Classes (Chap. 3)

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

Intermediate Programming, Spring 2017*

Introduction to Classes

Fast Introduction to Object Oriented Programming and C++

EL2310 Scientific Programming

Functions. Function Prototypes. Function prototype is needed if the function call comes before the function definition in the program.

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

CS 1337 Computer Science II Page 1

System Design and Programming II

Operator Overloading

OBJECT-ORIENTED PROGRAMMING CONCEPTS-CLASSES III

Transcription:

Classes and Data Abstraction Topic 5

Classes Class User Defined Data Type Object Variable Data Value Operations Member Functions Object Variable Data Value Operations Member Functions Object Variable Data Value Operations Member Functions Encapsulation: combining a number of items such as variables and functions into a single package (object).

Classes Syntax: class Class_Name public: Member_Specification_1 Member_Specification_2. Member_Specification_n private: Member_Specification_n+1 Member_Specification_n+2. }; public members private members

Classes Example: class Bicycle public: private: }; char get_color(); int number_of_speeds(); void set (int the_speeds, char the_color); int speeds; char color; Bicycle my_bike, your_bike public members private members

Classes Member Function Syntax: Return_Type Class_Name :: Function_Name (Parameter_List) }; Function_Body_Statements Example: void } DayOfYear::output() Class DayOfYear public: void output(); int month; int day; }; cout<< month= <<month<< day= <<day<<endl;

Classes How do I call the member function output? DayOfYear today; today.month = 2; today.day = 10; today.output(); Class DayOfYear public: void output(); int month; int day; };

Classes Dot Operator (.) Scope Resolution Operator (::) Used with Objects class variables Example: new_student.output(); Used with Class name Example: void Student::output()

Classes & ADTs Public Vs Private Separate the rules for using the class and the details of the class implementation Have enough member functions that you never need to access member variables directly, only through member functions Code is easier to understand & update

Classes & ADTs Can we overload member functions? void set(int the_id, char the_major[2]); void set(int the_id); void set(double score); Student new_student; new_student.set (16.0); new_student.set (555); New_student.set ((999, CS ); Search for matching data types and/or number of parameters

Classes & ADTs Constructors: member functions automatically called when an object is declared Example: Student (int the_id, char the_major); Student (); Default constructor When default constructor is called in main: Student new_student; Not: Student new_student(); Student new_student(999, C )

Classes & ADTs #include <iostream> using namespace std; class Student public: Student (int the_id, char the_major); Student (int the_id); Student (); int get_id(); char get_major(); void output(); private: int id; char major; };

Classes & ADTs int main() Student new_student(55,'b'); new_student.output(); return 0; } Student::Student (int the_id, char the_major) id = the_id; major = the_major; } Student::Student (int the_id) id = the_id; } Student::Student () id = 0; major = 'X'; } void Student::output() cout<< id; cout<< major; }

Classes & ADTs Constructors 1. Default Constructor 2. Constructor with all member variables 3. Constructor with some member variables class School public: private: int NumOfStudents; int NumOf Classes; double Area; } School (); School (int students, int classes, double area); Student (int students); Student (int students, int classes); Student (int classes, double area);

Classes & ADTs Constructor Definitions: 1. Must have the same name as the class 2. Definition cannot return a value. No type not even void- can be given at the start of the function prototype or header How can I call a constructor in main? int main() School myschool; School YourSchool(200,10,5000); School AnotherSchool(); AnotherSchool.School(300,15,4000);

Classes & ADTs Accessor Functions Functions that give you access to the values of the private member variables. class School public: private: int NumOfStudents; int NumOf Classes; double Area; } int get_students(); //Return the number of students in a school int get_classes(); //Return the number of classes in a school double get_area(); //Return the area of a school

Classes & ADTs Private members need for Accessor Functions int get_id(); //returns the student id char get_major(); //returns the student major Student ID Major void set_id(int new_id); //assigns a value to student id void set_major(char new_major); //assigns a value to student major Student public: }; int id; char major;

Classes & ADTs Class DayOfYear public: void output(); private: int month; int day; }; Restriction: Once you make a member variable private, the only way to access it or change its value is by using one of the member functions. private member variables int main() DayOfYear Today; cin >> Today.month; cout << Today.month; If (Today.month ==1) cout << January ; ILLEGAL!

6.2 Classes Member Function Definition void DayOfYear::output() } cout<< month= << month; cout<< day= << day; cout<<endl; Class DayOfYear public: void output(); private: int month; int day; }; Private members may be used in member function definitions (but not elsewhere).

6.2 Classes Class Sample public: int void void private: int int void }; variable; output(); input(); month; day; dostuff(); public members private members Public members can be used in the main body of your program or in the definition of any function, even a nonmember function.

1 // Fig. 6.3: fig06_03.cpp 2 // Time class. 3 #include <iostream> 4 5 using std::cout; 6 using std::endl; 7 8 #include <iomanip> 9 10 using std::setfill; Define class Time. 11 using std::setw; 12 13 // Time abstract data type (ADT) definition 14 class Time 15 16 public: 17 Time(); // constructor 18 void settime( int, int, int ); // set hour, minute, second 19 void printuniversal(); // print universal-time format 20 void printstandard(); // print standard-time format 21 fig06_03.cpp (1 of 5)

22 private: 23 int hour; // 0-23 (24-hour clock format) 24 int minute; // 0-59 25 int second; // 0-59 26 27 }; // end class Time 28 Constructor initializes 29 // Time constructor initializes each data member to zero private and data members to 0. 30 // ensures all Time objects start in a consistent state 31 Time::Time() 32 33 hour = minute = second = 0; 34 35 } // end Time constructor 36 37 // set new Time value using universal time, perform validity 38 // checks on the data values and set invalid values to zero 39 void Time::setTime( int h, int m, int s ) 40 41 hour = ( h >= 0 && h < 24 )? h : 0; 42 minute = ( m >= 0 && m < 60 )? m : 0; 43 second = ( s >= 0 && s < 60 )? s : 0; 44 45 } // end function settime 46 fig06_03.cpp (2 of 5) public member function checks parameter values for validity before setting private data members.

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; 53 54 } // end function printuniversal 55 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 Declare variable t to be object of 62 << ( hour < 12? " AM" : " PM" ); class Time. 63 64 } // end function printstandard 65 66 int main() 67 68 Time t; // instantiate object t of class Time 69 No arguments (implicitly know purpose is to print data members); member function calls more concise. fig06_03.cpp (3 of 5)

70 // output Time object t's initial values 71 cout << "The initial universal time is "; 72 t.printuniversal(); // 00:00:00 73 74 cout << "\nthe initial standard time is "; 75 t.printstandard(); // 12:00:00 AM 76 77 t.settime( 13, 27, 6 ); // change time 78 79 // output Time object t's new values 80 cout << "\n\nuniversal time after settime is "; Attempt to set data members to 81 t.printuniversal(); // 13:27:06 invalid values using public 82 member function. 83 cout << "\nstandard time after settime is "; 84 t.printstandard(); // 1:27:06 PM 85 86 t.settime( 99, 99, 99 ); // attempt invalid settings 87 88 // output t's values after specifying invalid values 89 cout << "\n\nafter attempting invalid settings:" 90 << "\nuniversal time: "; 91 t.printuniversal(); // 00:00:00 92 Invoke public member functions to print time. Set data members using public member function. fig06_03.cpp (4 of 5)

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