Download presentation
Presentation is loading. Please wait.
1
Slides by Evan Gallagher
Chapter Nine: Classes C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved Slides by Evan Gallagher
2
To understand the concept of encapsulation
Chapter Goals To understand the concept of encapsulation To master the separation of interface and implementation To be able to implement your own classes To understand how constructors and member functions act on objects To discover appropriate classes for solving programming problems To distribute a program over multiple source files C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
3
Object-Oriented Programming
I thought you considered me more than just a collection of parts. I’m more than just functional. Am I just an object to you? C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
4
Object-Oriented Programming
I have an onboard computer – now will you love me for what I am? C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
5
Object-Oriented Programming
Did you know that you already are an Object Oriented Programmer? (No way!) C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
6
Object-Oriented Programming
Does string sound familiar? (Yes…) C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
7
Object-Oriented Programming
Does string sound familiar? How about cin and cout? (Yes, but...) C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
8
Object-Oriented Programming
An Object Oriented Programmer uses objects. (Wow, I didn’t realize…) C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
9
Object-Oriented Programming
But… a REAL Object Oriented Programmer designs and creates objects and then uses them. (Back to square 1 for me) (At least it’s not square 0 – that would be the very start.) C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
10
Object-Oriented Programming
Yes, you are mostly A Programmer Who Writes Functions To Solve Sub-problems And that is very good! C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
11
Object-Oriented Programming
As programs get larger, it becomes increasingly difficult to maintain a large collection of functions. It often becomes necessary to use the dreaded and deadly practice of USING GLOBAL VARAIBLES (Don’t do it, son!) C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
12
Object-Oriented Programming
Global variables are those defined outside of all functions – so all functions have access to them. But… C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
13
Object-Oriented Programming
When some part of the global data needs to be changed: to improve performance or to add new capabilities, a large number of functions may be affected – you will have to rewrite them – and hope everything still works! C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
14
Object-Oriented Programming
When some part of the global data needs to be changed: to improve performance or to add new capabilities, a large number of functions may be affected – you will have to rewrite them – and hope everything still works! Ouch! C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
15
No more global variables – Hurray!
Objects to the Rescue Computer scientists noticed that most often functions were working on related data so they invented: Objects where they keep the data and the functions that work with them together. No more global variables – Hurray! C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
16
Object Oriented Programming (OOP)
Objects to the Rescue objects Object Oriented Programming (OOP) (Not to be confused with oops!, the exclamation.) C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
17
The data stored in an object are called: data members
Objects to the Rescue Some new terminology. The data stored in an object are called: data members The functions that work on data members are: member functions No more variables and functions – separately. C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
18
Figure out which functions go with which data.
Objects to the Rescue Figure out which functions go with which data. C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
19
Create an object for each set of data.
Objects to the Rescue Create an object for each set of data. C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
20
Create another object for another set.
Objects to the Rescue Create another object for another set. C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
21
Objects to the Rescue And now, a third object.
C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
22
Objects to the Rescue C++ for Everyone by Cay Horstmann
Copyright © 2012 by John Wiley & Sons. All rights reserved
23
Get rid of those global variables.
Objects to the Rescue Get rid of those global variables. C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
24
From now on, we’ll have only objects.
Objects to the Rescue From now on, we’ll have only objects. C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
25
Objects to the Rescue Ah. C++ for Everyone by Cay Horstmann
Copyright © 2012 by John Wiley & Sons. All rights reserved
26
Encapsulation The data members are encapsulated
They are hidden from other parts of the program and accessible only through their own member functions. C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
27
Now when we want to change the way that an object is implemented,
Encapsulation Now when we want to change the way that an object is implemented, only a small number of functions need to be changed, and they are the ones in the object. C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
28
Because most real-world programs need
Encapsulation Because most real-world programs need to be updated often during their lifetime, this is an important advantage of object-oriented programming. Program evolution becomes much more manageable. (Ah …) C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
29
When you use string or stream objects,
Encapsulation When you use string or stream objects, you did not know their data members. Encapsulation means that they are hidden from you. (That’s good – you might have messed them up.) C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
30
Encapsulation and the Interface
But you were allowed to call member functions such as substr, and you could use operators such as [] or >> (which are actually functions). You were given an interface to the object. C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
31
Encapsulation and the Interface
All those member functions and operators are the interface to the object. C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
32
Encapsulation and the Interface
I wonder how the engine really works, and the speedometer, and the gas gauge, and that little thingy over there… C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
33
Encapsulation and the Interface
And I better stop thinking about all this or I won’t be able to drive! C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
34
Object-Oriented Programming
So you like my interface. Don’t get me started… C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
35
In C++, a programmer doesn’t implement a single object.
Classes In C++, a programmer doesn’t implement a single object. Instead, the programmer implements a class. C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
36
A class describes a set of objects with the same behavior.
Classes A class describes a set of objects with the same behavior. You would create the Car class to represent cars as objects. C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
37
Object-Oriented Programming
An object ?! C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
38
you must specify the behavior
Defining Classes To define a class, you must specify the behavior by providing implementations for the member functions, and by defining the data members for the objects … C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
39
Defining Classes Oops! (the exclamation), I’m a little early – sorry.
I, camel, will be back later. C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
40
Again, to define a class:
Classes Again, to define a class: Implement the member functions to specify the behavior. Define the data members to hold the object’s data. C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
41
We will design a cash register object.
Designing the Class We will design a cash register object. C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
42
• Clear the cash register to start a new sale.
Designing the Class By observing a real cashier working, we realize our cash register design needs member functions to do the following: • Clear the cash register to start a new sale. • Add the price of an item. • Get the total amount owed and the count of items purchased. C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
43
These activities will be our public interface.
Classes These activities will be our public interface. The public interface is specified by declarations in the class definition. The data members are defined there also. C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
44
To define a class you write:
Classes To define a class you write: class NameOfClass { public: // the public interface private: // the data members }; C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
45
// the public interface private: // the data members };
Classes Any part of the program should be able to call the member functions – so they are in the public section. Data members are defined in the private section of the class. Only member functions of the class can access them. They are hidden from the rest of the program. class NameOfClass { public: // the public interface private: // the data members }; C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
46
Here is the C++ syntax for the CashRegister class definition:
Classes Here is the C++ syntax for the CashRegister class definition: class CashRegister { public: void clear(); void add_item(double price); double get_total() const; int get_count() const; private: // data members will go here }; C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
47
void add_item(double price); double get_total() const;
Classes The public interface has the three activities that we decided this object should support. class CashRegister { public: void clear(); void add_item(double price); double get_total() const; int get_count() const; private: // data members will go here }; C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
48
Notice that these are declarations. They will be defined later.
Classes Notice that these are declarations. They will be defined later. class CashRegister { public: void clear(); void add_item(double price); double get_total() const; int get_count() const; private: // data members will go here }; C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
49
Defining Classes Hi. I’m back. I’m here for style purposes.
C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
50
Defining Classes Remember, earlier, when I said, “I, camel, will be back later.” That was to help you with the style for class names: CAMEL BACK – well, actually it’s CAMEL CASE. C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
51
Defining Classes I personally think CAMEL BACK is
more stylish than CAMEL CASE. And I consider my style to be immaculate. Just look… at me of course: C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
52
Defining Classes Look at my head and my humps. (Very cute!)
That’s how your class names should look: Each “word” should start with an uppercase letter. (Very good style!) C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
53
What should you choose for the name of the class to represent me?
Defining Classes What should you choose for the name of the class to represent me? C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
54
{ }; Defining Classes class Two Hump Camel
C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
55
Defining Classes I’ll be going now… C++ for Everyone by Cay Horstmann
Copyright © 2012 by John Wiley & Sons. All rights reserved
56
Defining Classes but don’t forget: class names should be…
C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
57
Defining Classes …CAMEL CASE C++ for Everyone by Cay Horstmann
Copyright © 2012 by John Wiley & Sons. All rights reserved
58
There are two kinds of member functions: Mutators Accessors
Methods There are two kinds of member functions: Mutators Accessors C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
59
A mutator is a function that modifies the data members of the object.
Mutators A mutator is a function that modifies the data members of the object. C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
60
CashRegister has two mutators: clear
class CashRegister { public: void clear(); void add_item(double price); double get_total() const; int get_count() const; private: // data members will go here }; C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
61
CashRegister has two mutators: clear and add_item.
class CashRegister { public: void clear(); void add_item(double price); double get_total() const; int get_count() const; private: // data members will go here }; C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
62
CashRegister register1; ... register1.clear();
Mutators You call the member functions by first creating a variable of type CashRegister and then using the dot notation: Because these are mutators, the data stored in the class will be changed. CashRegister register1; ... register1.clear(); register1.add_item(1.95); C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
63
Mutators C++ for Everyone by Cay Horstmann
Copyright © 2012 by John Wiley & Sons. All rights reserved
64
An accessor is a function that queries a data member of the object.
Accessors An accessor is a function that queries a data member of the object. It returns the value of a data member to its caller. C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
65
CashRegister has two accessors: get_total
class CashRegister { public: void clear(); void add_item(double price); double get_total() const; int get_count() const; private: // data members will go here }; C++ for Everyone by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
66
CashRegister has two accessors: get_total and get_count.
class CashRegister { public: void clear(); void add_item(double price); double get_total() const; int get_count() const; private: // data members will go here }; C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved C++ for Everyone by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved
67
void add_item(double price); double get_total() const;
Accessors Because accessors should never change the data in an object, you should make them const . class CashRegister { public: void clear(); void add_item(double price); double get_total() const; int get_count() const; private: // data members will go here }; not here C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
68
This statement will print the current total:
Accessors This statement will print the current total: cout << register1.get_total() << endl; C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
69
Mutators and Accessors: The Interface
The interface for our class: C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
70
Class Definition Syntax
C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
71
Common Error: Can you find the error? class MysteryClass { public: ...
private: } int main() C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
72
Common Error: Missing Semicolon
Don’t forget that semicolon! class MysteryClass { public: ... private: } // Forgot semicolon int main() // Many compilers report // that error here in main! } C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
73
Let’s continue with the design of CashRegister.
Encapsulation Let’s continue with the design of CashRegister. Each CashRegister object must store the total price and item count of the sale that is currently rung up. We have to choose an appropriate data representation. C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
74
Encapsulation This is pretty easy: C++ for Everyone by Cay Horstmann
Copyright © 2012 by John Wiley & Sons. All rights reserved
75
item_count for the count
Encapsulation item_count for the count class CashRegister { public: // interface private: int item_count; double total_price; }; C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
76
total_price for the total
Encapsulation total_price for the total class CashRegister { public: // interface private: int item_count; double total_price; }; C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
77
Every CashRegister object has a separate copy of these data members.
Encapsulation Every CashRegister object has a separate copy of these data members. CashRegister register1; CashRegister register2; C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
78
Encapsulation C++ for Everyone by Cay Horstmann
Copyright © 2012 by John Wiley & Sons. All rights reserved
79
Because the data members are private, this won’t compile:
Encapsulation Because the data members are private, this won’t compile: int main() { ... cout << register1.item_count; // Error—use get_count() instead } C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
80
A good design principle: Never have any public data members.
Encapsulation A good design principle: Never have any public data members. Son, consider that an addition to the RULES! I know you can make data members public, but don’t. Just don’t do it! C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
81
Encapsulation and Methods as Guarantees
One benefit of the encapsulation mechanism is we can make guarantees. C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
82
Encapsulation and Methods as Guarantees
We can write the mutator for item_count so that item_count cannot be set to a negative value. If item_count were pubic, it could be directly set to a negative value by some misguided (or worse, devious) programmer. C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
83
Encapsulation and Methods as Guarantees
There is a second benefit of encapsulation that is particularly important in larger programs: Things Change. C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
84
Encapsulation and Methods as Guarantees
Well, that’s not really a benefit. Things change means: Implementation details often need to change over time … C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
85
Encapsulation and Methods as Guarantees
You want to be able to make your classes more efficient or more capable, without affecting the programmers that use your classes. The benefit of encapsulation is: As long as those programmers do not depend on the implementation details, you are free to change them at any time. C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
86
The Interface The interface should not change even if the details of how they are implemented change. C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
87
The Interface A driver switching to an electric car does not need to relearn how to drive. C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
88
Object-Oriented Programming
How dare you compare my interface with that, that… I’m shocked! C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
89
Implementing the Member Functions
Now we have what the interface does, and what the data members are, so what is the next step? Implementing the member functions. C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
90
Implementing the Member Functions
The details of the add_item member function: void add_item(double price) { item_count++; total_price = total_price + price; } C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
91
Implementing the Member Functions
Unfortunately this is NOT the add_item member function. It is a separate function, just like you used to write. It has no connection with the CashRegister class void add_item(double price) { item_count++; total_price = total_price + price; } C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
92
Implementing the Member Functions
To specify that a function is a member function of your class you must write CashRegister:: in front of the member function’s name: C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
93
Implementing the Member Functions
To specify that a function is a member function of your class you must write CashRegister:: in front of the member function’s name: Not here void CashRegister::add_item(double price) { item_count++; total_price = total_price + price; } C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
94
Implementing the Member Functions
Use CashRegister:: only when defining the function – not in the class definition. class CashRegister { public: ... private: }; Not here Only here void CashRegister::add_item(double price) { item_count++; total_price = total_price + price; } C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
95
We are changing data members …
Implicit Parameters Wait a minute. We are changing data members … BUT THERE’S NO VARIABLE TO BE FOUND! Which variable is add_item working on? C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
96
Oh No! We’ve got two cash registers! CashRegister register2;
Implicit Parameters Oh No! We’ve got two cash registers! CashRegister register2; CashRegister register1; Which cash register is add_item working on? C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
97
When a member function is called:
Implicit Parameters When a member function is called: The variable to the left of the dot operator is implicitly passed to the member function. In the example, register1 is the implicit parameter. CashRegister register1; ... register1.add_item(1.95); C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
98
The variable register1 is an implicit parameter.
Implicit Parameters The variable register1 is an implicit parameter. register1.add_item(1.95); void CashRegister::add_item(double price) { implicit parameter.item_count++; implicit parameter.total_price = implicit parameter.total_price + price; } C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
99
Implicit Parameters C++ for Everyone by Cay Horstmann
Copyright © 2012 by John Wiley & Sons. All rights reserved
100
We’ll get back to this, later …
Implicit Parameters We’ll get back to this, later … C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
101
Calling a Member Function from a Member Function
Let’s add a member function that adds multiple instances of the same item. C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
102
Calling a Member Function from a Member Function
Like when we are programming… and we get a dozen strong, black coffees to go. ¥500 C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
103
Calling a Member Function from a Member Function
We have already written the add_item member function and the same good design principle of code reuse with functions is still fresh in our minds, so: void CashRegister::add_items(int qnt, double prc) { for (int i = 1; i <= qnt; i++) add_item(prc); } C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
104
Calling a Member Function from a Member Function
When one member function calls another member function on the same object, you do not use the dot notation. void CashRegister::add_items(int qnt, double prc) { for (int i = 1; i <= qnt; i++) add_item(prc); } C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
105
Calling a Member Function from a Member Function
So how does this work? Remember our friend: implicit parameter ! It’s as if it were written to the left of the dot (which also isn’t there) register1.add_items(6,0.95); void CashRegister::add_items(int qnt, double prc) { for (int i = 1; i <= qnt; i++) implicit parameter.add_item(prc); } C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
106
Calling a Member Function from a Member Function
C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
107
The Cash Register Program
ch09/registertest1.cpp #include <iostream> #include <iomanip> using namespace std; /** A simulated cash register that tracks the item count and the total amount due. */ class CashRegister { public: C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
108
The Cash Register Program
ch09/registertest1.cpp class CashRegister { public: /** Clears the item count and the total. */ void clear(); Adds an item to this cash register. @param price the price of this item void add_item(double price); C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
109
The Cash Register Program
ch09/registertest1.cpp /** @return the total amount of the current sale */ double get_total() const; @return the item count of the current sale int get_count() const; private: int item_count; double total_price; }; C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
110
The Cash Register Program
ch09/registertest1.cpp void CashRegister::clear() { item_count = 0; total_price = 0; } void CashRegister::add_item(double price) item_count++; total_price = total_price + price; double CashRegister::get_total() const return total_price; C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
111
The Cash Register Program
ch09/registertest1.cpp int CashRegister::get_count() const { return item_count; } /** Displays the item count and total price of a cash register. @param reg the cash register to display */ void display(CashRegister reg) cout << reg.get_count() << " $“ << fixed << setprecision(2) << reg.get_total() << endl; C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
112
The Cash Register Program
ch09/registertest1.cpp int main() { CashRegister register1; register1.clear(); register1.add_item(1.95); display(register1); register1.add_item(0.95); register1.add_item(2.50); return 0; } C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
113
const Correctness You should declare all accessor functions in C++ with the const reserved word. C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
114
But let’s say, just for the sake of checking things out
const Correctness But let’s say, just for the sake of checking things out – you would never do it yourself, of course – suppose you did not make display const: class CashRegister { void display(); // Bad style—no const ... }; C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
115
This will compile with no errors.
const Correctness This will compile with no errors. class CashRegister { void display(); // Bad style—no const ... }; C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
116
But son, it’s not just about you.
const Correctness But son, it’s not just about you. C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
117
Very correctly she makes the array const.
const Correctness What happens when some other, well intentioned, good design-thinking programmer uses your class, an array of them actually, in a function. Very correctly she makes the array const. void display_all(const CashRegister[] registrs) { for (int i = 0; i < NREGISTERS; i++) registrs[i].display(); } C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
118
Son, look what you’ve done!
const Correctness Son, look what you’ve done! C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
119
void display_all(const CashRegister[] registrs) {
const Correctness The compiler (correctly) notices that registrs[i].display() is calling a NON-CONST display method on a CONST CashRegister object. void display_all(const CashRegister[] registrs) { for (int i = 0; i < NREGISTERS; i++) registrs[i].display(); } compiler error C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
120
const Correctness Son…
Yes, it’s actually her fault for not reading your code closely enough but that is no excuse for your bad behavior. C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
121
Constructors House house1; House house2; House house3; ...
A friendly construction worker reading a class definition C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
122
(It doesn’t construct?)
Constructors A constructor is a member function that initializes the data members of an object. (It doesn’t construct?) C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
123
The constructor is automatically called whenever an object is created.
Constructors The constructor is automatically called whenever an object is created. CashRegister register1; (You don’t see it but it’s there.) C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
124
By supplying a constructor,
Constructors By supplying a constructor, you can ensure that all data members are properly set before any member functions act on an object. (Ah, consistency …) C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
125
By supplying a constructor,
Constructors By supplying a constructor, you can ensure that all data members are properly set before any member functions act on an object. What would be the value of a data member that was not (no way!) properly set? GARBAGE C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
126
“Garbage” is a technical computer science term.
Constructors “Garbage” is a technical computer science term. It means… …well… “garbage.” C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
127
CashRegister register1; register1.add_item(1.95);
Constructors To understand the importance of constructors, consider the following statements: CashRegister register1; register1.add_item(1.95); int count = get_count(); // May not be 1 Notice that the programmer forgot to call clear before adding items. (Smells like “garbage” to me!) C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
128
CashRegister register1; register1.add_item(1.95);
Constructors To understand the importance of constructors, consider the following statements: CashRegister register1; register1.add_item(1.95); int count = get_count(); // May not be 1 Notice that the programmer forgot to call clear before adding items. (Recall that technical computer science term and what it means?) C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
129
CashRegister register1; register1.add_item(1.95);
Constructors To understand the importance of constructors, consider the following statements: CashRegister register1; register1.add_item(1.95); int count = get_count(); // May not be 1 Notice that the programmer forgot to call clear before adding items. (A “garbage” value is not to be trusted.) (And preferably not smelled.) C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
130
Constructors Constructors are written to guarantee that an object is always fully and correctly initialized when it is defined. (Ah, consistency …) (I said that already, didn’t I?) (At least I am consistent!) C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
131
You declare constructors in the class definition:
class CashRegister { public: CashRegister(); // A constructor ... }; C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
132
The name of a constructor is identical to the name of its class:
Constructors The name of a constructor is identical to the name of its class: class CashRegister { public: CashRegister(); // A constructor ... }; C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
133
There must be no return type, not even void.
Constructors There must be no return type, not even void. class CashRegister { public: CashRegister(); // A constructor ... }; C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
134
And, of course, you must define the constructor.
Constructors And, of course, you must define the constructor. CashRegister::CashRegister() { item_count = 0; total_price = 0; } C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
135
CashRegister::CashRegister() { item_count = 0; total_price = 0; }
Constructors To connect the definition with the class, you must use the same :: notation CashRegister::CashRegister() { item_count = 0; total_price = 0; } C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
136
You should choose initial values
Constructors You should choose initial values for the data members so the object is correct. CashRegister::CashRegister() { item_count = 0; total_price = 0; } C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
137
And still no return type.
Constructors And still no return type. CashRegister::CashRegister() { item_count = 0; total_price = 0; } C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
138
A constructor with no parameters is called a default constructor.
Constructors A constructor with no parameters is called a default constructor. CashRegister::CashRegister() { item_count = 0; total_price = 0; } C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
139
Default constructors are called when you define an object
and do not specify any parameters for the construction. Notice that you do NOT use an empty set of parentheses. CashRegister register1; C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
140
CashRegister register1;
Constructors register1.item_count and register1.total_price are set to zero as they should be. CashRegister register1; C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
141
Constructors can have parameters, and constructors can be overloaded:
class BankAccount { public: // Sets balance to 0 BankAccount(); // Sets balance to initial_balance BankAccount(double initial_balance); // Member functions omitted private: double balance; }; C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
142
BankAccount joes_account; // Uses default constructor
Constructors When you construct an object, the compiler chooses the constructor that matches the parameters that you supply: BankAccount joes_account; // Uses default constructor BankAccount lisas_account(499.95); // Uses BankAccount(double) constructor C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
143
They will be garbage if you don’t set them in the constructor.
Constructors It is good design to think about what values you should put in numeric and pointer data members. They will be garbage if you don’t set them in the constructor. Is that OK? (Son…) C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
144
Data members of classes that have constructors will not be garbage.
For example, the string class has a default constructor that sets strings to the empty string (""). C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
145
THINK: is the default string OK?
Constructors THINK: is the default string OK? ... private: string name; double hourlyRate; }; THINK, then set. C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
146
Common Error: Trying to Use the Constructor to Reset
You cannot use a constructor to “reset” a variable. It seems like a good idea but you can’t: CashRegister register1; ... register1.CashRegister(); // Error C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
147
Constructors – The System Default Constructor
If you write no constructors at all, the compiler automatically generates a system default constructor that initializes all data members of class type with their default constructors (which is just garbage for numeric and pointer data members). C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
148
Initialization Lists When you construct an object whose data members are themselves objects, those objects are constructed by their class’s default constructor. However, if a data member belongs to a class without a default constructor, you need to invoke the data member’s constructor explicitly. C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
149
A class to represent an order might not have a default constructor:
Initialization Lists A class to represent an order might not have a default constructor: class Item: public: Item(string item_descript, double item_price); // No other constructors ... }; C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
150
Order(string customer_name, string item_descript, double item_price);
Initialization Lists A class to represent an order would most likely have an Item type data member: class Order { public: Order(string customer_name, string item_descript, double item_price); ... private: Item article; string customer; }; C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
151
The Order constructor must call the Item constructor.
Initialization Lists The Order constructor must call the Item constructor. This is done in the initializer list. The initializer list goes before the opening brace of the constructor by putting the name of the data member followed by their construction arguments: Order::Order(string customer_name, string item_description, double item_price) : article(item_description, item_price) ... C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
152
Order::Order(string customer_name, string item_description,
Initialization Lists Any other data members can also be initialized in the initializer list by putting their initial values in parentheses after their name, just like the class type data members. These must be separated by commas: Order::Order(string customer_name, string item_description, double item_price) : article(item_description, item_price), customer(customer_name) { } Notice there’s nothing to do in the body of the constructor now. C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
153
Recall how you hand traced code to help you understand functions.
Tracing Objects Recall how you hand traced code to help you understand functions. Adapting tracing for objects will help you understand objects. Grab some index cards (blank ones). C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
154
You know that the public: section is for others.
Tracing Objects You know that the public: section is for others. That’s where you’ll write methods for their use. That will be the front of the card. class CashRegister { public: void clear(); void add_item(double price); double get_total() const; int get_count() const; private: int item_count; double total_price; }; ... CashRegister reg1; C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
155
That will be the back of the card.
Tracing Objects You know that the private: section is for your data – they are not allowed to mess with it except through the public methods you provide. That will be the back of the card. class CashRegister { public: void clear(); void add_item(double price); double get_total() const; int get_count() const; private: int item_count; double total_price; }; ... CashRegister reg1; C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
156
You’ll need a card for every variable.
Tracing Objects You’ll need a card for every variable. You might want to make several now. C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
157
When an object is constructed,
Tracing Objects CashRegister reg1; When an object is constructed, add the variable’s name to the front of a card and fill in the initial values. C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
158
You would do this for every variable.
Tracing Objects CashRegister reg1; CashRegister reg2; You would do this for every variable. C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
159
When a method is invoked, grab the right card...
Tracing Objects CashRegister reg1; CashRegister reg2; reg1.addItem(19.95); When a method is invoked, grab the right card... C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
160
Tracing Objects CashRegister reg1; CashRegister reg2; …flip it over…
reg1.addItem(19.95); …flip it over… C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
161
…cross out the old values...
Tracing Objects CashRegister reg1; CashRegister reg2; reg1.addItem(19.95); …cross out the old values... C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
162
…then write the new values below.
Tracing Objects CashRegister reg1; CashRegister reg2; reg1.addItem(19.95); …then write the new values below. C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
163
Suppose you are asked to get the sales tax.
Tracing Objects These cards can help you in development when you need to add more functionality: Suppose you are asked to get the sales tax. C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
164
You would add that to the front of the cards.
Tracing Objects You would add that to the front of the cards. Grab any card – they will all have to be redone. Add the newly requested method. Then flip it over and start thinking. get_sales_tax C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
165
You would add that to the front of the cards.
Tracing Objects You would add that to the front of the cards. Grab any card – they will all have to be redone. Add the newly requested method. Then flip it over and start thinking. C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
166
I have to calculate the sales tax.
Tracing Objects I have to calculate the sales tax. Do I have enough information here on the back of this card? I can only use these and any values passed in through parameters and global variables. get_sales_tax C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
167
Need a new data member: taxable_total.
Tracing Objects Tax rate? Need a new data member tax_rate for this which would be set in the constructor to a global constant. Are all items taxable? Need to add another parameter for taxable-or-not to add_item which would appropriately update... …what??? Need a new data member: taxable_total. C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
168
Add these things and do some tracing.
Tracing Objects Add these things and do some tracing. CashRegister reg2(TAX_RATE); reg2.addItem(3.95, false); reg2.addItem(19.95, true); C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
169
Now you get to actually use English in Object Oriented Programming!
Discovering Classes Do you recall all those little mini-English lessons we’ve been throwing in? Now you get to actually use English in Object Oriented Programming! (Hi!) (That’s Japanese for “yes”.) C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
170
Discovering Classes Using nouns and verbs
C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
171
Discovering Classes One simple approach for discovering classes and member functions is to look for the nouns and verbs in the problem description. C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
172
nouns correspond to classes, and verbs correspond to member functions.
Discovering Classes Often times, nouns correspond to classes, and verbs correspond to member functions. C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
173
Many classes are abstractions of real-life entities. BankAccount
Discovering Classes Many classes are abstractions of real-life entities. BankAccount CashRegister C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
174
The name for such a class should be a noun that describes the concept.
Discovering Classes Generally, concepts from the problem domain, be it science, business, or a game, make good classes. The name for such a class should be a noun that describes the concept. Other frequently used classes represent system services such as files or menus. C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
175
Not Discovering Classes
What might not be a good class? If you can’t tell from the class name what an object of the class is supposed to do, then you are probably not on the right track. C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
176
Not Discovering Classes
For example, you might be asked to write a program that prints paychecks. You start by trying to design a class PaycheckProgram. C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
177
Not Discovering Classes
class PaycheckProgram ? What would an object of this class do? C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
178
Not Discovering Classes
class PaycheckProgram ? ? An object of this class would have to do everything! C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
179
Not Discovering Classes
class PaycheckProgram ? ? ? That doesn’t simplify anything. A better class would be: C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
180
! ! ! ! ! Discovering Classes class Paycheck
! ! ! ! ! C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
181
Not Discovering Classes
Another common mistake, made particularly by those who are used to writing programs that consist of functions, is to turn an action into a class. C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
182
Not Discovering Classes
For example, if you are to compute a paycheck, you might consider writing a class ComputePaycheck. C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
183
Not Discovering Classes
class ComputePaycheck But can you visualize a “ComputePaycheck” object? A thing that is a computePaycheck? C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
184
Not Discovering Classes
class ComputePaycheck The fact that “computepaycheck” is not a noun tips you off that you are on the wrong track. On the other hand, a “paycheck” class makes intuitive sense. (The word “paycheck” is a noun.) C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
185
You can visualize a paycheck object.
Discovering Classes You can visualize a paycheck object. You can then think about useful member functions of the Paycheck class, such as compute_taxes, that help you solve the problem. C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
186
It is then helpful to consider how these classes are related.
“Has-a” relationship When you analyze a problem description, you often find that you have multiple classes. It is then helpful to consider how these classes are related. One of the fundamental relationships between classes is the “aggregation” relationship (which is informally known as the “has-a” relationship). C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
187
“Has-a” relationship The aggregation relationship states that objects of one class contain objects of another class. C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
188
Consider a quiz that is made up of questions.
“Has-a” relationship Consider a quiz that is made up of questions. Since each quiz has one or more questions, we say that the class Quiz aggregates the class Question (not to be confused with “begs the question”). C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
189
UML (Unified Modeling Language)
There is a standard notation to describe class relationships: a UML class diagram (Unified Modeling Language) C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
190
UML (Unified Modeling Language)
In the UML notation, aggregation is denoted by a line with a diamond-shaped symbol C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
191
UML (Unified Modeling Language)
The problem states that the Quiz object manages lots of Question objects. The code follows directly, using a vector to mange the Questions: class Quiz { }; ... private: vector<Question> questions; C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
192
• Find relationships between the classes that you have discovered.
Discovering Classes In summary, when you analyze a problem description, you will want to carry out these tasks: • Find the concepts that you need to implement as classes. Often, these will be nouns in the problem description. • Find the responsibilities of the classes. Often, these will be verbs in the problem description. • Find relationships between the classes that you have discovered. C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
193
You will want to split your code into separate source files.
Separate Compilation When you write and compile small programs, you can place all your code into a single source file. When your programs get larger or you work in a team, that situation changes. You will want to split your code into separate source files. C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
194
There are two reasons why this split becomes necessary.
Separate Compilation There are two reasons why this split becomes necessary. First, it takes time to compile a file, and it seems silly to wait for the compiler to keep translating code that doesn’t change. If your code is distributed over several source files, then only those files that you changed need to be recompiled. C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
195
Separate Compilation The second reason becomes apparent when you work with other programmers in a team. It would be very difficult for multiple programmers to edit a single source file simultaneously. Therefore, the program code is broken up so that each programmer is solely responsible for a separate set of files. C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
196
There must be a path of communication between the files.
Separate Compilation If your program is composed of multiple files, some of these files will define data types or functions that are needed in other files. There must be a path of communication between the files. In C++, that communication happens through the inclusion of header files. Yes, #include. C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
197
The code will be in two kinds of files: header files
Separate Compilation The code will be in two kinds of files: header files (which will be #include-ed) source files (which should never be #include-ed) C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
198
Definitions of classes. Definitions of constants.
Separate Compilation A header file contains the interface: Definitions of classes. Definitions of constants. Declarations of nonmember functions. C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
199
Definitions of member functions. Definitions of nonmember functions.
Separate Compilation A source file contains the implementation: Definitions of member functions. Definitions of nonmember functions. C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
200
There will also be either:
Separate Compilation There will also be either: a “tester” program or the real problem solution This is where main goes. C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
201
cashregister.h cashregister.cpp
Separate Compilation For the CashRegister class, you create a pair of files: cashregister.h the interface – the class definition cashregister.cpp the implementation – all the member function definitions C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
202
Separate Compilation: The Cash Register Program
This is the header file, cashregister.h Notice the first two lines. There is an ending #endif at the end of the file. This makes sure the header is only included once. Always write these. Use the name of the as shown. #ifndef CASHREGISTER_H #define CASHREGISTER_H /** A simulated cash register that tracks the item count and the total amount due. */ class CashRegister ch09/cashregister.h C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
203
Separate Compilation: The Cash Register Program
/** A simulated cash register that tracks the item count and the total amount due. */ class CashRegister { public: Constructs a cash register with cleared item count and total. CashRegister(); Clears the item count and the total. void clear(); ch09/cashregister.h C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
204
Separate Compilation: The Cash Register Program
/** Adds an item to this cash register. @param price the price of this item */ void add_item(double price); @return the total amount of the current sale double get_total() const; @return the item count of the current sale int get_count() const; ch09/cashregister.h C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
205
Separate Compilation: The Cash Register Program
private: int item_count; double total_price; }; #endif ch09/cashregister.h You include this header file whenever the definition of the CashRegister class is required. Since this file is not a standard header file, you must enclose its name in quotes, not <...>, when you include it, like this: #include "cashregister.h" And now the implementation (.cpp) file: C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
206
Separate Compilation: The Cash Register Program
Notice that the implementation file #includes its own header file. #include "cashregister.h" CashRegister::CashRegister() { clear(); } void CashRegister::clear() item_count = 0; total_price = 0; ch09/cashgregister.cpp C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
207
Separate Compilation: The Cash Register Program
ch09/cashgregister.cpp void CashRegister::add_item(double price) { item_count++; total_price = total_price + price; } double CashRegister::get_total() const return total_price; int CashRegister::get_count() const return item_count; C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
208
They are part of the interface, not the implementation.
Separate Compilation Notice that the member function comments are in the header file, not the .cpp file. They are part of the interface, not the implementation. C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
209
Separate Compilation There’s no main! HELP!
No, someone who wants to use your class will write their own main and #include your header. Like this: C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
210
Separate Compilation: The Cash Register Program
ch09/registertest2.cpp #include <iostream> #include <iomanip> #include "cashregister.h" using namespace std; /** Displays the item count and total price of a cash register. @param reg the cash register to display */ void display(CashRegister reg) { cout << reg.get_count() << " $“ << fixed << setprecision(2) << reg.get_total() << endl; } C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
211
Separate Compilation: The Cash Register Program
ch09/registertest2.cpp int main() { CashRegister register1; register1.clear(); register1.add_item(1.95); display(register1); register1.add_item(0.95); register1.add_item(2.50); return 0; } C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
212
NAMESPACE POLUTION ERROR
FIX POLUTION OF NAMESPACE C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
213
Dynamically Allocating Objects How about dynamic objects? Fine:
Pointers to Objects Dynamically Allocating Objects How about dynamic objects? Fine: C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
214
Pointers to Objects CashRegister* register_pointer = new CashRegister;
BankAccount* account_pointer = new BankAccount(1000); C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
215
Accessing: The -> Operator
Because register_pointer is a pointer to a CashRegister object, the value *register_pointer denotes the CashRegister object itself. To invoke a member function on that object, you might call (*register_pointer).add_item(1.95); C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
216
Accessing: The -> Operator
The parentheses are necessary because in C++ the dot operator takes precedence over the * operator. The expression without the parentheses would be a syntax error: *register_pointer.add_item(1.95); // Error – you can’t apply . to a pointer C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
217
register_pointer->add_item(1.95);
Pointers to Objects Because calling a member function through a pointer is very common, the designers of C++ supply an operator to abbreviate the “follow pointer and access member” operation. That operator is written -> and usually pronounced as “arrow”. Here is how you use the “arrow” operator: register_pointer->add_item(1.95); C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
218
And now for another English sort of thing: The this pointer.
(Yes, it’s correct English) (if you are talking about C++.) Remember, way back there, when we said: C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
219
“We’ll get back to this, later …”
Implicit Parameters “We’ll get back to this, later …” Well, now is later! C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
220
That’s the this pointer.
Implicit Parameters this That’s the this pointer. C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
221
The variable register1 is the implicit parameter.
Implicit Parameters The variable register1 is the implicit parameter. this = register1 (assigned by the system) register1.add_item(1.95); void CashRegister::add_item(double price) { implicit parameter.item_count++; implicit parameter.total_price = implicit parameter.total_price + price; } this. C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
222
The this Pointer Each member function has a special parameter variable, called this, which is a pointer to the implicit parameter. C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
223
CashRegister::add_item
The this Pointer For example, consider the member function CashRegister::add_item C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
224
The this Pointer If you call ... register1.add_item(1.95) ...
then the this pointer has type CashRegister* and points to the register1 object. C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
225
The this Pointer (I don’t see it.) No, but you can use it:
C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
226
The this Pointer ... register1.add_item(1.95) ...
C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
227
The this pointer is made to point to the implicit variable.
If you call ... register1.add_item(1.95) ... The this pointer is made to point to the implicit variable. (The system did that assignment behind your back.) (Thank you!) this C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
228
void CashRegister::add_item(double price) { this->item_count++;
The this Pointer void CashRegister::add_item(double price) { this->item_count++; this->total_price = this->total_price + price; } C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
229
void CashRegister::add_item(double price) { this->item_count++;
The this Pointer void CashRegister::add_item(double price) { this->item_count++; this->total_price = this->total_price + price; } this points at the that implicit parameter. C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
230
void CashRegister::add_item(double price) { this->item_count++;
The this Pointer void CashRegister::add_item(double price) { this->item_count++; this->total_price = this->total_price + price; } The this pointer is not necessary here, but some programmers like to use the this pointer to make it very, very clear that item_count is a data member and not a variable. The this pointer is not necessary here, but some programmers like to use the this pointer to make it very, very clear that item_count and total_price are data members—not (plain old) variables or parameters. C++ for Everyone by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
231
Destructors and Resource Management
When a programmer uses new to obtain a dynamic array, she is requesting a system resource. And as all good recyclers know… …resources are limited and should be returned. peas recyple The this pointer is not necessary here, but some programmers like to use the this pointer to make it very, very clear that item_count is a data member and not a variable. C++ for Everyone by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
232
Destructors and Resource Management
THE HEAP: No Entry Without Permission Of OS! Destructors and Resource Management class String { ... private: char* char_array; } String::String(const char initial_chars[]) char_array = new char[strlen(initial_chars) + 1]; strcpy(char_array, initial_chars); The characters of a String are stored on the heap, a system resource. C++ for Everyone by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
233
Destructors and Resource Management
Constructors don’t really construct (they initialize). There is another method that doesn’t really do what it’s name implies: the destructor. (Not in any way associated with professional wrestling.) The this pointer is not necessary here, but some programmers like to use the this pointer to make it very, very clear that item_count is a data member and not a variable. C++ for Everyone by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
234
Destructors and Resource Management
A destructor, like a constructor, is written without a return type and its name is the tilde character followed by the name of the class: ~ String (not to be confused with the dwiddle character) The this pointer is not necessary here, but some programmers like to use the this pointer to make it very, very clear that item_count is a data member and not a variable. C++ for Everyone by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
235
Destructors and Resource Management
A class can have only one destructor and it cannot have any parameters. String::~ String() { ... The this pointer is not necessary here, but some programmers like to use the this pointer to make it very, very clear that item_count is a data member and not a variable. C++ for Everyone by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
236
Destructors and Resource Management
Destructors don’t really destruct: String::~ String() { ... The this pointer is not necessary here, but some programmers like to use the this pointer to make it very, very clear that item_count is a data member and not a variable. C++ for Everyone by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
237
Destructors and Resource Management
Destructors don’t really destruct: they are used to recycle resources. String::~ String() { ... The this pointer is not necessary here, but some programmers like to use the this pointer to make it very, very clear that item_count is a data member and not a variable. C++ for Everyone by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
238
Destructors and Resource Management
Destructors don’t really destruct: they are used to recycle resources. String::~ String() { delete[] char_array; } The this pointer is not necessary here, but some programmers like to use the this pointer to make it very, very clear that item_count is a data member and not a variable. C++ for Everyone by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
239
Destructors and Resource Management
THE HEAP: No Entry Without Permission Of OS! Destructors and Resource Management The memory for the characters in a string are properly recycled.. void fun() { String name("Harry"); ... } Heap memory is allocated by the constructor Do you seen a method being invoked? Right there! C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved C++ for Everyone by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved
240
Destructors and Resource Management
THE HEAP: No Entry Without Permission Of OS! Destructors and Resource Management Destructors are automatically invoked when an object of that type is no longer needed. The memory for the characters in a string are properly recycled.. void fun() { String name("Harry"); ... } String::~String() is invoked right there. C++ for Everyone by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
241
Destructors and Resource Management
THE HEAP: No Entry Without Permission Of OS! Destructors and Resource Management Unfortunately, it’s a more complicated when assignment comes along: void no_fun() { String name1("Harry"); String name2("Sally"); name1 = name2; ... } Heap memory is allocated by both the constructors What happened to the memory for “Harry”? Now what?! C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved C++ for Everyone by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved
242
Destructors and Resource Management
This is not a topic covered in these slides. It involves: the destructor and another kind of constructor - the copy constructor and rewriting how the assignment operation works. These three topics together are called The Big Three. (Again, not in any way associated with professional wrestling.) C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved C++ for Everyone by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved
243
Object-Oriented Programming
Now I know you love me for the object I am! C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
244
Object-Oriented Programming
Hump h! Hrrrrr … Now I know you love me for the object I am! C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved
245
Chapter Summary C++ for Everyone by Cay Horstmann
Copyright © 2012 by John Wiley & Sons. All rights reserved
246
Chapter Summary C++ for Everyone by Cay Horstmann
Copyright © 2012 by John Wiley & Sons. All rights reserved
247
Chapter Summary C++ for Everyone by Cay Horstmann
Copyright © 2012 by John Wiley & Sons. All rights reserved
248
Chapter Summary C++ for Everyone by Cay Horstmann
Copyright © 2012 by John Wiley & Sons. All rights reserved
249
Slides by Evan Gallagher
End Chapter Nine C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved Slides by Evan Gallagher
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.