Warning: foreach() argument must be of type array|object, bool given in /var/www/html/web/app/themes/studypress-core-theme/template-parts/header/mobile-offcanvas.php on line 20

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 \(\cdot\) get \(_{-}\) profit () \(a_{-}\) profit \(=\) hyundai \(.\) get_profit () if (hyundai \(==\) jaguar) cout \( < < \) "Want to swap cars?"; hyundai = jaguar;

Short Answer

Expert verified
Answer: The allowed statements in the main part of the program are: 1. jaguar.set_price(30000.97) 2. double a_price, a_profit; 3. a_price = jaguar.get_price();

Step by step solution

01

Annotate and understand given code

We are given a class definition for an Automobile, which has member functions 'set_price()', 'set_profit()', 'get_price()', and 'get_profit()', and member variables 'price' and 'profit'. The code has the following structure: 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(); }; We notice that 'set_price' and 'get_price' functions and the member variables 'price' and 'profit' are public, while 'get_profit()' function is private.
02

Evaluate each given statement

We'll now analyze each given statement to see if it's allowed in the main part of the program: 1. hyundai.price = 4999.99 Since 'price' is a private member variable, it cannot be accessed directly from the main part of the program. This statement is NOT allowed. 2. jaguar.set_price(30000.97) This statement is using the public member function 'set_price()'. It is allowed in the main part of the program. 3. double a_price, a_profit; This statement is declaring two variables, 'a_price' and 'a_profit'. It is allowed in the main part of the program. 4. a_price = jaguar.get_price() This statement is using the public member function 'get_price()'. It is allowed in the main part of the program. 5. a_profit = jaguar.get_profit() This statement is using the private member function 'get_profit()'. It cannot be accessed from the main part of the program, so this statement is NOT allowed. 6. a_profit = hyundai.get_profit() This statement is also using the private member function 'get_profit()'. It cannot be accessed from the main part of the program, so this statement is NOT allowed. 7. if (hyundai == jaguar) cout << "Want to swap cars?"; There is no overloaded equality operator defined in the class definition, so we cannot compare two objects of the 'Automobile' class directly. This statement is NOT allowed. 8. hyundai = jaguar; This statement is attempting to assign one Automobile object to another. However, there is no overloaded assignment operator defined in the class definition. Therefore, this statement is NOT allowed. In conclusion, the allowed statements in the main part of the program are: 1. jaguar.set_price(30000.97) 2. double a_price, a_profit; 3. a_price = jaguar.get_price();

Unlock Step-by-Step Solutions & Ace Your Exams!

  • Full Textbook Solutions

    Get detailed explanations and key concepts

  • Unlimited Al creation

    Al flashcards, explanations, exams and more...

  • Ads-free access

    To over 500 millions flashcards

  • Money-back guarantee

    We refund you if you fail your exam.

Over 30 million students worldwide already upgrade their learning with Vaia!

Key Concepts

These are the key concepts you need to understand to accurately answer the question.

Private and Public Access Specifiers in C++
In C++, access specifiers define how the members of a class can be accessed. Understanding the difference between private and public access is crucial in object-oriented programming.

Public access specifiers allow members to be accessible from any part of the program. This means any code that has visibility of the object can call public member functions or access public variables. For example, the 'set_price()' and 'get_price()' functions in our Automobile class are public, which means they can be called from main or any other function.

Private access specifiers, on the other hand, restrict access to the members they protect. Only the member functions within the class can access private members. Attempting to directly access 'price' or 'profit' from outside the class, such as from the main function, will cause a compilation error because they're declared private in our Automobile class.

A common mistake for beginners is to attempt direct access to private members, as was shown in the exercise statement 'hyundai.price = 4999.99', which is not permitted.
Member Functions in C++
Member functions are at the heart of a class's behavior in C++. They define what an object of the class can do. Member functions can be either public or private, guiding how they can be used within your code.

Public member functions, such as 'set_price(double)' in the Automobile class, can be called on an object from anywhere in the code. These functions often serve as interfaces to interact with an object, allowing controlled manipulation of the object's internal state without exposing its implementation details.

Private member functions, like 'get_profit()' in the same example, are internal to the class. They can only be called by other member functions within the class — not from outside the class. These functions are typically helper functions that support the public interface of the class, providing a way to encapsulate functionality and enforce abstraction.
Class Member Accessibility
Class member accessibility is about who can utilize the functionalities and data within a class. It's a tool to implement encapsulation, one of the core principles of object-oriented programming.

The Automobile class in our exercise demonstrates how to control access to its internals. The 'price' and 'profit' variables are private, meaning they can only be modified within the class through member functions like 'set_price()'. In contrast, because 'get_price()' is public, it can be used externally to retrieve the price of an automobile without compromising the integrity of the private data.

Following the principle of least privilege, it's advisable to make all members private by default and only expose what is necessary as public. Doing this reduces the chance of unexpected errors from outside interference and makes the class safer and easier to maintain.
Object-Oriented Programming Principles
Object-oriented programming (OOP) is a paradigm based on the concept of 'objects', which can contain data in the form of fields (often known as attributes or properties) and code in the form of procedures (known as methods). Four main principles define OOP: encapsulation, inheritance, polymorphism, and abstraction.

  • Encapsulation: This is the concept of hiding the internal state of an object and requiring all interaction to be performed through the object's methods. It promotes modular design and reduces system complexity.
  • Inheritance: It allows a new class to take on the properties and methods of existing classes. This supports code reusability and simplifies maintenance.
  • Polymorphism: It allows for one interface to be used for a general class of actions. It simplifies programming by allowing the same operation to be used on different objects.
  • Abstraction: It involves hiding complex reality while exposing only the necessary parts. It reduces programming effort by eliminating the need to code for each type of hardware.

Our Automobile class is a step towards implementing these principles by encapsulating the price information, although to fully adhere to OOP, one could also introduce methods to handle car comparison and assignment.

One App. One Place for Learning.

All the tools & learning materials you need for study success - in one app.

Get started for free

Most popular questions from this chapter

Explain what publ ic: and private: do in a class definition. In particular, explain why we do not just make everything public: and save difficulty in access.

Suppose your friend defines an ADT as a C++ class in the way we described in Section \(10.3 .\) You are given the task of writing a program that uses this ADT. That is, you must write the main part of the program as well as any nonmember functions that are used in the main part of the program. The ADT is very long and you do not have a lot of time to write this program. What parts of the ADT do you need to read and what parts can you safely ignore?

The private member function DayofYear: :check_date in Display 10.4 allows some illegal dates to get through, such as February \(30 .\) Redefine the member function DayofYear: :check_date so that it ends the program whenever it finds any illegal date. Allow February to contain 29 days, so you account for leap years. (Hint: This is a bit tedious and the function definition is a bit long, but it is not very difficult.

Give a definition for the function with the following function declaration. The class BankAccount is defined in Display 10.5 double difference(BankAccount account1, BankAccount account2); / / Precondition: account1 and account2 have been given values // (that is, their member variables have been given values). // Returns the balance in account1 minus the balance in account2.

a. How many public: sections are required in a class for the class to be useful? b. How many private: sections are required in a class? c. What kind of section do you have between the opening \(\\{\) and the first public: or private: section label of a class? d. What kind of section do you have between the opening \(\\{\) and the first public: or private: section label of a structure?

See all solutions

Recommended explanations on Computer Science Textbooks

View all explanations

What do you think about this solution?

We value your feedback to improve our textbook solutions.

Study anywhere. Anytime. Across all devices.

Sign-up for free