In this chapter you’ve seen how to define a class and its class methods. The next step is to produce a program that creates and uses objects of a class. The C++ goal is to make using classes as similar as possible to using the basic, built-in types, such as int and char. You can create a class object by declaring a class variable or using new to allocate an object of a class type. You can pass objects as arguments, return them as function return values, and assign one object to another. C++ provides facilities for initializing objects, teaching cin and cout to recognize objects, and even providing automatic type conversions between objects of similar classes. It will be a while before you can do all those things, but let’s start now with the simpler properties. Indeed, you’ve already seen how to declare a class object and call a member function. Listing 10.3 provides a program to use the interface and implementation files. It creates a Stock object named fluffy_the_cat. The program is simple, but it tests the features built in to the class. To compile the complete program, use the techniques for multifile programs described in Chapter 1, “Getting Started with C++,” and in Chapter 9. In particular, compile it with stock00.cpp and have stock00.h present in the same directory or folder.

Listing 10.3. usestok0.cpp

// usestck0.cpp -- the client program

// compile with stock00.cpp

#include

#include "stock00.h"

int main()

{

    Stock fluffy_the_cat;

    fluffy_the_cat.acquire("NanoSmart", 20, 12.50);

    fluffy_the_cat.show();

    fluffy_the_cat.buy(15, 18.125);

    fluffy_the_cat.show();

    fluffy_the_cat.sell(400, 20.00);

    fluffy_the_cat.show();

    fluffy_the_cat.buy(300000,40.125);

    fluffy_the_cat.show();

    fluffy_the_cat.sell(300000,0.125);

    fluffy_the_cat.show();

    return 0;

}

Here is the output of the program in Listing 10.3:

Company: NanoSmart  Shares: 20

  Share Price: $12.5  Total Worth: $250

Company: NanoSmart  Shares: 35

  Share Price: $18.125  Total Worth: $634.375

You can't sell more than you have! Transaction is aborted.

Company: NanoSmart  Shares: 35

  Share Price: $18.125  Total Worth: $634.375

Company: NanoSmart  Shares: 300035

  Share Price: $40.125  Total Worth: $1.20389e+007

Company: NanoSmart  Shares: 35

  Share Price: $0.125  Total Worth: $4.375

Note that main() is just a vehicle for testing the design of the Stock class. When the Stock class works as you want it to, you can use it as a user-defined type in other programs. The critical point in using the new type is to understand what the member functions do; you shouldn’t have to think about the implementation details. See the following sidebar, “The Client/Server Model.”

The Client/Server Model

OOP programmers often discuss program design in terms of a client/server model. In this conceptualization, the client is a program that uses the class. The class declaration, including the class methods, constitute the server, which is a resource that is available to the programs that need it. The client uses the server through the publicly defined interface only. This means that the client’s only responsibility, and, by extension, the client’s programmer’s only responsibility, is to know that interface. The server’s responsibility, and, by extension, the server’s designer’s responsibility, is to see that the server reliably and accurately performs according to that interface. Any changes the server designer makes to the class design should be to details of implementation, not to the interface. This allows programmers to improve the client and the server independently of each other, without changes in the server having unforeseen repercussions on the client’s behavior.

Changing the Implementation

There may be something about the program output, aside from Fluffy the Cat’s lack of financial acumen, that bothers you—the somewhat inconsistent formatting of the numbers. Here’s an opportunity to refine an implementation while keeping the interface unchanged. The ostream class included member functions that control formatting. Without going into much detail, you can avoid e-notation by using the setf() method much as we did in Listing 8.8:

std::cout.setf(std::ios_base::fixed, std::ios_base::floatfield);

This sets a flag in the cout object instructing cout to use fixed-point notation. Similarly, the following statement causes cout to show three places to the right of the decimal when using fixed-point notation:

std::cout.precision(3);

You’ll find more details in Chapter 17, “Input, Output, and Files.”

Перейти на страницу:

Все книги серии Developer's Library

Похожие книги