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

Find the error(s) in each of the following and explain how to correct it (them): a. Assume the following prototype is declared in class Time: void ~Time( int ); b. The following is a partial definition of class Time: class Time { public: // function prototypes private: int hour = 0; int minute = 0; int second = 0; }; // end class Time c. Assume the following prototype is declared in class Employee: int Employee( const char *, const char * );

Short Answer

Expert verified
Part A: Remove 'void' and 'int'. Part B: Use a constructor for initialization. Part C: Remove 'int' from the constructor.

Step by step solution

01

Identify the Error in Part A

In part (a), the prototype is for a destructor. In C++, destructors do not have a return type and are declared with a tilde (~) followed by the class name. Thus, "void" before "~Time(int);" is incorrect. Destructors do not take parameters, so "int" should also be removed.
02

Correct the Error in Part A

To correct the error, remove the "void" type and the parameter from the prototype: "~Time();". This correctly declares a destructor without a return type and no parameters.
03

Identify the Error in Part B

In part (b), the issue is with the initialization of member variables directly in the class body (before the constructor definition), which was not allowed before C++11. For this declaration to be universally correct, initialization should occur in a constructor.
04

Correct the Error in Part B

To be compatible with all versions of C++, move the default initialization of member variables to the constructor of the class Time: ```cpp class Time { public: Time() : hour(0), minute(0), second(0) {} private: int hour; int minute; int second; }; // end class Time ``` This code declares a constructor to initialize the variables when a `Time` object is created.
05

Identify the Error in Part C

In part (c), the function prototype "int Employee(const char *, const char *);" is incorrect because constructors do not have a return type. This format suggests a constructor due to the same name as the class, but it incorrectly includes an "int" return type.
06

Correct the Error in Part C

To correct this error, remove the "int" return type from the constructor prototype: ```cpp Employee(const char *, const char *); ``` This properly declares a constructor for the Employee class which initializes using two string parameters.

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.

Destructor Definition
In C++, a destructor is a special member function used to perform clean-up operations when an object of a class is destroyed. The key characteristics of destructors include:
  • No return type. Destructors never return a value, unlike other functions. Including a return type is an error, as seen in the example "void ~Time(int);" where "void" should not precede the destructor.
  • Declaration using a tilde (~) prefix followed by the class name. This specific syntax helps the compiler identify the function as a destructor.
  • No parameters. Destructors are called automatically when an object goes out of scope or is deleted and should not accept any arguments.
When correcting a destructor prototype, it typically looks like this: `~ClassName();`. This ensures that your destructor adheres to the rules of not having return types or parameters, simplifying its automatic invocation processes.
Member Variable Initialization
Member variable initialization refers to setting initial values for variables defined within a class. Pre-C++11, variables in a class could not be initialized directly at the point of declaration, which would lead to errors if done so:
  • Variables had to be assigned values within a constructor. This is because the constructor is called upon object creation, ensuring the variables are initialized.
  • Post-C++11, in-class initialization has been allowed. However, for backward compatibility and ensuring broader code accessibility, relying on constructors for initialization can be favorable.
In the example "class Time", initializing with a constructor ensures compatibility: ``` class Time { public: Time() : hour(0), minute(0), second(0) {} private: int hour; int minute; int second; }; ``` This approach uses an initializer list with the constructor, which efficiently assigns initial values to member variables.
Constructor Prototypes
Constructors are fundamental to creating objects in C++. Like destructors, constructors have specific rules:
  • No return type. This is crucial: constructors are not standard functions and should not return any value. Including a return type like "int" is incorrect and will cause errors, as observed in the "int Employee(const char *, const char *);" example.
  • Same name as the class. A constructor needs to share its name with the class it belongs to, indicating its purpose to initialize new objects of the class.
  • Can have parameters. Constructors can be overloaded, allowing for different initializations depending on how and with what data an object is created.
A corrected constructor prototype for class `Employee` should look like: `Employee(const char*, const char*);`. The absence of a return type and matching the function name with the class ensures it fulfills its role of initializing objects correctly.

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

(Rational Class) Create a class called Rational for performing arithmetic with fractions. Write a program to test your class. Use integer variables to represent the private data of the classthe numerator and the denominator. Provide a constructor that enables an object of this class to be initialized when it is declared. The constructor should contain default values in case no initializers are provided and should store the fraction in reduced form. For example, the fraction \(\frac{2}{4}\) would be stored in the object as 1 in the numerator and 2 in the denominator. Provide public member functions that perform each of the following tasks: a. Adding two Rational numbers. The result should be stored in reduced form. b. Subtracting two Rational numbers. The result should be stored in reduced form. c. Multiplying two Rational numbers. The result should be stored in reduced form. d. Dividing two Rational numbers. The result should be stored in reduced form. e. Printing Rational numbers in the form a/b, where a is the numerator and b is the denominator. f. Printing Rational numbers in floating-point format.

(Tictactoe Class) Create a class Tictactoe that will enable you to write a complete program to play the game of tic-tac-toe. The class contains as private data a 3 -by-3 two-dimensional array of integers. The constructor should initialize the empty board to all zeros. Allow two human players. Wherever the first player moves, place a 1 in the specified square. Place a 2 wherever the second player moves. Each move must be to an empty square. After each move, determine whether the game has been won or is a draw. If you feel ambitious, modify your program so that the computer makes the moves for one of the players. Also, allow the player to specify whether he or she wants to go first or second. If you feel exceptionally ambitious, develop a program that will play three-dimensional tic-tac-toe on a \(4-b y-4-b y-4\) board. [Caution: This is an extremely challenging project that could take many weeks of effort!]

(Rectangle Class) Create a class Rectangle with attributes length and width, each of which defaults to \(1 .\) Provide member functions that calculate the perimeter and the area of the rectangle. Also, provide set and get functions for the length and width attributes. The set functions should verify that length and width are each floating-point numbers larger than 0.0 and less than 20.0.

What is the purpose of the scope resolution operator?

(HugeInteger Class) Create a class Hugetnteger that uses a 40 -element array of digits to store integers as large as 40 digits each. Provide member functions input, output, add and substract. For comparing HugeInteger objects, provide functions isEqualTo, isNotEqualTo, isGreaterThan, isLessThan, isGreaterThan0rEqualTo and isLessThan0rEqualtoeach of these is a "predicate" function that simply returns TRue if the relationship holds between the two HugeIntegers and returns false if the relationship does not hold. Also, provide a predicate function is zero. If you feel ambitious, provide member functions multiply, divide and modulus.

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