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

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?

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)

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

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.

Define a function called copy_char that takes one argument that is an input stream. When called, copy_char will read one character of input from the input stream given as its argument and will write that character to the screen. You should be able to call your function using either cin or an input-file stream as the argument to your function copy_char. (If the argument is an input-file stream, then the stream is connected to a file before the function is called, so copy_char will not open or close any files.) For example, the first of the following two calls to copy_char will copy a character from the file stuff. dat to the screen, and the second will copy a character from the keyboard to the screen: ifstream fin; fin.open("stuff.dat") copy_char(fin) copy_char(cin)

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