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

When you define an ADT as a C++ class, should you make the member variables public or private? Should you make the member functions public or private?

Short Answer

Expert verified
In general, when defining an ADT using a C++ class, member variables should have the private access specifier to ensure data encapsulation, and member functions should mainly have the public access specifier, allowing external parts of the program to interact with the ADT. However, private member functions may be used internally by the class as needed.

Step by step solution

01

Understanding Access Specifiers in C++ Classes

Access specifiers determine the scope and visibility of class members (variables and functions) in C++ programming. The main access specifiers are public, private, and protected. Public members are accessible from any part of the program, private members can only be accessed within the class, and protected members can be accessed within the class and its subclasses.
02

Deciding on Access Specifiers for Member Variables

In an ADT (Abstract Data Type) implementation using a C++ class, it's common practice to make the member variables private. Making member variables private ensures data encapsulation and prevents accidental modification of the data from external parts of the program. By keeping member variables private, we maintain a clear separation of concerns between the interface and the implementation of the ADT.
03

Deciding on Access Specifiers for Member Functions

Member functions should generally be made public, especially if they form part of the interface used by external code to interact with the ADT. Making member functions public allows external parts of the program to use these functions to perform operations on the data within the class. In some cases, you may also have private member functions that are used internally by the class, which should not be accessible from external parts of the program.
04

Conclusion

When defining an ADT as a C++ class, you should make member variables private and the member functions mainly public (with private functions used internally as needed). This approach ensures proper data encapsulation and provides a clean interface for interacting with the ADT.

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!

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

Give a definition for the function with the following function declaration. The class BankAccount is defined in Display 10.5 BankAccount new_account(BankAccount old_account); //Precondition: old_account has previously been given a value // (that is, its member variables have been given values). //Returns the value for a new account that has a balance of zero / / and the same interest rate as the old_account. For example, after this function is defined, a program could contain the following: BankAccount account3, account4; account3.set(999, 99, 5.5) ; account4 \(=\) new_account(account 3) account4.output(cout); This would produce the following output:

How does inheritance support code reuse and make code easier to main\(\operatorname{tain} ?\)

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;

Define a function called send_line that takes one argument that is an output stream. When called, send_line reads one line of input from the keyboard and outputs the line to the output stream given as its argument. You should be able to call your function using either cout or an outputfile stream as the argument to your function send_line. (If the argument is an output-file stream, then the stream is connected to a file before the function is called, so send_line will not open or close any files.) For example, the first of the following calls to send_line copies a line from the keyboard to the file morestuf.dat, and the second copies a line from the keyboard to the screen: ofstream fout; fout.open("morestuf.dat") cout \(<<\) "Enter 2 lines of input: \(\backslash n "\) send_line(fout); send_line(cout)

Write a definition for a structure type for records consisting of a person's wage rate, accrued vacation (which is some whole number of days), and status (which is either hourly or salaried). Represent the status as one of the two char values 'H' and 'S'. Call the type EmployeeRecord.

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