CMSC202 Computer Science II for Majors Lecture 07 – Classes and Objects (Continued) Dr. Katherine Gibson Based on slides by Chris Marron at UMBC.

Slides:



Advertisements
Similar presentations
Objectives Introduction to Inheritance and Composition (Subclasses and SuperClasses) Overriding (and extending), and inheriting methods and constructors.
Advertisements

Road Map Introduction to object oriented programming. Classes
Terms and Rules Professor Evan Korth New York University (All rights reserved)
More C++ Bryce Boe 2013/07/18 CS24, Summer 2013 C.
CSC 142 Computer Science II Zhen Jiang West Chester University
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 13: Introduction to Classes.
Agenda Object Oriented Programming Reading: Chapter 14.
An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.
Classes In C++ 1. What is a class Can make a new type in C++ by declaring a class. A class is an expanded concept of a data structure: instead of holding.
CPSC 252 The Big Three Page 1 The “Big Three” Every class that has data members pointing to dynamically allocated memory must implement these three methods:
CMSC 202 Computer Science II for Majors. CMSC 202UMBC Topics Objects and Classes Project 2 description.
Copyright © 2002 W. A. Tucker1 Chapter 10 Lecture Notes Bill Tucker Austin Community College COSC 1315.
Chapter 3 Introduction to Classes and Objects Definitions Examples.
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?
Lecture 19: Introduction to Classes Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
Object-Oriented Programming (OOP) What we did was: (Procedural Programming) a logical procedure that takes input data, processes it, and produces output.
Classes - Part II (revisited) n Constant objects and member functions n Definition Form of Member Functions n friend functions and friend classes n Constructors.
CIS162AD Constructors Part 2 08_constructors.ppt.
Defining Classes I Part B. Information hiding & encapsulation separate how to use the class from the implementation details separate how to use the class.
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.
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.
Class Definitions and Writing Methods Chapter 3 3/31/16 & 4/4/16.
Class Definitions and Writing Methods Chapter 3 10/12/15 & 10/13/15 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education.
Class Definitions: The Fundamentals Chapter 6 3/30/15 & 4/2/15 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education.
OOP Details Constructors, copies, access. Initialize Fields are not initialized by default:
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.
Computer Programming II Lecture 5. Introduction to Object Oriented Programming (OOP) - There are two common programming methods : procedural programming.
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.
CMSC202 Computer Science II for Majors Lecture 05 – References Dr. Katherine Gibson Based on slides by Chris Marron at UMBC.
CMSC202 Computer Science II for Majors Lecture 09 – Overloaded Operators and More Dr. Katherine Gibson Based on slides by Chris Marron at UMBC.
CMSC202 Computer Science II for Majors Lecture 16 – Exceptions
Creating Your Own Classes
Classes (Part 1) Lecture 3
Classes C++ representation of an object
Classes and Objects: Encapsulation
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?
Object-Oriented Programming Part 1 07_classes.ppt
Abstract Data Types Programmer-created data types that specify
CMPE 135: Object-Oriented Analysis and Design September 14 Class Meeting Department of Computer Engineering San Jose State University Fall 2017 Instructor:
Auburn University COMP 3000 Object-Oriented Programming for Engineers and Scientists Constructors and Other Tools Dr.
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
HKCT Java OOP Unit 02 Object Oriented Programming in Java Unit 02 Methods, Classes, and Objects 1.
Defining Classes II.
Classes and Objects: Encapsulation
Introduction to Classes
Classes and Objects Encapsulation
CMSC202 Computer Science II for Majors Lecture 04 – Pointers
Learning Objectives Classes Constructors Principles of OOP
Classes Short Review of Topics already covered Constructor
How Classes Work with Memory Diagram
Object Oriented Programming (OOP) Lecture No. 13
Introduction of Programming
CMSC202 Computer Science II for Majors Lecture 10 and 11 – Inheritance
Classes C++ representation of an object
CMSC 202 Lesson 8 Classes II.
CMSC 202 Encapsulation Version 9/10.
How Classes Work with Memory Diagram
Lecture 8 Object Oriented Programming (OOP)
Classes and Objects Systems Programming.
Presentation transcript:

CMSC202 Computer Science II for Majors Lecture 07 – Classes and Objects (Continued) Dr. Katherine Gibson Based on slides by Chris Marron at UMBC

Last Class We Covered Object Oriented Programming Versus Procedural Programming Classes Members Member variables Member functions (class methods) Livecoding: Rectangle class

Any Questions from Last Time?

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

Class Access Specifiers

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 May set variables to invalid values

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

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

Public Access Specifier 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?

Private Access Specifier Private member variables and functions can only 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!

Protected Access Specifier 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)

Access Specifiers for Date Class class Date { ???????: void Output(); int m_month; int m_day; int m_year; };

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

Other Member Functions

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)

Member Function Types There are many ways of classifying types, but here are the ones we’ll use: Accessors (“Getters”) Mutators (“Setters”) Facilitators (“Helpers”)

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

Member Function: Mutators Name starts with Set, ends with member name Allows controlled changing of the value of a private data member Examples: void SetMonth(int month); void SetDay (int day); void SetYear (int year); Don’t generally return anything

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; } what’s wrong with this function?

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; } } in what file would you store these constants?

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)

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; }; for the sake of brevity, we’ll generally leave out the accessors and mutators when showing examples

Constructors

Constructors Automatically called when an object is created 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);

Constructor Syntax Syntax: Notice that... For prototype: ClassName(); For definition: ClassName::ClassName() { /* code */ } Notice that... There is no return type Same name as class!

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!

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; } } is this the best way to handle this? what might be a better solution?

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

Time for… LIVECODING!!!

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!

Designing a Class Ask yourself: Rules of thumb: 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)

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!