Classes and Data Abstraction

Slides:



Advertisements
Similar presentations
 2003 Prentice Hall, Inc. All rights reserved. ECE 2552 Dr. S. Kozaitis Summer Summary of Topics Related to Classes Class definition Defining member.
Advertisements

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 10 Defining Classes.
 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.
Copyright © 2003 Pearson Education, Inc. Slide 1.
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)
1 Classes and Data Abstraction Andrew Davison Noppadon Kamolvilassatian Department of Computer Engineering Prince of Songkla University.
 2003 Prentice Hall, Inc. All rights reserved. 1 IS 0020 Program Design and Software Tools Introduction to C++ Programming Lecture 4 - Classes Jan 27,
Chapter 6 Structures and Classes. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-2 Structures  2 nd aggregate data type: struct  Recall:
1 Fall 2002 ACS 168 Problem Solving Using the Computer Weeks 9 and 10 Structures and Classes Class Diagrams Weeks 9 and 10 Structures and Classes Class.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 10 Defining Classes.
1 Classes and Data Abstraction Part II Initializing Class Objects: Constructors Constructors  Initialize data members  Same name as class 
Chapter 10 Defining Classes. Slide Overview 10.1 Structures 10.2 Classes 10.3 Abstract Data Types.
1 Chapter 10 Defining Classes. 2 Overview Introduction Classes (10.2) Abstract Data Types and Structures (10.1)(10.3) Abstract Data Types and Structures.
 Classes in c++ Presentation Topic  A collection of objects with same properties and functions is known as class. A class is used to define the characteristics.
Classes Definition A class is a data type whose variables are objects Object – a variable that has member functions as well the ability to hold.
Chapter 6 Defining Classes. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. Slide 2 Overview Introduction Structures (6.1) Classes (6.2)
1 Classes and Data Abstraction Part I Introduction Object-oriented programming (OOP)  Encapsulates data (attributes) and functions (behavior)
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 6: Classes and Data Abstraction Outline 6.1Introduction 6.2Structure Definitions 6.3Accessing.
1 const and this Ying Wu Electrical Engineering & Computer Science Northwestern University EECS 230 Lectures Series.
 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.
 2003 Prentice Hall, Inc. All rights reserved. 1 Lecture 6: Classes and Data Abstraction Posted Feb 3 Chapter 6 pointer for function Class Introduction.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 20 November 10, 2009.
Slide 1 Chapter 6 Structures and Classes. Slide 2 Learning Objectives  Structures  Structure types  Structures as function arguments  Initializing.
 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.
Programming Fundamentals1 Chapter 7 INTRODUCTION TO CLASSES.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-1 Learning Objectives  Classes  Constructors  Principles of OOP  Class type member.
100 學年度碩士班新生暑期課程 程式設計 Object-Oriented Programming: Part I The Basics of Classes.
C++ Programming Lecture 13 Functions – Part V By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
 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.
 2003 Prentice Hall, Inc. All rights reserved. 1 IS 0020 Program Design and Software Tools Introduction to C++ Programming Lecture 3 Jan 20, 2004.
CSIS 123A Lecture 1 Intro To Classes Glenn Stevenson CSIS 113A MSJC.
Copyright © 2014 Pearson Addison-Wesley. All rights reserved. Chapter 10 Defining Classes.
Chapter 16: Classes and Data Abstraction
Structures and Classes
Lecture 5: Classes (continued) Feb 1, 2005
Classes and Data Abstraction
Structures Revisited what is an aggregate construct? What aggregate constructs have we studied? what is a structure? what is the keyword to define a structure?
Chapter 16: Classes and Data Abstraction
Chapter 10 Defining Classes. Chapter 10 Defining Classes.
10.2 Classes Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 1.
Structures Revisited what is an aggregate construct? What aggregate constructs have we studied? what is a structure? what is the keyword to define a structure?
This technique is Called “Divide and Conquer”.
Classes: A Deeper Look Outline
Lecture 5: Classes September 14, 2004
Chapter 6: Classes and Data Abstraction
Learning Objectives Classes Constructors Principles of OOP
Introduction to Classes and Objects
Chapter 6: Classes and Data Abstraction
Classes Short Review of Topics already covered Constructor
7.4 friend Functions and friend Classes
Object-oriented programming (OOP)
Chapter 6: Classes and Data Abstraction
CS360 Client/Server Programming Using Java
CISC181 Introduction to Computer Science Dr
Chapter 7: Classes Part II
Recitation Course 0603 Speaker: Liu Yu-Jiun.
Chapter 6: Classes and Data Abstraction
Chapter 6: Classes and Data Abstraction
CMSC 202 Lesson 8 Classes II.
Classes and Objects Systems Programming.
Introduction to Classes and Objects
Presentation transcript:

Classes and Data Abstraction Topic 5

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

Classes Syntax: class Class_Name { public: private: }; public members 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: }; public members char get_color(); int number_of_speeds(); void set (int the_speeds, char the_color); private: int speeds; char color; }; public members private members Bicycle my_bike, your_bike

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

Classes DayOfYear today; today.month = 2; today.day = 10; 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 Scope Resolution Operator (::) Dot Operator (.) Used with Class name Example: void Student::output() Dot Operator (.) Used with Objects – class variables Example: new_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) Student::Student () { id = 0; major = 'X'; void Student::output() { cout<< id; cout<< major;

Classes & ADTs Constructors Default Constructor Constructor with all member variables 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: Must have the same name as the class 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 Student ID Major int get_id(); //returns the student id char get_major(); //returns the student 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 Class DayOfYear Member Function Definition { public: void output(); private: int month; int day; }; void DayOfYear::output() { cout<< “month= “<< month; cout<<“day=“ << day; cout<<endl; } Private members may be used in member function definitions (but not elsewhere).

6.2 Classes Class Sample { public: int variable; void output(); void input(); private: int month; int day; void 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 non- member 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; 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) Define class Time.

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 29 // Time constructor initializes each data member to zero and 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) Constructor initializes private data members to 0. 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 62 << ( hour < 12 ? " AM" : " PM" ); 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) Declare variable t to be object of class Time.

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 "; 81 t.printUniversal(); // 13:27:06 82 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. fig06_03.cpp (4 of 5) Set data members using public member function. Attempt to set data members to invalid values using public member function.

93 cout << "\nStandard time: "; 94 t.printStandard(); // 12:00:00 AM 95 cout << endl; 96 97 return 0; 98 99 } // end main fig06_03.cpp (5 of 5) fig06_03.cpp output (1 of 1) 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 After attempting invalid settings: Universal time: 00:00:00 Standard time: 12:00:00 AM Data members set to 0 after attempting invalid settings.