Im writing a function that should receive a list of book objects as an argument. In each book object there is a private data member price. The function is suppose to compare the prices of each book and return the book with the highest price.
//Client program
#include <iostream>
#include "Book.h"
#include "textbook.h"
#include "Name.h"
#include "unsorted.h"
using namespace std;
int main()
{
book b1("The Exception to the Rulers", "Amy", "Goodman", "Hyperion", 342, "1-4013-0131", 21.95,'N'); // this is the title, authors first & last name, publisher, number of pages, isbn number, price, and code.
book b2("Who moved my cheese", "Spencer", "Johnson", "Red Tree", 95, "0-399-14446-3", 19.99, 'H');
book b3("Hellbound Hearts", "Neil", "Gaiman", "Dark Harvest", 326, "978-1-4391-4090-1", 16.00, 'F');
UnsortedType L1; // creating a list "L1" with the default vaule lengh 0
L1.InsertItem(b1); // populating the list with the first book
L1.InsertItem(b2); // populating the list with the second book
L1.InsertItem(b3); // populating the list with the third book
In the main im not really sure how to pass the actual list “L1” or the contents of L1 to the function that will compare the prices. I guess im getting confused because in order to call the function getMostExpensive I would do some thing like:
L1.getMostExpensive();
but if i call my function with L1 do I have to pass any args, and if not then how do I access the private data member price inside of the function getMostExpensive()?
Why should
pricebe a private member ofbook? Seems to me like “everyone” should be able to know a book’s price…If you can indeed make it public, why not use a simpler
std::vector<book>with a free functiongetMostExpensive()?If you must keep
priceprivate, you can makeUnsortedTypeafriendofbook:in which case
L1has access to the privates ofbook.My default rule of thumb however is that whenever I need
friend‘s, there’s a flaw in my design :pYou could also use a getter/setter approach: