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

(Employee Class) Create a class called Employee that includes three pieces of information as data membersa first name (type string), a last name (type string and a monthly salary (type int). [Note: In subsequent chapters, we'll use numbers that contain decimal points (e.g., 2.75 )called floating-point valuesto represent dollar amounts.] Your class should have a constructor that initializes the three data members. Provide a set and a get function for each data member. If the monthly salary is not positive, set it to \(\theta\). Write a test program that demonstrates class Employee's capabilities. Create two Employee objects and display each object's yearly salary. Then give each Employee a 10 percent raise and display each Employee's yearly salary again.

Short Answer

Expert verified
Define an Employee class with data members, create getter and setter methods, and test the functionality by applying raises and calculating yearly salaries.

Step by step solution

01

Define the Employee Class

Begin by defining a class named `Employee`. This class will encapsulate the data members and the methods for interacting with employee data.
02

Add Data Members

Inside the `Employee` class, declare the following data members: `first_name` (string), `last_name` (string), and `monthly_salary` (int). These fields will store the employee's personal information and salary.
03

Create the Constructor

Define a constructor for the `Employee` class that takes three parameters to initialize the data members: `first_name`, `last_name`, and `monthly_salary`. If `monthly_salary` is negative, set it to zero.
04

Implement Getter and Setter Methods

Provide getter and setter methods for each data member: - `get_first_name` and `set_first_name` - `get_last_name` and `set_last_name` - `get_monthly_salary` and `set_monthly_salary` (ensure monthly_salary cannot be set to a negative value).
05

Write a Test Program

Write a test program that: - Instantiates two `Employee` objects with sample data. - Displays each object's yearly salary by multiplying `monthly_salary` by 12.
06

Apply a 10% Raise to Each Employee

Calculate a 10% raise for each employee by increasing their `monthly_salary` by multiplying it by 1.10. Update the salary using the setter method.
07

Display Updated Yearly Salaries

Finally, display each employee's updated yearly salary after the raise. Again, compute it by multiplying the `monthly_salary` by 12.

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.

Class Definition
In C++ programming, a class is a blueprint for creating objects. It groups together data and functions that work on the data, allowing for structured and organized code. For our exercise, we are creating an `Employee` class.

The class definition includes the data members (or attributes) and member functions (or methods). In our `Employee` class, we define the data members `first_name`, `last_name`, and `monthly_salary`. These attributes store essential information about an employee.

To define a class, we use the `class` keyword followed by the class name. Inside, we declare variables to hold the data and outline the functions we'll need to interact with this data. A key part of class definition is deciding which data and functions should be accessible from the outside (public) and which should be hidden (private) to ensure controlled data manipulation.
Constructors in C++
Constructors in C++ are special class functions responsible for initializing objects. When a new object of a class is created, the constructor is called, setting initial values for the object's attributes. This makes constructors crucial for setting up necessary conditions for the object right from the start.

In our `Employee` example, the constructor takes three parameters: `first_name`, `last_name`, and `monthly_salary`. The constructor initializes the corresponding data members with these input values.

One important condition is checked during this initialization: if the `monthly_salary` is negative, it is set to zero. This safety measure ensures that no employee object ends up with an invalid, negative salary, maintaining the integrity of our data.
Getter and Setter Methods
Getter and setter methods are essential parts of a class, especially when we want to adhere to the principles of data encapsulation. A "getter" method retrieves the value of an attribute, while a "setter" method updates the value of an attribute.

For the `Employee` class, we have several getter methods:
  • `get_first_name()`
  • `get_last_name()`
  • `get_monthly_salary()`
These allow us to access the employee's first name, last name, and monthly salary.

Similarly, setter methods are:
  • `set_first_name(string name)`
  • `set_last_name(string name)`
  • `set_monthly_salary(int salary)` - with a check to ensure salary cannot be set to a negative value.
These setter methods cater to updating an employee's information, while maintaining control over the data being assigned.
Data Encapsulation
Data encapsulation is a core principle of object-oriented programming, which helps in organizing and protecting data within our classes. It refers to the bundling of data, along with methods that operate on that data, into a single unit known as a class.

In the `Employee` class, data encapsulation is achieved by defining our data members as private. This prevents direct access from outside the class. Instead, we use public getter and setter methods to view or modify these private attributes.

This controlled access helps prevent unauthorized modifications and keeps the object's data in a valid state. For instance, the `set_monthly_salary` method ensures that a negative value cannot be assigned to `monthly_salary`, demonstrating data encapsulation by protecting data integrity while still providing necessary interaction pathways.

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

What is a header file? What is a source-code file? Discuss the purpose of each.

(Date Class) Create a class called oate that includes three pieces of information as data membersa month (type int), a day (type int) and a year (type int). Your class should have a constructor with three parameters that uses the parameters to initialize the three data members. For the purpose of this exercise, assume that the values provided for the year and day are correct, but ensure that the month value is in the range 112 ; if it is not, set the month to \(1 .\) Provide a set and a get function for each data member. Provide a member function displayoate that displays the month, day and year separated by forward slashes ( \(/\) ). Write a test program that demonstrates class bate's capabilities.

(Invoice Class) Create a class called Invoice that a hardware store might use to represent an invoice for an item sold at the store. An Invoice should include four pieces of information as data membersa part number (type string \(),\) a part description (type string), a quantity of the item being purchased (type int) and a price per item (type int). [Note: In subsequent chapters, we'll use numbers that contain decimal points (e.g., 2.75 )called floating- point valuesto represent dollar amounts.] Your class should have a constructor that initializes the four data members. Provide a set and a get function for each data member. In addition, provide a member function named getInvoiceAmount that calculates the invoice amount (i.e., multiplies the quantity by the price per item), then returns the amount as an int value. If the quantity is not positive, it should be set to \(\theta\). If the price per item is not positive, it should be set to \(\theta .\) Write a test program that demonstrates class Invoice's capabilities.

Explain the purpose of a function parameter. What is the difference between a parameter and an argument?

Explain the difference between a function prototype and a function definition.

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