CMSC 202 - PowerPoint PPT Presentation

About This Presentation
Title:

CMSC 202

Description:

CMSC 202 Lesson 8 Classes II – PowerPoint PPT presentation

Number of Views:81
Avg rating:3.0/5.0
Slides: 23
Provided by: DanaW150
Category:

less

Transcript and Presenter's Notes

Title: CMSC 202


1
CMSC 202
  • Lesson 8
  • Classes II

2
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
  • What should method return?
  • What parameters?
  • Create a main function that
  • Drinks the entire cup
  • Fills the cup

3
Announcements
  • Proj1
  • Due on Sunday, 1159 pm
  • If you havent started, youre already behind!
  • Get help from Me/TAs/Tutors today and tomorrow

4
Class Review
  • Class
  • 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
  • // functions
  • // data

5
Class Goals
  • Abstraction
  • 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

6
Class Member Access
  • Public
  • 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

7
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
8
Using DayOfYear Class
  • int main( )
  • DayOfYear today
  • // Attempt to use private data
  • today.m_month 2 // ERROR!
  • today.m_day 23 // ERROR!
  • cout ltlt Today ltlt m_month ltlt /
  • ltlt m_day ltlt endl // ERROR!
  • // Instead, use public methods
  • today.Set( 2, 23 )
  • cout ltlt Today ltlt today.GetMonth() ltlt /
  • ltlt today.GetDay() ltlt endl
  • return 0

9
Practice
  • Rewrite your Cup class from the warmup to use
    public/private protection
  • class Cup
  • public
  • bool Drink( )
  • void Fill( )
  • private
  • int m_fullness

10
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?
11
Class Methods
  • Accessors
  • 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

12
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
13
Class Implementation (Simple)
  • void DayOfYearSet( int newMonth, int newDay )
  • m_month newMonth
  • m_day newDay
  • void DayOfYearSet( int newMonth )
  • m_month newMonth
  • m_day 1
  • int DayOfYearGetMonthNumber( )
  • return m_month
  • int DayOfYearGetDay( )

These method implementations belong in
DayOfYear.cpp file
How could the Set methods be improved?
14
Class Implementation (Improved)
  • //------------------------------------------------
    ---
  • // Set
  • // PreConditions
  • // 1 lt newMonth lt 12
  • // 1 lt newDay lt 31
  • // PostConditions
  • // day of year changed to user supplied values
  • // if an error, exit program
  • //------------------------------------------------
    ---
  • void DayOfYearSet(int newMonth, int newDay)
  • if ((newMonth gt 1) (newMonth lt 12))
  • m_month newMonth
  • else
  • cout ltlt "Illegal month value! Program
    aborted.\n"
  • exit(1)
  • if ((newDay gt 1) (newDay lt 31))

15
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)

16
DayOfYear Input
  • void DayOfYearInput( )
  • cout ltlt "Enter the month as a number "
  • cin gtgt m_month
  • cout ltlt "Enter the day of the month "
  • cin gtgt m_day
  • if ((m_month lt 1) (m_month gt 12)
  • (m_day lt 1) (m_day gt 31))
  • cerr ltlt "Illegal date! Program aborted.\n"
  • exit(1)

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

18
Using DayOfYear Class
  • int main( )
  • DayOfYear today, bachBirthday
  • // input and echo today's date
  • cout ltlt "Enter today's date\n"
  • today.Input( )
  • cout ltlt "Today's date is "
  • today.Output( ) cout ltlt endl
  • // set and output JSB's birthday
  • bachBirthday.Set(3, 21)
  • cout ltlt "J. S. Bach's birthday is "
  • bachBirthday.Output( )
  • cout ltlt endl
  • // output special message
  • if ((today.GetMonthNumber( )
    bachBirthday.GetMonthNumber( ))
  • (today.GetDay( ) bachBirthday.GetDay( )
    ))

19
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?
  • Which should be private? Which should be public?
  • Rules of thumb
  • Data should be private (usually)
  • Operations should be public (usually)
  • At least 1 mutator and 1 accessor per data member
    (usually)

20
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

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

22
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 951 am (so that youre not
    late for your 10 am class)
Write a Comment
User Comments (0)
About PowerShow.com