1 warm-up problem just like with primitive data types (int, double, etc.), we can create arrays of...
TRANSCRIPT
![Page 1: 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](https://reader036.vdocuments.site/reader036/viewer/2022081603/56649f155503460f94c2ae9e/html5/thumbnails/1.jpg)
1
Warm-Up ProblemWarm-Up Problem
Just like with primitive data types (int, double, etc.), we can create arrays of objects.
ex: bankAccount employees[100];Problem: It’s payday. Put $500 in
each employee’s account.for(int i = 0; i < 99; i++){ employees[i].deposit(500);}
(i < 99 should be i <= 99 or i < 100, but I'm leaving that be as a place for an alertness point in the review of this problem in future lectures. )
![Page 2: 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](https://reader036.vdocuments.site/reader036/viewer/2022081603/56649f155503460f94c2ae9e/html5/thumbnails/2.jpg)
Classes:Classes: Member Functions and Member Functions and
Starting ImplementationStarting Implementation
Edited for CMPSC 122Penn State University
Prepared by Doug Hogan
![Page 3: 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](https://reader036.vdocuments.site/reader036/viewer/2022081603/56649f155503460f94c2ae9e/html5/thumbnails/3.jpg)
3
OverviewOverview
Review and Follow UpPreview of ImplementationClassification of Class
FunctionsClass interface and Functions
Review of the formConstructorsModifiersAccessors
![Page 4: 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](https://reader036.vdocuments.site/reader036/viewer/2022081603/56649f155503460f94c2ae9e/html5/thumbnails/4.jpg)
4
Review of StringsReview of Strings Last time, we finished with a problem:
string returnedExpression(string inputString) // PRE: inputString is a line of valid C++ code
// (<=80 chars) containing the "return"// keyword and ending with a semicolon
// POST: FCTVAL == the expression that follows // the return keyword, not including the// semicolon
Recall the string member functions: stringObject.length() stringObject.find(string) stringObject.substr(startLocation, length)
How do we use these functions to construct a solution?
![Page 5: 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](https://reader036.vdocuments.site/reader036/viewer/2022081603/56649f155503460f94c2ae9e/html5/thumbnails/5.jpg)
5
SolutionSolution string returnedExpression(string inputString)
// PRE: inputString is a line of valid C++ code (<=80 chars) containing // the "return" keyword and ending with a semicolon
// POST: FCTVAL == the expression that follows the return keyword, // not including the semicolon
First, find where "return" is located int returnLoc = inputString.find("return");
Next, find how long the string is int exprLength = inputString.length();
Find how long the expression is: exprLength -= 1; // don’t want semicolon exprLength -= returnLoc; // or stuff before "return" exprLength -= 7; // or "return "
![Page 6: 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](https://reader036.vdocuments.site/reader036/viewer/2022081603/56649f155503460f94c2ae9e/html5/thumbnails/6.jpg)
6
SolutionSolution string returnedExpression(string inputString)
// PRE: inputString is a line of valid C++ code (<=80 chars) containing // the "return" keyword and ending with a semicolon
// POST: FCTVAL == the expression that follows the return keyword, // not including the semicolon
Finally, construct and return the expression as a substring:return inputString.substr(returnLoc+7, exprLength);
![Page 7: 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](https://reader036.vdocuments.site/reader036/viewer/2022081603/56649f155503460f94c2ae9e/html5/thumbnails/7.jpg)
7
Object Oriented TermsObject Oriented Terms
Class Object Member Encapsulation Information Hiding Message Abstraction
![Page 8: 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](https://reader036.vdocuments.site/reader036/viewer/2022081603/56649f155503460f94c2ae9e/html5/thumbnails/8.jpg)
8
Review: Library Book Review: Library Book ProblemProblem
Your ideas?member data?member functions?
We’ll work with this set of member data:string author;string title;int year; string isbn;
![Page 9: 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](https://reader036.vdocuments.site/reader036/viewer/2022081603/56649f155503460f94c2ae9e/html5/thumbnails/9.jpg)
9
A Preview of A Preview of ImplementationImplementation
Implementation of class member functions is similar to implementing nonmember functionsFunction headers vs. function prototypes
The main difference: Member functions need to know what
class they belong to (their scope)Scope resolution operator (::)
![Page 10: 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](https://reader036.vdocuments.site/reader036/viewer/2022081603/56649f155503460f94c2ae9e/html5/thumbnails/10.jpg)
10
Scope ResolutionScope Resolution
The class name and the scope resolution operator (::) go directly before the function namee.g. void bankAccount::withdraw(int amount)
NOT bankAccount::void withdraw(int amount)
![Page 11: 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](https://reader036.vdocuments.site/reader036/viewer/2022081603/56649f155503460f94c2ae9e/html5/thumbnails/11.jpg)
11
Implementation ExampleImplementation 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
“?”
![Page 12: 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](https://reader036.vdocuments.site/reader036/viewer/2022081603/56649f155503460f94c2ae9e/html5/thumbnails/12.jpg)
12
More Implementation More Implementation ExamplesExamples
void bankAccount::deposit(int amount)// PRE: amount in dollars and amount > $0// POST: amount has been added on to balance{ balance = balance + amount;}
Problem: Implement withdraw. void bankAccount::withdraw(int amount)
// PRE: amount in dollars and amount > $0// POST: amount has been subtracted from balance{ balance = balance - amount;}
![Page 13: 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](https://reader036.vdocuments.site/reader036/viewer/2022081603/56649f155503460f94c2ae9e/html5/thumbnails/13.jpg)
13
Class InterfaceClass Interface Defines the WHAT, not the HOW
General form: class className{ public: // member function declarations
private: // member data declarations};
![Page 14: 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](https://reader036.vdocuments.site/reader036/viewer/2022081603/56649f155503460f94c2ae9e/html5/thumbnails/14.jpg)
14
Three Types of FunctionsThree Types of Functions
Class member functions are classified into three categories:constructors
create objects (allocate memory, set initial state)
modifierschange the state of objects
accessorsmake information about the state of the
object available outside the class
![Page 15: 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](https://reader036.vdocuments.site/reader036/viewer/2022081603/56649f155503460f94c2ae9e/html5/thumbnails/15.jpg)
15
ExerciseExercise
Classify the functions of bankAccountconstructors
bankAccount();modifiers
void withdraw(int amount);void deposit(int amount);
accessorsdouble getBalance();
![Page 16: 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](https://reader036.vdocuments.site/reader036/viewer/2022081603/56649f155503460f94c2ae9e/html5/thumbnails/16.jpg)
16
Continuing Bank Account…Continuing Bank Account…
Work with a partner to think of one more constructor, modifier, and accessor that would be good additions to the bankAccount class.
Your ideas??
![Page 17: 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](https://reader036.vdocuments.site/reader036/viewer/2022081603/56649f155503460f94c2ae9e/html5/thumbnails/17.jpg)
17
Onward…Onward…
Now we'll look at each of the three kinds of member functions in more detail.
![Page 18: 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](https://reader036.vdocuments.site/reader036/viewer/2022081603/56649f155503460f94c2ae9e/html5/thumbnails/18.jpg)
18
ConstructorsConstructors
Goal:construct objects of the classallocate memory
Four important observations…namereturn typeoverloadingcalling
![Page 19: 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](https://reader036.vdocuments.site/reader036/viewer/2022081603/56649f155503460f94c2ae9e/html5/thumbnails/19.jpg)
19
1. Constructors: Names1. 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();
![Page 20: 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](https://reader036.vdocuments.site/reader036/viewer/2022081603/56649f155503460f94c2ae9e/html5/thumbnails/20.jpg)
20
2. Constructors – Return 2. Constructors – Return TypeType
They don't have a return type. It's simply omitted.
Ex: bankAccount(); NOT voidNOT double, int, etc.
![Page 21: 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](https://reader036.vdocuments.site/reader036/viewer/2022081603/56649f155503460f94c2ae9e/html5/thumbnails/21.jpg)
21
3. Constructors - 3. Constructors - OverloadingOverloading
Constructors can be overloadedCan have several constructors
same namedifferent lists of parameters
This ability allows you to create a default constructor
no parametersinitializer constructors
parameters specifying initial state of an object
![Page 22: 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](https://reader036.vdocuments.site/reader036/viewer/2022081603/56649f155503460f94c2ae9e/html5/thumbnails/22.jpg)
22
3. Constructors - 3. Constructors - OverloadingOverloading
Example default constructor: bankAccount();
// POST: A default bankAccount object is // created with name set to a blank and // and balance set to $0.00
Example initializer constructors: bankAccount(string initName, double initBalance); // PRE: initName has been assigned a value // && initBalance >= 0.00 and initBalance is in // dollars // POST: A bankAccount object is created with // name set to initName// and balance set to initBalance
bankAccount(string initName); bankAccount(double initBalance);
![Page 23: 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](https://reader036.vdocuments.site/reader036/viewer/2022081603/56649f155503460f94c2ae9e/html5/thumbnails/23.jpg)
23
4. Constructors – Calling 4. Constructors – Calling (Client)(Client)
Not called directly, i.e. no dot notation
Default constructor call:bankAccount myAcct; no parentheses
Initializer constructor call:bankAccount myAcct("Homer", 100);
![Page 24: 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](https://reader036.vdocuments.site/reader036/viewer/2022081603/56649f155503460f94c2ae9e/html5/thumbnails/24.jpg)
24
ProblemProblem
Given the libraryBook class… string author;string title;int year; string isbn;
Define a default constructor.Define two initializer constructors.
![Page 25: 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](https://reader036.vdocuments.site/reader036/viewer/2022081603/56649f155503460f94c2ae9e/html5/thumbnails/25.jpg)
25
ProblemProblem
Define a default constructor.libraryBook();
Define two initializer constructors. libraryBook(string initTitle, string initAuthor, string initISBN, int initYear);
libraryBook(string initTitle);
![Page 26: 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](https://reader036.vdocuments.site/reader036/viewer/2022081603/56649f155503460f94c2ae9e/html5/thumbnails/26.jpg)
26
Another exercise…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 Algorithms Unlocked. libraryBook book2("Algorithms Unlocked");
![Page 27: 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](https://reader036.vdocuments.site/reader036/viewer/2022081603/56649f155503460f94c2ae9e/html5/thumbnails/27.jpg)
27
ModifiersModifiers
The functions that do most of the work.
Note: Objects have three characteristics: namestateset of operations
Modifiers define the set of operations.
![Page 28: 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](https://reader036.vdocuments.site/reader036/viewer/2022081603/56649f155503460f94c2ae9e/html5/thumbnails/28.jpg)
28
ModifiersModifiers 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 or setters Modifiers that just "set" the value of a private
variable from a parameter without doing any calculations
![Page 29: 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](https://reader036.vdocuments.site/reader036/viewer/2022081603/56649f155503460f94c2ae9e/html5/thumbnails/29.jpg)
29
Modifiers - ExamplesModifiers - Examples void withdraw(double amount); // PRE: amount >= 0.00 and amount // is in dollars // POST: amount is deducted from // balance
A set function: void resetAccount(string newName, double newBalance); // PRE: newName has been assigned a value // && newBalance >= 0.00 and newBalance is // in dollars // POST: This account object is reset with // name set to newName and balance// set to newBalance
![Page 30: 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](https://reader036.vdocuments.site/reader036/viewer/2022081603/56649f155503460f94c2ae9e/html5/thumbnails/30.jpg)
30
AccessorsAccessors 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 or getters Accessors that just "get" the value of a private
variable without doing any calculations
![Page 31: 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](https://reader036.vdocuments.site/reader036/viewer/2022081603/56649f155503460f94c2ae9e/html5/thumbnails/31.jpg)
31
AccessorsAccessors
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 // in dollars
![Page 32: 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](https://reader036.vdocuments.site/reader036/viewer/2022081603/56649f155503460f94c2ae9e/html5/thumbnails/32.jpg)
32
More complicated accessorsMore 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
![Page 33: 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](https://reader036.vdocuments.site/reader036/viewer/2022081603/56649f155503460f94c2ae9e/html5/thumbnails/33.jpg)
33
ExerciseExercise
Declare an accessor for the libraryBook type.string getTitle() const;
Write the function header for the accessor. string libraryBook::getTitle() const
![Page 34: 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](https://reader036.vdocuments.site/reader036/viewer/2022081603/56649f155503460f94c2ae9e/html5/thumbnails/34.jpg)
34
Pre- and PostconditionsPre- and Postconditions
A few new things to notePreconditions
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
![Page 35: 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](https://reader036.vdocuments.site/reader036/viewer/2022081603/56649f155503460f94c2ae9e/html5/thumbnails/35.jpg)
35
Pre- and PostconditionsPre- and Postconditions
PostconditionsWhat is the state of the object after this
method has been called? What is returned or displayed?
What private data members have changed? How?
![Page 36: 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](https://reader036.vdocuments.site/reader036/viewer/2022081603/56649f155503460f94c2ae9e/html5/thumbnails/36.jpg)
36
SummarySummary 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
![Page 37: 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](https://reader036.vdocuments.site/reader036/viewer/2022081603/56649f155503460f94c2ae9e/html5/thumbnails/37.jpg)
37
Preview of What’s to Preview of What’s to Come…Come…
Implementation Tips for implementing each kind of function
Client end More on working with objects Test drivers
More Examples Advanced Issues
For next time: work on the blue worksheet and a short lab exercise