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

Consider the following statements: class temporary { public: void set(string, double, double); void print(); double manipulate(); void get(string&, double&, double&); void setDescription(string); void setFirst(double); void setSecond(double); string getDescription() const; double getFirst()const; double getSecond()const; temporary(string = "", double = 0.0, double = 0.0); private: string description; double first; double second; }; a. How many members does class temporary have? b. How many private members does class temporary have? c. How many constructors does class temporary have? Can this constructor be used to initialize an object without specifying any parameters? If yes, then illustrate with an example; otherwise, explain why it cannot be used to initialize an object witout specifying any parameters.

Short Answer

Expert verified
Class temporary has 14 members, 3 private members, and 1 constructor which can be used without parameters.

Step by step solution

01

Identify the Total Members

A class member can be a data member or a member function. In the class `temporary`, identify the total members by counting both the data members and member functions. - Data Members: `description`, `first`, and `second` - Member Functions: `set`, `print`, `manipulate`, `get`, `setDescription`, `setFirst`, `setSecond`, `getDescription`, `getFirst`, `getSecond`, and the constructor `temporary`. So, the total number of class members is 3 (data members) + 11 (member functions) = 14.
02

Count the Private Members

Private members are explicitly listed under the `private` access specifier. In class `temporary`, the private data members are `description`, `first`, and `second`. There are no private member functions mentioned explicitly related to these specific data members. Thus, the number of private members is 3.
03

Identify and Describe the Constructors

A constructor is a special type of member function used to initialize objects. The class `temporary` has one constructor definition as `temporary(string = "", double = 0.0, double = 0.0)`. This constructor can be called with default parameters because each parameter has a default value specified. Since all parameters have default values, this constructor can be used to initialize an object without specifying any parameters.
04

Provide an Example for Default Constructor Usage

Illustrate the use of the default constructor without parameters. ```cpp // Creating an object of class temporary without passing any arguments // This will invoke the constructor with default parameters temporary tempObject; ```

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.

Data Members
Data members are variables that hold the data specific to a class in C++. They define the properties of objects created from the class. In the class `temporary`, the data members include:
  • description: A string storing a descriptive text.
  • first: A double representing the first numerical value.
  • second: Another double holding the second numerical value.
These members are crucial as they store the state of an object. They are declared in the private section of the class to protect them from unintended modifications from outside the class itself. Access to these data members is typically managed through member functions.
Member Functions
Member functions in C++ are designed to manipulate data members and define class behaviors. They allow interaction with class data safely and effectively. In the `temporary` class, member functions include:
  • set(): Assigns values to the data members.
  • print(): Outputs the details of the object's state.
  • manipulate(): Performs calculations or modifications involving data members.
  • get(): Retrieves current values of the data members.
  • setDescription(), setFirst(), setSecond(): These set specific data members.
  • getDescription(), getFirst(), getSecond(): Retrieve the values of specific data members.
  • The temporary constructor initializes objects.
These functions can modify or provide access to the data while maintaining the integrity and security of the internal state.
Default Constructor
A default constructor is a special member function that initializes objects of a class, setting up initial values for data members. It is called automatically when an object is instantiated with no initial arguments. In the `temporary` class, the constructor is defined as:\[\text{temporary(string = "", double = 0.0, double = 0.0)}\]Each parameter has a default value, allowing the constructor to initialize an object without needing any explicit arguments. The use of default values makes it highly versatile. For instance, creating an instance of `temporary` can be done simply by writing:```cpptemporary tempObject;```This line creates a `temporary` object with `description` set to an empty string, and both `first` and `second` initialized to 0.0. This facilitates quick initialization where specific values aren't necessary or yet available.
Access Specifiers
Access specifiers in C++ control how class members are accessed. They determine the visibility and accessibility of data members and member functions. In our `temporary` class, there are two primary access specifiers:
  • public: Members declared under it are accessible from outside the class. For the `temporary` class, all member functions are public, allowing them to manipulate or retrieve data.
  • private: Limits access solely within the class itself. Only member functions or friends of the class can access private members. In `temporary`, description, first, and second are private to prevent unauthorized external modification.
Through careful use of these specifiers, a class can encapsulate its data and ensure that its internal state remains controlled and predictable. This separation is a cornerstone of object-oriented programming, promoting data integrity and security.

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

Find the syntax errors in the following class definition: class discover //Line 1 { //Line 2 public; //Line 3 void set(string, int, int); //Line 4 void print() const; //Line 5 discover(); //Line 6 discover(string, int, int); //Line 7 bool discover(string, int, int); //Line 8 private: //Line 9 string type; //Line 10 int l; //Line 11 int w; //Line 12 } //Line 13

b. ! c. ~ d. \( # Which of the following characters appears before a destructor’s name? a. # b. ! c. ~ d. \)

Consider the definition of the following class: class employee //Line 1 { //Line 2 public: //Line 3 employee(); //Line 4 employee(string, int, double); //Line 5 employee(int, double); //Line 6 employee(string); //Line 7 void setData(string, int, double); //Line 8 void print() const; //Line 9 void updatePay(double x); //Line 10 int getNumOfServiceYears() const; //Line 11 double getPay() const; //Line 12 private: //Line 13 string name; //Line 14 int numOfServiceYears; //Line 15 double pay; //Line 16 }; //Line 17 a. Give the line number containing the constructor that is executed in each of the following declarations: i. employee tempEmployee; ii. employee newEmployee("Harry Miller", 0, 25000); iii. employee oldEmployee("Bill Dunbar", 15, 55000); b. Write the definition of the constructor in Line 4 so that the instance variables are initialized to "", 0, and 0.0, respectively. c. Write the definition of the constructor in Line 5 so that the instance variables are initialized according to the parameters. d. Write the definition of the constructor in Line 6 so that the instance variable name is initialized to the empty string and the remaining instance variables are initialized according to the parameters.

Write the definition of a class that has the following properties: a. The name of the class is secretType. b. The class secretType has four member variables: name of type string, age and weight of type int, and height of type double. c. The class secretType has the following member functions. (Make each accessor function constant.) print—outputs the data stored in the member variables with the appropriate titles setName—function to set the name setAge—function to set the age setWeight—function to set the weight setHeight—function to set the height getName—value-returning function to return the name getAge—value-returning function to return the age getWeight—value-returning function to return the weight getHeight—value-returning function to return the height constructor—with default parameters: The default value of name is the empty string " ", and the default values of age, weight, and height are 0. d. Write the definition of the member functions of the class secretType, as described in Part c.

What is the main difference between a struct and a class?

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