Computer Science II for Majors Lecture 07 Classes and Objects - - PowerPoint PPT Presentation

computer science ii for majors
SMART_READER_LITE
LIVE PREVIEW

Computer Science II for Majors Lecture 07 Classes and Objects - - PowerPoint PPT Presentation

CMSC202 Computer Science II for Majors Lecture 07 Classes and Objects (Continued) Dr. Katherine Gibson Based on slides by Chris Marron at UMBC www.umbc.edu Last Class We Covered Object Oriented Programming Versus Procedural


slide-1
SLIDE 1

www.umbc.edu

CMSC202 Computer Science II for Majors

Lecture 07 –

Classes and Objects (Continued)

  • Dr. Katherine Gibson

Based on slides by Chris Marron at UMBC

slide-2
SLIDE 2

www.umbc.edu

Last Class We Covered

  • Object Oriented Programming

– Versus Procedural Programming

  • Classes

– Members

  • Member variables
  • Member functions (class methods)
  • Livecoding: Rectangle class

2

slide-3
SLIDE 3

www.umbc.edu

Any Questions from Last Time?

slide-4
SLIDE 4

www.umbc.edu

Today’s Objectives

  • To understand more about classes in C++

– Learn the uses for access modifiers – Discuss more types of methods

  • Accessors
  • Mutators
  • Facilitators
  • Constructors

– Overloading class methods

4

slide-5
SLIDE 5

www.umbc.edu

Class Access Specifiers

slide-6
SLIDE 6

www.umbc.edu

Access Specifiers

  • In our definition for the DayOfYear class,

everything was public –This is not good practice!

  • Why?

– Encapsulation! We don’t want the end user to have direct access to the data – Why?

  • May set variables to invalid values

6

slide-7
SLIDE 7

www.umbc.edu

Access Specifier Types

  • We have three different options for

access specifiers, each with their own role:

– public – private – protected

  • Used to specify access for member variables

and functions inside the class

7

slide-8
SLIDE 8

www.umbc.edu

Toy Syntax Example

class Date { public: int m_month; private: int m_day; protected: int m_year; };

8

slide-9
SLIDE 9

www.umbc.edu

Public Access Specifier

  • public

–Anything that has access to a Date object also has access to all public member variables and functions

  • Normally used for functions

– But not all functions

  • Need to have at least one public member

– Why?

9

slide-10
SLIDE 10

www.umbc.edu

Private Access Specifier

  • private

–Private member variables and functions can

  • nly be accessed by member functions of the

Date class –Cannot be accessed in main(), in other files, or by other functions

  • If not specified, members default to private
  • Should specify anyway – good coding practices!

10

slide-11
SLIDE 11

www.umbc.edu

Protected Access Specifier

  • protected

–Protected member variables and functions can only be accessed by:

  • Member functions of the Date class
  • Member functions of any derived classes
  • (We’ll cover this in detail later)

11

slide-12
SLIDE 12

www.umbc.edu

Access Specifiers for Date Class

class Date { ???????: void Output(); ???????: int m_month; int m_day; int m_year; };

12

slide-13
SLIDE 13

www.umbc.edu

Access Specifiers for Date Class

class Date { public: void Output(); private: int m_month; int m_day; int m_year; };

13

slide-14
SLIDE 14

www.umbc.edu

Other Member Functions

slide-15
SLIDE 15

www.umbc.edu

New Member Functions

  • Now that m_month, m_day, and m_year

are private, how do we give them values, or retrieve those values?

  • Write public member functions to provide

indirect, controlled access for the user

  • Remember, there is an ideal:

–User only knows interface (public functions) not implementation (private variables)

15

slide-16
SLIDE 16

www.umbc.edu

Member Function Types

  • There are many ways of classifying types, but

here are the ones we’ll use:

  • Accessors

(“Getters”)

  • Mutators

(“Setters”)

  • Facilitators

(“Helpers”)

16

slide-17
SLIDE 17

www.umbc.edu

Member Function: Accessors

  • Name starts with Get, ends with member name
  • Allows retrieval of private data members
  • Examples:

