I have instantiated an object with arguments( string, float, float)

The two floating numbers are initialized using the constructor but when I display my object using my print function instead of getting the two floating numbers I entered, I get 0.00 for each.

#ifndef Publication_H
#define Publication_H

#include <string>
using std::string;

class Publication
{
public:
    Publication( const string &, float = 0.00 );

    void setBookTitle( const string & ); //function to set book's title
    string getBookTitle() const; // function to retrieve book's title

    void setBookPrice( const float  ); // function to set book's price
    float getBookPrice() const; // function to retrieve book's price

    void print() const;

private:
    string bookTitle;
    float bookPrice;
};

#endif
#include <iostream>
using std::cout;
using std::endl;

#include "Publication.h"

Publication::Publication( const string &title, float price)
: bookTitle( title )
{
    setBookPrice( price );
}

void Publication::setBookTitle(const string &title )
{
    bookTitle = title;
}

string Publication::getBookTitle() const
{
    return bookTitle;
}

void Publication::setBookPrice( const float price )
{
    bookPrice = ( price > 0.00 ) ? 0.00 : price;
}

float Publication::getBookPrice() const
{
    return bookPrice;
}

void Publication::print() const
{
    cout << "Wayman Publishing\n Books in Circulation:"
        << "\nTitle: " << getBookTitle() << "\nPrice: "<< "$" << getBookPrice() << endl;
}

Notice here I instantiate Tape audio( "Lord of the Rings", 19.95, 246.50 ); but the 19.95 and 246.50 display 0.00

#include <iostream>
using std::cout;
using std::cin;
using std::endl;
using std::fixed;

#include <iomanip>
using std::setprecision;

#include "book.h"
#include "tape.h"

int main()
{
    Tape audio( "Lord of the Rings", 19.95, 246.50 );
    Book book1( "Terrorism in the Air", 64.99, 343);

    cout << fixed << setprecision( 2 );

    audio.setBookPrice( 19.95 );
    audio.setPlayingTime( 261.15 );
    audio.print();
    book1.print();
}

> but the 19.95 and 246.50 display 0.00
Perhaps it's because of this bizarre line of code

> bookPrice = ( price > 0.00 ) ? 0.00 : price;
If the price is greater that zero, set it to zero.

Salem was right it was the obscure code but I copied from my textbook and just tried to morph it into my program. I guess I don't really understand what the previous code was trying to do, any ideas?

in the book it has a funciton defined as

void CommissionEmployee::setGrossSales( double sales )
{
      grossSales = ( sales < 0.0 ) ? 0.0 : sales;
}

Edited 3 Years Ago by Dani: Formatting fixed

> grossSales = ( sales < 0.0 ) ? 0.0 : sales;
Is a shorter version of this.

if ( sales < 0.0 ) {
    grossSales = 0.0
} else {
    grossSales = sales;
}

Most compilers would regard them as being equivalent anyway, so go for the clarity.

This article has been dead for over six months. Start a new discussion instead.