Classes: Implementation and Testing

Slides:



Advertisements
Similar presentations
IMPLEMENTING CLASSES Chapter 3. Black Box  Something that magically does its thing!  You know what it does but not how.  You really don’t care how.
Advertisements

1 Classes Object-oriented programming: Model the problem as a collection of objects that have certain attributes and interact with one another and/or the.
1 Using Classes and Working With Class Interfaces November 20, 2002 CSE103 - Penn State University Prepared by Doug Hogan.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Chapter Three - Implementing Classes.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Classes and Objects Systems Programming.
1 Chapter 11 Structured Types, Data Abstraction and Classes Dale/Weems/Headington.
More on Objects CS 102 More, more, more on Objects.
1 Lab Session-XIV CSIT121 Spring 2002 b Namespaces b First Class Travel b Lab Exercise 14 (Demo) b Lab Exercise b Practice Problem.
1 Lecture 29 Chapter 11 Structured Types, Data Abstraction and Classes Dale/Weems/Headington.
Chapter Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
Chapter 11: Classes and Data Abstraction
Writing Classes (Chapter 4)
Chapter 11: Classes and Data Abstraction. C++ Programming: Program Design Including Data Structures, Fourth Edition2 Objectives In this chapter, you will:
CHAPTER 13 CLASSES AND DATA ABSTRACTION. In this chapter, you will:  Learn about classes  Learn about private, protected, and public members of a class.
C++ Lecture 4 Tuesday, 15 July Struct & Classes l Structure in C++ l Classes and data abstraction l Class scope l Constructors and destructors l.
1 Warm-Up Problem Just like with primitive data types (int, double, etc.), we can create arrays of objects. ex: bankAccount employees[100]; Problem: It’s.
Classes: Member Functions and Implementation November 22, 2002 CSE103 - Penn State University Prepared by Doug Hogan.
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.
C++ Classes and Data Structures Jeffrey S. Childs
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:
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.
1 Strings, Classes, and Working With Class Interfaces CMPSC 122 Penn State University Prepared by Doug Hogan.
2 Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
CIS 270—Application Development II Chapter 8—Classes and Objects: A Deeper Look.
Classes and Data Abstraction Andrew Davison Noppadon Kamolvilassatian Department of Computer Engineering Prince of Songkla University 1
Classes and Objects CS177 Rec 10. Announcements Project 4 is posted ◦ Milestone due on Nov. 12. ◦ Final submission due on Nov. 19. Exam 2 on Nov. 4 ◦
Chapter 10: Classes and Data Abstraction. Objectives In this chapter, you will: Learn about classes Learn about private, protected, and public members.
EGR 2261 Unit 11 Classes and Data Abstraction  Read Malik, Chapter 10.  Homework #11 and Lab #11 due next week.  Quiz next week.
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?
1 2/21/05CS250 Introduction to Computer Science II Destructors, Get and Set, and Default Memberwise Assignment.
 2008 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
1 Classes II Chapter 7 2 Introduction Continued study of –classes –data abstraction Prepare for operator overloading in next chapter Work with strings.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 12: Classes and Data Abstraction.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 05: Classes and Data Abstraction.
Department of Computer Science and Engineering, HKUST 1 HKUST Summer Programming Course 2008 Using Member Functions and Data Members.
Chapter 10: Classes and Data Abstraction. Classes Object-oriented design (OOD): a problem solving methodology Objects: components of a solution Class:
Chapter 1 C++ Basics Review (Section 1.4). Classes Defines the organization of a data user-defined type. Members can be  Data  Functions/Methods Information.
Monday, Jan 27, 2003Kate Gregory with material from Deitel and Deitel Week 4 Questions from Last Week Hand in Lab 2 Classes.
1 Data Structures CSCI 132, Spring 2014 Lecture 2 Classes and Abstract Data Types Read Ch Read Style Guide (see course webpage)
Programming Fundamentals1 Chapter 7 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.
CSIS 123A Lecture 1 Intro To Classes Glenn Stevenson CSIS 113A MSJC.
1 Chapter 12 Classes and Abstraction. 2 Chapter 12 Topics Meaning of an Abstract Data Type Declaring and Using a class Data Type Using Separate Specification.
CompSci 230 S Programming Techniques
Chapter 12 Classes and Abstraction
EGR 2261 Unit 13 Classes Read Malik, Chapter 10.
Structures and Classes
Chapter 6 CS 3370 – C++ Functions.
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?
Topic 7 Introduction to Classes and Objects
Classes Object-oriented programming: Example: Bank transactions
Implementing Classes Yonglei Tao.
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 Structured Types, Data Abstraction and Classes
CS 1430: Programming in C++.
User-Defined Classes and ADTs
Chapter Three - Implementing Classes
More on Classes Classes may have constructors which are called when objects are created and destructors which are called when objects are destroyed. Classes.
د.سناء الصايغ الفصل الأول البرمجة الشيئية
Classes and Data Abstraction
Chapter 6 Class Definitions and Member Functions
Chapter 8: User-Defined Classes and ADTs
Introduction to Classes and Objects
Recap Week 2 and 3.
CMSC202 Computer Science II for Majors Lecture 07 – Classes and Objects (Continued) Dr. Katherine Gibson Based on slides by Chris Marron at UMBC.
Class rational part2.
Classes and Objects Systems Programming.
(4 – 2) Introduction to Classes in C++
Introduction to Classes and Objects
Presentation transcript:

