Chapter 10 Margin Notes
General Note: In this chapter we finally get to the topic that puts the ++ in C++...Classes and Objects.
Page 441, "ADT". In case you forgot, ADT stands for Abstract Data Type.
Page 441, The Idea of Classes and Objects. Classes are frequently analogized to a cookie cutter, and Objects to the cookies pressed out of the dough. For instance, a program may have a single Class called Employee, with several actual instances of Employee objects, such as Mary, Jane, John and Al.
Page 441-442, Word Processing Analogy. I really hate this analogy---it's probably just going to confuse you.
Page 442, Magnifying Glass at the bottom of the page. This is good---classes define attributes and behavior for an object. For instance, each Employee object may be defined to have attributes such as Name, Social Security Number, Pay Rate. Behavior in an Employee class might be the ability to Add a new Employee, Retire an Employee, change the Pay Rate, etc.
Page 443, "abstraction". Worth repeating---a class is abstract, an object is an instance of a class.
Page 443, The Abstract Level. Enough already--let's get to the nitty gritty. How many times can you repeat this?
Page 443, "interface". Theoretics, theoretics, theoretics.
Page 444, The Implementation Level. Enough already--let's get to the nitty gritty. How many times can you repeat this?
Page 444, Members. Members of a class are its variables (what the author calls data members) and its functions.
Page 444, Data Members. Good Object Oriented implementation requires that the Data Members (the variables in a class) be defined as private---this provides for the "Information Hiding" that is mentioned on Page 440.
Page 445, Function Members. Function Members are functions defined in a class. Most often, these functions are designed to do 'something' with the Data members (variables) of the class. In theory, programs that create objects using our Class template can only 'get at' the Data Members of the class by using these Function Members.
Page 445, Class Declaration Format. Similar to what we've been working with all along in the course--the difference being that the first line of a class begins with the keyword 'class'---notice that the last line of the class ends with a semicolon.
Page 446. I really hate declaring the public section of the class first--my preference is to declare the private section first, but this is the author's preference. The important thing is that some parts of the class will be public (functions) and some parts private (variables). Some functions may be defined as private also.
Page 446, Debugging Tip. I've already mentioned it, but it's worth noting again.
Pages 447, 448 Let's get to the examples!
Pages 449. OK, finally we get to the example. The class is SavingsAccount, its functions are
Its variables are
Pages 449, UML. UML stands for Unified Modeling Language, and is considered the industry standard for designing/documenting classes. There's no need to go crazy over this in this course---read the extra material at the end of the chapter for more information on UML.
Pages 450, the SavingsAccount class. Finally! You may want to code this up and follow along (actually, save your efforts for the Rectangle class which is much easier to code and follow). Notice that there is no implementation code for addDeposit(), addInterest(), getBalance() or subtractWithdrawal(). In essence, what we have here are function prototypes. Notice that two of the four functions are defined with parameters (arguments).
Warning: You'll be able to compile this code, but the linker step will fail since it doesn't contain a main() function. Classes are designed to be used by programs that contain a main() function. Hang on!
By the way, if you don't code the public and private sections like this...
void addDeposit(double amount);
void subtractWithdrawal(double amount);
you would have to do this...
public void addDeposit(double amount);
public void addInterest();
public double getBalance();
public void subtractWithdrawal(double amount);
private int accountNumber;
private double balance;
private double interestRate;
Pages 450, the SavingsAccount class. Finally! Code this up and follow along. Warning: You'll be able to compile this code, but the linker step will fail
Pages 451-452. A nice second example--again, code this one up. By the way, notice that there is no actual implementation code for the area() or perimeter() functions. Again, if you compile this class, it will compile successfully OK, but the linker step will fail.
Pages 453. I'm sorry this section is so weak--the author waits too long to show you how to use the Rectangle class in your program. Skip to page 469 for a preview.
Pages 454, Example 10.2. Horrendous example--no explanation. In order to create an instance of the Rectangle class or the SavingsAccount class, you need to specify a variable name--just like declaring an int variable. Here, the name of the variable used to represent a Rectangle object is box
Here, the name of the variable used to represent a SavingAccount object is myAccount
These variables could just as easily be named x and y
Pages 454, Member Functions. Functions in a class are no different than ordinary functions---a lot of overkill here--but follow along with the code, and update your class coding as you do so.
Pages 455, Scoping Operator (:). Check out the Scoping Operator. Make sure you include this. Note how the return type of the function precedes the name of the class.
Pages 456, Constructors. Each class has a special function, having the name of the class, called a Constructor. Constructor functions are automatically executed whenever an object from a class is created---and typically have 'initialization' code in them, such as setting initial values of class variables. Note the rules for creating Constructors---probably the most important one being that Constructors do not have a return type, not even void. If you accidentally code a Constructor with a return type, there will be no syntax error--however, it won't automatically execute when an object of the class is created.
Pages 459, Default Parameters for Constructors. Note how this is done.
Pages 459, Overloaded Constructors. Just as ordinary functions can be overloaded, so too can Constructors. Remember, an overloaded function is one that performs different tasks depending on the number and/or type of arguments that it receives.
Pages 460, Example at the bottom of the page. Notice the different implementation of the Rectangle object---one is for a square, one is for a regular box.
Pages 462, Scoping Inside of Functions. Be careful of naming your function arguments the same as a class variable. It can cause problems. Rather than deal with the convoluted process the author gives to solve this problem (although 'this' is pretty cool), take the easier approach---name them something different. This, in essence, is what he's saying in the Programming Tip on Page 463.
Pages 464, Example 10.4. Take my advice and skip to Page 466. You don't need another example to follow.
Pages 466, Access Functions. An Access function is just an ordinary function that provides access to the private variables of the class. These are typically named with the prefix get, and use the return statement to return the value of the private variable to the program calling it.
Pages 468, the Dot Operator. The Dot operator is used in conjunction with objects to execute its member functions---like this...
Pages 468, Code Example. Take note that box3 is creating a Rectangle object using a non-existing Constructor. What do you think will happen? If you code up this example, compile and test it, you'll find out. By the way, notice how the class definition is included in the code for the program that will create objects for it. This is not typical. Usually, the code for the class is separate and distinct from the code that implements the class. We'll see how to do this in my in-class demo, and to some degree, on Page 478.
Pages 471, Problem Solving in Action. An interesting example---you may want to follow along with this, or just skip to page 478 to learn how to create a multifile implementation.
Pages 478, Multifile Program Construction. Read this section carefully---and pay careful attention to my in-class demo if you have trouble following this. In Microsoft's Visual C++, you can have multiple files be part of your project--that's what you'll need to do here. You've already read Appendix C--but as a refresher, check out Page 723 in the book for help on adding multiple files to your Visual C++ project.