The class SalariedEmployee inherits both of the functions get_name and print_check (among other things) from the base class Employee, yet only the function declaration for the function print_check is given in the definition of the class SalariedEmployee. Why isn't the function declaration for the function get_name given in the definition of SalariedEmployee?

Short Answer

Expert verified
Inheritance allows a derived class to inherit properties and methods from the base class. The SalariedEmployee class inherits both get_name and print_check functions from the base class Employee. However, if the derived class wants to modify the behavior of an inherited method, it needs to provide a new definition for that method, which is called method overriding. In this case, the SalariedEmployee class overrides the print_check function with a new definition. On the other hand, the SalariedEmployee class doesn't require any modification to the get_name function. Since the behavior of the get_name function doesn't need to be modified, there is no need to provide a function declaration for it in the SalariedEmployee class, and it can directly use the inherited get_name function from the base Employee class.

Step by step solution

01

Inheritance is a mechanism in object-oriented programming that enables a new class to inherit properties and methods of an existing class. The new class is called a derived class or subclass, and the existing class is the base class or superclass. In this exercise, SalariedEmployee is a derived class and Employee is the base class. When a derived class is created, it automatically inherits the properties and methods of its base class. With this knowledge, let's look into the problem statement about why the function declaration for the function get_name is not given in the definition of SalariedEmployee. #Step 2: Understand the base class and its functions#

The base class Employee contains the functions get_name and print_check. These functions are part of the Employee class and will be automatically inherited by any classes that extend the Employee class. In this exercise, the SalariedEmployee class extends or inherits the Employee class, which means both functions, get_name and print_check, should be available in this derived class. #Step 3: Analyze why only the function declaration for print_check is given in the derived class#
02

When a derived class inherits a base class, it can modify or create new behaviors for the inherited properties and methods. If the derived class wants to modify the behavior of an inherited method, it must provide a new definition for that method in the derived class. This is called method overriding. In this exercise, the SalariedEmployee class wants to modify the behavior of the print_check function; therefore, its declaration is given in the SalariedEmployee class with a new definition. This new definition will match the specific behavior required for the SalariedEmployee instances. #Step 4: Clarify why the function declaration for get_name is not given in the derived class#

When a derived class doesn't need to modify the behavior of an inherited method, it can simply use the method provided by the base class without needing to include its own function declaration. In this case, the SalariedEmployee class doesn't require any modification to the get_name function, and hence, there is no need to provide the function declaration for get_name in the SalariedEmployee class. The instances of SalariedEmployee class can directly use the inherited get_name function from the Employee base class, and it will work as expected.

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

Why can't we assign a base class object to a derived class variable?

include and using directives are added)? void show_employee_data(const Employee object): int main( ) \(l\) HourlyEmpl… # Is the following program legal (assuming appropriate #include and using directives are added)? void show_employee_data(const Employee object): int main( ) \(l\) HourlyEmployee joe("Mighty Joe", \\[ \left." 123-45-6789^{\prime \prime}, \quad 2 \theta \cdot 5 \theta, \quad 4 \theta\right) \\] SalariedEmployee boss("Mr. Big Shot", "987-65-4321", 10500.50) ; show_employee_data(joe) show_employee_data(boss) return \(\theta\) \\} void show_employee_data(const Employee object) \\{ cout \(<<\) "Name: \("<<\) object.get_name( ) \(<<\) endl; cout \(<<\) "Social Security Number: \&< object.get_ssn( ) \(<<\) endl;

Suppose the base class and the derived class each have a member function with the same signature. When you have a pointer to a base class object and call a function member through the pointer, discuss what determines which function is actually called-the base class member function or the derived-class function.

Give the definitions for the member function add_value, the copy constructor, the overloaded assignment operator, and the destructor for the following class. This class is intended to be a class for a partially filled array. The member variable number_used contains the number of array positions currently filled. The other constructor definition is given to help you get started. #include

Suppose Child is a class derived from the class Parent, and the class Grandchild is a class derived from the class Child. This question is concerned with the constructors and destructors for the three classes Parent, Child, and Grandchild. When a constructor for the class Grandchild is invoked, what constructors are invoked and in what order? When the destructor for the class Grandchild is invoked, what destructors are invoked and in what order?

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