int GetMonth(); int GetDay(); int GetYear();

  • Don’t generally take in arguments

17

slide-18
SLIDE 18

www.umbc.edu

Member Function: Mutators

  • Name starts with Set, ends with member name
  • Allows controlled changing of the value
  • f a private data member
  • Examples:

void SetMonth(int month); void SetDay (int day); void SetYear (int year);

  • Don’t generally return anything

18

slide-19
SLIDE 19

www.umbc.edu

Mutator for SetMonth()

  • How would you design a good mutator for the

SetMonth() member function?

void Date::SetMonth(int month) { if (month >= 1 && month <= 12) { m_month = month; } else { m_month = 1; } }

19

what’s wrong with this function?

slide-20
SLIDE 20

www.umbc.edu

Better Mutator for SetMonth()

  • This version of the SetMonth() member

function doesn’t use magic numbers!

void Date::SetMonth(int month) { if (month >= MIN_MONTH && month <= MAX_MONTH) { m_month = month; } else { m_month = DEFAULT_MONTH; } }

20

in what file would you store these constants?

slide-21
SLIDE 21

www.umbc.edu

Member Function: Facilitators

  • Provide support for the class’s operations
  • public if generally called outside function
  • private/protected if only called by

member functions

  • Examples:

void OutputMonth(); (public) void IncrementDate(); (private)

21

slide-22
SLIDE 22

www.umbc.edu

Date with Specifiers

class Date { public: void Output (); int GetMonth(); int GetDay(); int GetYear(); void SetMonth(int month); void SetDay (int day); void SetYear (int year); private: int m_month; int m_day; int m_year; };

22

for the sake of brevity, we’ll generally leave out the accessors and mutators when showing examples

slide-23
SLIDE 23

www.umbc.edu

Constructors

slide-24
SLIDE 24

www.umbc.edu

Constructors

  • Special methods that “build” (construct) an object

– Supply default values – Initialize an object

  • Automatically called when an object is created

– implicit: Date today; – explicit: Date today(7, 28, 1914);

24

slide-25
SLIDE 25

www.umbc.edu

Constructor Syntax

  • Syntax:

– For prototype: ClassName(); – For definition: ClassName::ClassName() { /* code */ }

  • Notice that...

– There is no return type – Same name as class!

25

slide-26
SLIDE 26

www.umbc.edu

Constructor Definition

Date::Date (int month, int day, int year) { m_month = month; m_day = day; m_year = year; }

  • What is missing from this constructor?

–Technically, nothing -- but... –Validation of the information being passed in!

26

slide-27
SLIDE 27

www.umbc.edu

Better Constructor Definition

Date::Date (int month, int day, int year) { if (m > 0 && m <= 12) { m_month = month; } else { m_month = 1; } if (d > 0 && d <= 31) { m_day = day; } else { m_day = 1; } if (y > 0 && y <= 2100) { m_year = year; } else { m_year = 1; } }

27

is this the best way to handle this? what might be a better solution?

slide-28
SLIDE 28

www.umbc.edu

Best Constructor Definition

Date::Date (int month, int day, int year) { SetMonth(month); SetDay(day); SetYear(year); }

  • This allows us to reuse already written code

28

slide-29
SLIDE 29

www.umbc.edu

Time for…

29

slide-30
SLIDE 30

www.umbc.edu

Livecoding Exercise

  • Update our Rectangle class with

– Constructor – Accessors and Mutators – Class methods to:

  • Calculate area
  • Calculate perimeter
  • Check if it’s Square
  • Print the rectangle’s dimensions
  • Create a main() function and use it!

30

slide-31
SLIDE 31

www.umbc.edu

Designing a Class

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

31

slide-32
SLIDE 32

www.umbc.edu

Announcements

  • Project 1 has been released
  • Found on Professor’s Marron website
  • Due by 9:00 PM on February 23rd
  • Get started on it now!
  • Make sure to read and follow the

coding standards for this course!

  • Next time: Wrap Up and Review for Exam 1!

32