SHARE
SPREAD
HELP

The Tradition of Sharing

Help your friends and juniors by posting answers to the questions that you know. Also post questions that are not available.


To start with, Sr2Jr’s first step is to reduce the expenses related to education. To achieve this goal Sr2Jr organized the textbook’s question and answers. Sr2Jr is community based and need your support to fill the question and answers. The question and answers posted will be available free of cost to all.

 

#
Authors:
Walter Savitch ,julia Lobur
Chapter:
Defining Classes
Exercise:
Self-test Exercises
Question:15 | ISBN:9780321531346 | Edition: 7

Question

Suppose your program contains the following class definition:

class Automobile

{

public:

void set_price(double new_price);

void set_profit(double new_profit);

double get_price( );

private:

double price;

double profit;

double get_profit( );

};

and suppose the main part of your program contains the following

declaration and that the program somehow sets the values of all the

member variables to some values:

Automobile hyundai, jaguar;

Which of the following statements are then allowed in the main part of

your program?

hyundai.price = 4999.99;

jaguar.set_price(30000.97);

double a_price, a_profit;

a_price = jaguar.get_price( );

a_profit = jaguar.get_profit( );

a_profit = hyundai.get_profit( );

if (hyundai == jaguar)

cout << "Want to swap cars?";

hyundai = jaguar;

TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

Let's analyze each statement to determine whether it is allowed in the main part of the program:

  1. hyundai.price = 4999.99;

    • Allowed: Since the price member variable is declared as public, it can be directly accessed and modified.
  2. jaguar.set_price(30000.97);

    • Allowed: The set_price() function is a public member function, which allows setting the price member variable.
  3. double a_price, a_profit;

    • Allowed: This statement declares two double variables a_price and a_profit.
  4. a_price = jaguar.get_price();

    • Allowed: The get_price() function is a public member function, which allows accessing the price member variable.
  5. a_profit = jaguar.get_profit();

    • Not allowed: The get_profit() function is declared as private, which means it can only be accessed from within the class and not from outside.
  6. a_profit = hyundai.get_profit();

    • Not allowed: Similar to the previous statement, get_profit() is a private member function, so it cannot be accessed from outside the class.
  7. if (hyundai == jaguar) cout << "Want to swap cars?";

    • Not allowed: There is no equality operator (==) defined for the Automobile class, so you cannot directly compare two Automobile objects using ==.
  8. hyundai = jaguar;

    • Not allowed: The assignment operator (=) is not defined explicitly for the Automobile class, so you cannot directly assign one Automobile object to another.

In summary, the allowed statements are:

  • hyundai.price = 4999.99;
  • jaguar.set_price(30000.97);
  • double a_price, a_profit;
  • a_price = jaguar.get_price();

Please note that the above analysis assumes the program sets the member variables price and profit to public access. If they are private, then direct access to price would not be allowed, and you would need to use the public member functions to set and retrieve their values.

0 0

Discussions

Post the discussion to improve the above solution.