Listing 10.5 provides the method definitions for the stock program. It includes the stock10.h file in order to provide the class declaration. (Recall that enclosing the filename in double quotation marks instead of in brackets causes the compiler to search for it at the same location where your source files are located.) Also Listing 10.5 includes the iostream header file to provide I/O support. The listing also provides using declarations and qualified names (such as std::string) to provide access to various declarations in the header files. This file adds the constructor and destructor method definitions to the prior methods. To help you see when these methods are called, they each display a message. This is not a usual feature of constructors and destructors, but it can help you better visualize how classes use them.

Listing 10.5. stock10.cpp

// stock10.cpp -- Stock class with constructors, destructor added

#include

#include "stock10.h"

// constructors (verbose versions)

Stock::Stock()        // default constructor

{

    std::cout << "Default constructor called\n";

    company = "no name";

    shares = 0;

    share_val = 0.0;

    total_val = 0.0;

}

Stock::Stock(const std::string & co, long n, double pr)

{

    std::cout << "Constructor using " << co << " called\n";

    company = co;

    if (n < 0)

    {

        std::cout << "Number of shares can't be negative; "

                   << company << " shares set to 0.\n";

        shares = 0;

    }

    else

        shares = n;

    share_val = pr;

    set_tot();

}

// class destructor

Stock::~Stock()        // verbose class destructor

{

    std::cout << "Bye, " << company << "!\n";

}

// other methods

void Stock::buy(long num, double price)

{

     if (num < 0)

    {

        std::cout << "Number of shares purchased can't be negative. "

             << "Transaction is aborted.\n";

    }

    else

    {

        shares += num;

        share_val = price;

        set_tot();

    }

}

void Stock::sell(long num, double price)

{

    using std::cout;

    if (num < 0)

    {

        cout << "Number of shares sold can't be negative. "

             << "Transaction is aborted.\n";

    }

    else if (num > shares)

    {

        cout << "You can't sell more than you have! "

             << "Transaction is aborted.\n";

    }

    else

    {

        shares -= num;

        share_val = price;

        set_tot();

    }

}

void Stock::update(double price)

{

    share_val = price;

    set_tot();

}

void Stock::show()

{

    using std::cout;

    using std::ios_base;

    // set format to #.###

    ios_base::fmtflags orig =

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

    std::streamsize prec = cout.precision(3);

    cout << "Company: " << company

        << "  Shares: " << shares << '\n';

    cout << "  Share Price: $" << share_val;

    // set format to #.##

    cout.precision(2);

    cout << "  Total Worth: $" << total_val << '\n';

    // restore original format

    cout.setf(orig, ios_base::floatfield);

    cout.precision(prec);

}

A Client File

Listing 10.6 provides a short program for testing the new methods in the stock program. Because it simply uses the Stock class, this listing is a client of the Stock class. Like stock10.cpp, it includes the stock10.h file to provide the class declaration. The program demonstrates constructors and destructors. It also uses the same formatting commands invoked by Listing 10.3. To compile the complete program, you use the techniques for multifile programs described in Chapters 1 and 9.

Listing 10.6. usestok1.cpp

// usestok1.cpp -- using the Stock class

// compile with stock10.cpp

#include

#include "stock10.h"

int main()

{

  {

    using std::cout;

    cout << "Using constructors to create new objects\n";

    Stock stock1("NanoSmart", 12, 20.0);            // syntax 1

    stock1.show();

    Stock stock2 = Stock ("Boffo Objects", 2, 2.0); // syntax 2

    stock2.show();

    cout << "Assigning stock1 to stock2:\n";

    stock2 = stock1;

    cout << "Listing stock1 and stock2:\n";

    stock1.show();

    stock2.show();

    cout << "Using a constructor to reset an object\n";

    stock1 = Stock("Nifty Foods", 10, 50.0);    // temp object

    cout << "Revised stock1:\n";

    stock1.show();

    cout << "Done\n";

  }

    return 0;

}

Compiling the program represented by Listings 10.4, 10.5, and 10.6 produces an executable program. Here’s one compiler’s output from the executable program:

Using constructors to create new objects

Constructor using NanoSmart called

Company: NanoSmart  Shares: 12

  Share Price: $20.00  Total Worth: $240.00

Constructor using Boffo Objects called

Company: Boffo Objects  Shares: 2

  Share Price: $2.00  Total Worth: $4.00

Assigning stock1 to stock2:

Listing stock1 and stock2:

Company: NanoSmart  Shares: 12

  Share Price: $20.00  Total Worth: $240.00

Company: NanoSmart  Shares: 12

  Share Price: $20.00  Total Worth: $240.00

Using a constructor to reset an object

Constructor using Nifty Foods called

Bye, Nifty Foods!

Revised stock1:

Company: Nifty Foods  Shares: 10

  Share Price: $50.00  Total Worth: $500.00

Done

Bye, NanoSmart!

Bye, Nifty Foods!

Some compilers may produce a program with the following initial output, which has one additional line:

Using constructors to create new objects

Constructor using NanoSmart called

Company: NanoSmart  Shares: 12

  Share Price: $20.00  Total Worth: $240.00

Constructor using Boffo Objects called

Bye, Boffo Objects!                          << additional line

Company: Boffo Objects  Shares: 2

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

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

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