Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8799067
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T00:14:07+00:00 2026-06-14T00:14:07+00:00

Im writing a function that should receive a list of book objects as an

  • 0

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()?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T00:14:08+00:00Added an answer on June 14, 2026 at 12:14 am

    Why should price be a private member of book? 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 function getMostExpensive()?

    #include <vector>
    
    ...
    
    std::vector<book> L1; 
    
    L1.push_back(b1); // include first book
    L1.push_back(b2); // include second book
    L1.push_back(b3); // include third book
    
    ...
    
    // free function
    book getMostExpensive(const std::vector<book>& b) {
    
        double maxPrice=0;
        unsigned int maxInd;
        for(unsigned int i=0; i<b.size(); ++i){
            if (b[i].price > maxPrice){
                maxInd = i;
                maxPrice = b[i].price;
            }
        }
        return b[maxInd];
    }
    

    If you must keep price private, you can make UnsortedType a friend of book:

    class UnsortedType;
    class book {
        ...
        friend class UnsortedType;
        ...
    };
    

    in which case L1 has access to the privates of book.

    My default rule of thumb however is that whenever I need friend‘s, there’s a flaw in my design :p

    You could also use a getter/setter approach:

    class book 
    {
    private:
        double _price; // the actual price
    
    public: 
        const double& price // read-only copy of price
    
        // constructor
        book(...);
    
        // price-setter
        void setPrice(double newPrice);
    
    };
    
    // constructor initializes const reference to private member _price
    book::book(...) : price(_price) {...}
    
    void book::setPrice(double newPrice) { _price = newPrice>=0.0?newPrice:0.0; }
    
    ...
    
    int main(...){
        book b;
        ...
        double P = b.price;  // valid
        b.price = 56.8;      // NOT valid; compile-time error
    
        b.setPrice(56.8);    // valid; this is the only way to set the price
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing a .hpp file for a class that should receive a function as
I'm writing a control that should be able to display any list of data.
Suppose I'm writing a function that takes a list of integers and returns only
I am writing a function that takes in an argument. From that argument, I
How would i go about writing a function that would handle the data in
I'm writing a function in x86 assembly that should be callable from c code,
I have an array of objects. I am interested in writing a function that
I am writing function that involve other function from base R with a lot
I'm writing a function that replaces long hex coded color ( #334455 ) with
I am writing a function that takes in a output target file and a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.