Classes: Implementation and Testing Edited for CMPSC 122 Penn State University Prepared by Doug Hogan

Preview of Today… Implementation Client end -- Test drivers Tips for implementing each kind of function More Examples Client end -- Test drivers

Review Questions on the blue worksheet?

Implementing Functions Remember Implementation outside the class interface Name of the class and scope resolution operator (::) before the FUNCTION NAME

Implementing Constructors Do whatever is necessary to set up the class Initialize each private member Default constructors: default values Initializer constructors: some from input parameters validate? set others to default values

Exercise Implement the default constructor for tvShow. tvShow::tvShow() { name = “New show”; channel = 2; startHour = 0; startMin = 0; }

Implementing initializer constructors Most specify SOME, not ALL, initial values Set the others as in the default constructor

Initializer constructor example tvShow::tvShow(string initName, int initChannel) { name = initName; // initialized from channel = initChannel; // parameters // we still need to handle two data members! startHour = 0; // initialized to startMin = 0; // defaults } tvShow object constructed with this method name channel startHour startMin d from init- Channel value of initName

Implementing modifiers Ultimately: must have assignment statements (or function calls) that change private data

Modifier Example void tvShow::reschedule(int hoursLater, int minutesLater) // PRE: hoursLater >= 0, 0 <= minutesLater <= 59 // POST: this tvShow now starts hoursLater hours and // minutesLater minutes after it did before { startHour = ((startMin + minutesLater)/60 + startHour + hoursLater)%24; // add on hour from minutes rolling over // add on hours, correct for day rolling over startMin = (startMin + minutesLater)%60; // add on time, correct for new hour }

Implementing Accessors “Get” accessors return a private variable ex: int tvShow::getChannel() const { return channel; } Don’t forget the const if it’s in the prototype!

Accessors That Print vs. Accessors That Return int tvShow::getChannel() const // POST: Method returns this show’s channel { return channel; } void tvShow::printChannel() const // POST: Method displays this show’s channel { cout << "Channel: " << channel; } Always provide the first type. The second type can be useful, but from a user interface design perspective, really ought to be avoided. A general principle is that you should separate logic from user interface at all times.

An Accessor Exercise Write an accessor that prints the time of a tvShow in the format “8:30 a.m.”

An Accessor Exercise void tvShow::printTimeAMPM() const { // POST: a string is displayed with this // show's start time in "h:mm a.m." form { int hr; // hour in format 1..12 hr = startHour%12; // get hour in 0..12 range if(hr == 0) // correct for hour 0 == 12 hr = 12; if(min <= 9) // need leading zero cout << hr << ":0" << min; else // no leading zero cout << hr << ":" << min; if(startHour < 12) // check for a.m. cout << " a.m."; else // otherwise it's p.m. cout << " p.m."; } Question: Can any of the if statements be optimized?

Test Drivers Main program that checks whether the class is working properly Create a few objects Call each of the member functions Check the results Good practice: test drive classes before writing programs with them Find the errors WITHIN THE CLASS vs. outside the class

Example Test Driver int main() { bankAccount acct1; // def. constructor bankAccount acct2("Homer", 100); // one init constr bankAccount acct3("Lisa"); // another init con acct1.resetAcct("Marge", 75); // test resetAcct cout << acct1.getName(); // test getName // did resetAcct work? cout << acct1.getBalance(); // test getBalance acct1.deposit(50); // deposit cout << acct1.getBalance(); // did deposit work? acct1.withdraw(100); // withdraw cout << acct1.getBalance(); // did withdraw work? // additional calls, use accessors to see that other // constructors worked } Note: Comments in this test driver are for you. I'll never require test driver comments.

Summary Implementation Test drive classes before using them Scope resolution operator and class name Constructors – initialize each private member Modifiers – change private members Accessors – remember const, printing vs. returning Test drive classes before using them Lab time now: Your turn to make your own class!