CMSC 202 Lesson 8 Classes II.

Slides:



Advertisements
Similar presentations
Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
Advertisements

Introduction to Programming Lecture 34. In Today’s Lecture Arrays of objects Arrays of objects Interaction of Arrays with Free Store Interaction of Arrays.
1 Engineering Problem Solving With C++ An Object Based Approach Chapter 5 Functions.
Functions Most useful programs are much larger than the programs that we have considered so far. To make large programs manageable, programmers modularize.
Chapter 6 Control Structures.
1 By Joaquin Vila Preparedbv Sally Scott ACS 168 Problem Solving Using the Computer Chapter 6 Classes Chapter 6 Classes.
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 © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Procedural and Object-Oriented Programming 13.1.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 13: Introduction to Classes.
CMSC 202 Lesson 7 Classes I. Warmup  Add the correct parameter list and return type to the following function: (hint: all the information you need is.
Chapter 3 Part I. 3.1 Introduction Programs written in C ◦ All statements were located in function main Programs written in C++ ◦ Programs will consist.
CSC 205 Java Programming II Defining & Implementing Classes.
By Joaquin Vila Prepared by Sally Scott ACS 168 Problem Solving Using the Computer Week 13 More on Classes Chapter 8 Week 13 More on Classes Chapter 8.
Friend Functions.  An ordinary function that is given special access to the private members of a class  NOT a member function of the class  Prototype.
Lecture 19: Introduction to Classes Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 7: Introduction to Classes and Objects Starting Out with C++ Early.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 15. Dictionaries (1): A Key Table Class.
Defining Classes I Part B. Information hiding & encapsulation separate how to use the class from the implementation details separate how to use the class.
CMSC 202 Lesson 5 Functions I. Warmup Use setw() to print the following (in tabular format) Fred Flintstone Barney Rubble Bugs Bunny Daffy Duck.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 13: Introduction to Classes.
CPSC 252 ADTs and C++ Classes Page 1 Abstract data types (ADTs) An abstract data type is a user-defined data type that has: private data hidden inside.
100 學年度碩士班新生暑期課程 程式設計 Object-Oriented Programming: Part I The Basics of Classes.
Classes CMSC 202. Version 9/12 2 Programming & Abstraction All programming languages provide some form of abstraction. –Also called information hiding.
CMSC 202 Lesson 8 Classes II. Warmup Declare a class (.h part) named “Cup” It has a data member that says how full it is One method called “Drink” that.
CMSC 202 Lesson 9 Classes III. Warmup Using the following part of a class, implement the Sharpen() method, it removes 1 from the length: class Pencil.
CSIS 123A Lecture 1 Intro To Classes Glenn Stevenson CSIS 113A MSJC.
CMSC202 Computer Science II for Majors Lecture 06 – Classes and Objects Dr. Katherine Gibson Based on slides by Chris Marron at UMBC.
General Updates ● The Wiki is now back up to date. I have also added a working link to the lecture slides online. ● Whenever you notice something missing.
Copyright © 2014 Pearson Addison-Wesley. All rights reserved. Chapter 10 Defining Classes.
COMP 53 – Week Two Operator Overloading.
Programming Abstractions
Structures and Classes
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 7: Introduction to Classes and Objects
Abstract Data Types Programmer-created data types that specify
Chapter 10 Defining Classes. Chapter 10 Defining Classes.
Chapter 10 Defining Classes
10.2 Classes Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 1.
CMSC202 Computer Science II for Majors Lecture 08 – Overloaded Constructors Dr. Katherine Gibson Based on slides by Chris Marron at UMBC.
Introduction to Classes
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?
CMPE Data Structures and Algorithms in C++ February 22 Class Meeting
Classes and Objects 2nd Lecture
User-defined Functions
Classes and Objects: Encapsulation
Introduction to Classes
Classes and Objects Encapsulation
Classes and Data Abstraction
CMSC 202 Lesson 7 Classes I.
User-defined Functions
Learning Objectives Classes Constructors Principles of OOP
Classes Short Review of Topics already covered Constructor
Java Classes and Objects 3rd Lecture
Friends, Overloaded Operators, and Arrays in Classes
Exceptions 1 CMSC 202.
Classes.
Java Classes and Objects
CMSC 202 Classes and Objects.
CMSC202 Computer Science II for Majors Lecture 07 – Classes and Objects (Continued) Dr. Katherine Gibson Based on slides by Chris Marron at UMBC.
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
CMSC 202 Encapsulation Version 9/10.
Lecture 8 Object Oriented Programming (OOP)
CMSC 202 Lesson 20 Exceptions 1.
Classes and Objects Systems Programming.
2015 January February March April May June July August September
CMSC 202 Lesson 5 Functions I.
Presentation transcript:

CMSC 202 Lesson 8 Classes II

Warmup Declare a class (.h part) named “Cup” It has a data member that says how full it is One method called “Drink” that removes some portion of the cup [portion is randomly decided] What should method return? What parameters? One method called “Fill” that fills the cup Create a main function that: Drinks the entire cup Fills the cup

Announcements Proj1 Due on Sunday, 11:59 pm If you haven’t started, you’re already behind! Get help from Me/TAs/Tutors today and tomorrow

Class Review Class Object Syntax: Blueprint, pattern, model, etc. for a data type Describes an object Has Data and Operations on that data Object An instance of a class Stores data for a single instance Syntax: class ClassName { public: // functions // data private: };

Class Goals Abstraction Encapsulation Definition… Provide a simple interface to other classes/functions Information Hiding Hide details of data storage and implementation Encapsulation Control access to data Private versus Public Definition… Classes describe user-defined ADTs Abstract Data Types

Class Member Access Public Private Any code can access this member Private Only members of the class can access this member Default? If nothing defined, members are private Syntax: class ClassName { public: // public functions // public data private: // private functions // private data };

Improved DayOfYear Class class DayOfYear { public: void Input( ); void Output( ); void Set( int newMonth, int newDay ); void Set( int newMonth ); int GetMonthNumber( ); int GetDay( ); private: int m_month; int m_day; }; This is the Class declaration – belongs in DayOfYear.h

Using DayOfYear Class int main( ) { DayOfYear today; // Attempt to use private data… today.m_month = 2; // ERROR! today.m_day = 23; // ERROR! cout << “Today: “ << m_month << “/” << m_day << endl; // ERROR! // Instead, use public methods… today.Set( 2, 23 ); cout << “Today: “ << today.GetMonth() << “/” << today.GetDay() << endl; return 0; }

Practice Rewrite your Cup class from the warmup to use public/private protection class Cup { public: bool Drink( ); void Fill( ); private: int m_fullness; };

Improved DayOfYear Class class DayOfYear { public: void Input( ); void Output( ); void Set( int newMonth, int newDay ); void Set( int newMonth ); int GetMonthNumber( ); int GetDay( ); private: int m_month; int m_day; }; What are these methods?

Class Methods Accessors Mutators Facilitators (Services) Allow outside code to inspect a private data member Start with “Get” (usually) Mutators Allow outside code to modify a private data member’ Start with “Set” (usually) Facilitators (Services) Provide some service for outside code Print all class data Retrieve data from user Format data into a string Calculate something

Accessors, Mutators, Facilitators? class DayOfYear { public: void Input( ); void Output( ); void Set( int newMonth, int newDay ); void Set( int newMonth ); int GetMonthNumber( ); int GetDay( ); private: int m_month; int m_day; }; Facilitators Mutators Accessors

Class Implementation (Simple…) void DayOfYear::Set( int newMonth, int newDay ) { m_month = newMonth; m_day = newDay; } void DayOfYear::Set( int newMonth ) m_day = 1; int DayOfYear::GetMonthNumber( ) return m_month; int DayOfYear::GetDay( ) return m_day; These method implementations belong in DayOfYear.cpp file How could the Set methods be improved?

Class Implementation (Improved) //--------------------------------------------------- // Set // PreConditions: // 1 <= newMonth <= 12 // 1 <= newDay <= 31 // PostConditions: // day of year changed to user supplied values // if an error, exit program void DayOfYear::Set(int newMonth, int newDay) { if ((newMonth >= 1) && (newMonth <= 12)) m_month = newMonth; else cout << "Illegal month value! Program aborted.\n"; exit(1); } if ((newDay >= 1) && (newDay <= 31)) m_day = newDay; cout << "Illegal day value! Program aborted.\n";

More Improvements How else could this be improved? Valid day for each month Ex: April has 30 days Valid day for month and year Ex: February has 28 or 29 days, depending on year Bad data? Set to “safe” value (ex: 1 for month or day) Print an error & keep data Return “false” to indicate illegal state Set flag to “invalid object” (Zombie objects)

DayOfYear Input void DayOfYear::Input( ) { cout << "Enter the month as a number: "; cin >> m_month; cout << "Enter the day of the month: "; cin >> m_day; if ((m_month < 1) || (m_month > 12) || (m_day < 1) || (m_day > 31)) cerr << "Illegal date! Program aborted.\n"; exit(1); }

DayOfYear Output void DayOfYear::Output( ) { switch (m_month) case 1: cout << "January "; break; case 2: cout << "February "; break; case 3: cout << "March "; break; case 4: cout << "April "; break; case 5: cout << "May "; break; case 6: cout << "June "; break; case 7: cout << "July "; break; case 8: cout << "August "; break; case 9: cout << "September "; break; case 10: cout << "October "; break; case 11: cout << "November "; break; case 12: cout << "December "; break; default: cout << "Error in DayOfYear::Output."; break; } cout << m_day;

Using DayOfYear Class int main( ) { DayOfYear today, bachBirthday; // input and echo today's date cout << "Enter today's date:\n"; today.Input( ); cout << "Today's date is "; today.Output( ); cout << endl; // set and output JSB's birthday bachBirthday.Set(3, 21); cout << "J. S. Bach's birthday is "; bachBirthday.Output( ); cout << endl; // output special message if ((today.GetMonthNumber( ) == bachBirthday.GetMonthNumber( )) && (today.GetDay( ) == bachBirthday.GetDay( ) )) cout << "Happy Birthday Johann Sebastian!\n"; else cout << "Happy Unbirthday Johann Sebastian!\n"; return 0; }

Class Design Ask yourself: What properties must each object have? What data-types should each of these be? Which should be private? Which should be public? What operations must each object have? What accessors, mutators, facilitators? What parameters must each of these have? Const, by-value, by-reference, default? What return value should each of these have? Const, by-value, by-reference? Rules of thumb: Data should be private (usually) Operations should be public (usually) At least 1 mutator and 1 accessor per data member (usually)

Guarding Header Files To use a class, must #include declaration #include “className.h” Every file that uses class should #include it How do you protect from including twice? #ifndef CLASSNAME_H #define CLASSNAME_H // class declaration here… #endif Guard EVERY .h file Include EVERY .h file that you directly use

Practice Design & Implement the “Stapler” class Data Operations Number of Staples Integer Private Operations Fill – fill stapler to max capacity Parameters? None Return value? None Public Staple – dispense one staple Return value? Bool – was action successful or not

Challenge Design and Declare an “Alarm Clock” class that beeps when the alarm goes off… What properties? What operations? Implement your Alarm Clock class Assume there are functions implemented in a standard library called: int GetCurrentHour(); - returns 0 to 23 int GetCurrentMinute(); - returns 0 to 59 Assume there exists an external mechanism to make the clock update every minute...keep it simple… Write a main function that Displays the current time to the user Sets the alarm for 9:51 am (so that you’re not late for your 10 am class)