Programming and Graphics

(Kiana) #1

188 Introduction to C++ Programming and Graphics


Running the program prints on the screen:


tulip black $4.99
annual red flower for $6
perennial garden yellow $9.39

Note that a flower uses the flower-classprintfunction, and a rose uses the
rose-classprintfunction.


To further illustrate this distinction, we endow the flower class with the
function:


void flower::printprice() const
{
cout <<"PRICE: $"<< price << endl;
}

and the derived rose class with the same-named function:


void rose::printprice() const
{
cout << "OUR PRICE: $" << price << endl;
}

We may then state in the main code:


flower A = flower();
A.printprice();
rose W = rose("perennial", "garden", "yellow", 9.39);
W.printprice();

Running the code produces on the screen:


PRICE: $4.99
OUR PRICE: $9.39

If we had implemented theprintpricefunction only in the flower class and
not theprintpricefunction in the rose class, the output of the code would
have been:


PRICE: $4.99
PRICE: $9.39
Free download pdf