Classes: Member Functions and Implementation November 22, 2002 CSE103 - Penn State University Prepared by Doug Hogan.

Slides:



Advertisements
Similar presentations
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
Advertisements

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.
Chapter 3 – Implementing Classes. Chapter Goals To become familiar with the process of implementing classes To be able to implement simple methods To.
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.
Chapter 3 Implementing Classes. Instance Variables Instance variables store the data of an object; the fields of an object. Instance of a class: an object.
1 Using Classes and Working With Class Interfaces November 20, 2002 CSE103 - Penn State University Prepared by Doug Hogan.
Classes. What is a class? Data and the functions (methods) that operate on that data – collectively called members –Example: bank account class Provide.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Chapter Three - Implementing Classes.
Classes. Object-Oriented Design Method for designing computer programs Consider “objects” interacting in the program –Example: a zoo, a gradebook.
Classes. What is a class? Data and the functions (methods) that operate on that data – collectively called members –Example: bank account class Provide.
1 Classes Separating interface from implementation Place the class declaration in a header file (.h) to be included (via #include) in each file that uses.
Chapter 9: Classes with Instance Variables or Classes=Methods+Variables Asserting Java © Rick Mercer.
Chapter Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
Introduction to Classes and Objects CS-2303, C-Term Introduction to Classes and Objects CS-2303 System Programming Concepts (Slides include materials.
Classes in C++ Bryce Boe 2012/08/15 CS32, Summer 2012 B.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to Classes and Objects Outline Introduction Classes, Objects, Member Functions and Data.
Writing Classes (Chapter 4)
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 2.
Classes CS 21a: Introduction to Computing I First Semester,
ACO 101: Introduction to Computer Science Anatomy Part 2: Methods.
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 13: Introduction to Classes.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 7 Structured Data and Classes.
Chapter 10 Introduction to Classes
Copyright © 2012 Pearson Education, Inc. Chapter 9 Classes and Multiform Projects.
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 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.
Classes Representing Non-Trivial Objects. Problem Write a program that reads a temperature (either Fahrenheit or Celsius), and displays that same temperature.
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.
Chapter 10 Defining Classes. The Internal Structure of Classes and Objects Object – collection of data and operations, in which the data can be accessed.
1 Announcements Note from admins: Edit.cshrc.solaris instead of.tcshrc Note from admins: Do not use delta.ece.
CSE 501N Fall ‘09 04: Introduction to Objects 08 September 2009 Nick Leidenfrost.
Classes. Student class We are tasked with creating a class for objects that store data about students. We first want to consider what is needed for the.
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.
Chapter 10: Classes and Data Abstraction. Objectives In this chapter, you will: Learn about classes Learn about private, protected, and public members.
CSci 162 Lecture 10 Martin van Bommel. Procedures vs Objects Procedural Programming –Centered on the procedures or actions that take place in a program.
C++ Class. © 2005 Pearson Addison-Wesley. All rights reserved 3-2 Abstract Data Types Figure 3.1 Isolated tasks: the implementation of task T does not.
Computing and Statistical Data Analysis Lecture 6 Glen Cowan RHUL Physics Computing and Statistical Data Analysis Introduction to classes and objects:
Intro to Classes via the C++ String Class November 18, 2002 CSE103 - Penn State University Online at
93 4/11/98 CSE 143 Class Constructors [Sections ]
Chapter 10: Classes and Data Abstraction. Classes Object-oriented design (OOD): a problem solving methodology Objects: components of a solution Class:
Object Oriented Programming and Data Abstraction Rowan University Earl Huff.
72 4/11/98 CSE 143 Abstract Data Types [Sections , ]
AP Java Ch. 4 Review Question 1  Java methods can return only primitive types (int, double, boolean, etc).
Monday, Jan 27, 2003Kate Gregory with material from Deitel and Deitel Week 4 Questions from Last Week Hand in Lab 2 Classes.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 13: Introduction to Classes.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Chapter Three - Implementing Classes.
Chapter 3 Implementing Classes
 An object is basically anything in the world that can be created and manipulated by a program.  Examples: dog, school, person, password, bank account,
Defining Data Types in C++ Part 2: classes. Quick review of OOP Object: combination of: –data structures (describe object attributes) –functions (describe.
Object-Oriented Programming Using C++ Third Edition Chapter 7 Using Classes.
Programming Abstractions
Procedural and Object-Oriented Programming
Classes C++ representation of an object
Classes Object-oriented programming: Example: Bank transactions
Implementing Classes Yonglei Tao.
Introduction to Classes
Introduction to Classes
Classes and Data Abstraction
Classes: Implementation and Testing
Chapter 6 Class Definitions and Member Functions
JAVA CLASSES.
Classes CS 21a: Introduction to Computing I
Classes C++ representation of an object
Chapter 9 Introduction To Classes
Introduction to Classes and Objects
Presentation transcript:

Classes: Member Functions and Implementation November 22, 2002 CSE103 - Penn State University Prepared by Doug Hogan

2 Announcements Homework 5 is due Monday. Correction to lab 12 handout: Surface area of a cylinder is 2  r  rh. No class next Wednesday.

3 Overview Review Implementation Class interface and Functions Review of the form Constructors Modifiers Accessors

4 Review: Library Book Problem Your ideas? member data? member functions? We’ll work with this set of member data: string author; string title; int year; string isbn;

5 Review Implementation of class member functions is similar to nonmember functions Function headers vs. function prototypes The main difference: Member functions need to know what class they belong to (their scope) Scope resolution operator ( :: )

6 Scope Resolution The class name and the scope resolution operator ( :: ) go directly before the function name e.g. void bankAccount::withdraw(int amount) NOT bankAccount::void withdraw(int amount)

7 Implementation Example Implementation of the bankAccount constructor: bankAccount::bankAccount() // POST: default bankAccount object constructed with // name == “?” and balance == 0 { name = “?”; balance = 0; } private member data scope resolution name balance default_acct 0 “?”

8 More Implementation Examples void bankAccount::deposit(int amount) // PRE: amount in dollars and amount > $0 // POST: amount had been added on to balance { balance = balance + amount; } Exercise: Implement withdraw. void bankAccount::withdraw(int amount) // PRE: amount in dollars and amount > $0 // POST: amount had been subtracted from the balance { balance = balance - amount; }

9 Class Interface Defines the WHAT, not the HOW General form: class className { public: // member function declarations private: // member data declarations };

10 Review What are the three categories of member functions? constructors create objects (allocate memory, set initial state ) modifiers change the state of objects accessors make information about the state of the object available outside the class

11 Continuing Bank Account… Last time I asked you to… Think of one more constructor, modifier, and accessor that would be good additions to the bankAccount class. Your ideas??

12 Constructors Goal: construct objects of the class allocate memory Four important observations… name return type overloading calling

13 1. Constructors: Names Constructors MUST have the same name as the class itself. That's how you'll make instances of the class (objects). Example: bankAccount class constructor: bankAccount(); rectangle class constructor: rectangle();

14 2. Constructors – Return Type They don't have a return type. It's simply omitted. Ex: bankAccount(); NOT void NOT double, int, etc.

15 3. Constructors - Overloading Constructors can be overloaded Can have several constructors same name different lists of parameters This ability allows you to create a default constructor no parameters initializer constructors parameters specifying initial state of the class

16 3. Constructors - Overloading Example default constructor: bankAccount(); // POST: A default bankAccount object is // created with the name set to a blank and // and the balance set to $0.00 Example initializer constructors: bankAccount(string initName, double initBalance); // PRE: initName has been assigned a value // && initBalance >= 0.00 and is in dollars // POST: A bankAccount object is created with the // name set to initName // and the balance set to initBalance bankAccount(string initName); bankAccount(double initBalance);

17 4. Constructors – Calling (Client) Not called directly, i.e. no dot notation Default constructor call: bankAccount myAcct; no parentheses Initializer constructor call: bankAccount myAcct(“Homer”, $100);

18 Problem Given the libraryBook class… string author; string title; int year; string isbn; Define a default constructor. Define two initializer constructors.

19 Problem Define a default constructor. libraryBook(); Define two initializer constructors. libraryBook(string initTitle, string initAuthor, string initISBN, int initYear); libraryBook(string initTitle);

20 Another exercise… Given the constructors we defined libraryBook(); libraryBook(string initTitle, string initAuthor, string initISBN, int initYear); libraryBook(string initTitle); Construct a default libraryBook object. libraryBook book1; Construct a libraryBook object with the initial title Starting Out With C++. libraryBook book2(“Starting Out With C++”);

21 Modifiers The functions that do most of the work. Note: Objects have three characteristics: name state set of operations Modifiers define the set of operations.

22 Modifiers Allow the client to make changes to the private variables. Declarations look like the ones for nonmember functions. Often, but not always, they have a void return type. “Set” functions Modifiers that just "set" the value of a private variable from a parameter without doing any calculations

23 Modifiers - Examples void withdraw(double amount); // PRE: amount >= 0.00 and is in dollars // POST: amount is deducted from the // balance stored for the account A set function: void resetAccount(string newName, double newBalance); // PRE: newName has been assigned a value // && newBalance >= 0.00 and is in dollars // POST: The account object is reset with the // name set to newName and the balance // set to newBalance

24 Accessors Allow the client to see what values the private variables have. Don't allow the client to make changes. Return type is that of the variable being "accessed.“ “Get” functions Accessors that just “get" the value of a private variable from a parameter without doing any calculations

25 Accessors Should be declared const They don't change the state of any variables. Forces the issue of not making changes Example: double getBalance() const; // POST: FCTVAL == current balance // of this account

26 More complicated accessors Some calculation based on the data as long as it doesn’t change the member data e.g. balance after interest w/o actually crediting it A data member converted to different units e.g. Fahrenheit version of Celsius temp. Part of a data member e.g. the cents part of a dollar amount

27 Exercise Declare an accessor for the libraryBook type. string getTitle() const; Write the function header for the accessor. string libraryBook::getTitle() const

28 Pre- and Postconditions A few new things to note Preconditions What must be true for the method to behave as intended Anything about the state of the object? Should another method have been called first? May need to look at private data members individually

29 Pre- and Postconditions Postconditions What is the state of the object after this method has been called? What is returned or displayed? What private data members have changed? How?

30 Summary Implementation Scope resolution operator (::) and class name directly before function name Remove semicolons Interface & functions Constructors – create instances, allocate memory same name as class no return type can have multiple with same name not called with dot notation Modifiers – change state of private variables, define operations Accessors – allows client to see state of private variables Pre- and postconditions

31 Preview of What’s Next… Implementation Tips for implementing each kind of function Client end More on working with objects Test drivers More Examples Odds and Ends from Chapter 13 For next time: work on the blue worksheet