(Employee Class) Create a class called Employee that includes three pieces of information as data members - a first name (type string), a last name (type string) and a monthly salary (type int). \([\text {Note: In subsequent chapters, we'll use numbers that contain decimal points (e.g., } 2.75)-\) called floating-point values - to 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 0. Write a test program that demonstrates class Employee's capabilities. Create two Employee objects and display each object's yean \(y\) 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 private attributes for first name, last name, and monthly salary. Provide a constructor, set/get functions, and methods to calculate and update the yearly salary. In the main function, demonstrate this functionality with two Employee instances, display their salaries, apply raises, and then display the updated salaries.

Step by step solution

01

Define the Employee Class

Begin by defining a class called 'Employee'. Within the class, declare three private data members: 'firstName', 'lastName', and 'monthlySalary'. All of which should be appropriately typed according to the specifications (string for the names and int for the salary). It's important to make these private to encapsulate the data and ensure that it can only be accessed through the class methods.
02

Create the Constructor

Create a constructor for the Employee class that takes three parameters (for the first name, last name, and monthly salary) and initializes the data members. In the constructor, also include a check to ensure that the 'monthlySalary' is positive. If it isn't, set it to 0.
03

Implement Set and Get Functions

Provide public member functions (setters and getters) for each of the three data members. The setters should include a check for the 'monthlySalary' to ensure that it does not get set to a negative value.
04

Define a Method to Calculate Yearly Salary

Add a member function to the Employee class that calculates the yearly salary. Simply multiply the 'monthlySalary' by 12 to obtain this. Additionally, ensure that the value is non-negative as per the previous steps.
05

Create a Method to Increase Salary

Write a method in the Employee class to increase the employee's salary by a certain percentage. This method will accept a percentage value and then apply this increase to the 'monthlySalary'.
06

Write Test Code

Create a test program ('main' function) where you instantiate two Employee objects with given names and salaries. Print out each employee's yearly salary. Then, apply a 10 percent raise using the method created in step 5 and print out the new yearly salaries for both employees.

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.

C++ Object-Oriented Programming
C++ is a versatile programming language that supports Object-Oriented Programming (OOP), a paradigm that structures programs using objects representing real-world entities. In OOP, objects are instances of classes, which define the attributes and behaviors that the objects can have.

Through encapsulation, inheritance, and polymorphism, C++ enables developers to create versatile and powerful programs. For instance, in the Employee Class exercise, encapsulation is used to protect the integrity of the data by making it private and only accessible through member functions. Using classes, C++ programmers can model real-world entities such as employees, giving structure and organization to complex programs.
Class Constructors
A constructor in C++ is a special member function that initializes objects of the class. Constructors share the name with the class and have no return type, not even void. They are called automatically when an object is created.

In our Employee Class example, the constructor receives first name, last name, and monthly salary as parameters and sets the class’s data members accordingly. If the monthly salary is not positive, the constructor initializes it to 0, ensuring the data remains in a valid state right from the object's creation.
Encapsulation in C++
Encapsulation is a fundamental principle of OOP, referring to the bundling of data with the methods that operate on that data. It restricts direct access to some of an object's components, which is a preventative measure against unintended interference and misuse.

In the Employee class, encapsulation is demonstrated by marking the data members as private. This means they cannot be accessed or modified directly from outside the class, and all interaction must go through public member functions (setters and getters). Encapsulation enhances the maintenance and flexibility of the code while promoting security and data integrity.
Member Functions
Member functions or methods are functions defined inside a class that manipulate an object's internal state. These include constructor, accessor (getters), mutator (setters), and any other functions that perform operations on class data.

In the provided solution, member functions include the getters and setters for employee’s names and salary, a function to calculate the yearly salary, and another to apply a raise. The encapsulation ensures that these member functions are the only way to interact with the private data members of the Employee class.
Data Abstraction
Data abstraction can be thought of as an extension of encapsulation—it involves providing only the necessary details to the outside world while hiding the implementation details. The concept allows programmers to change the internal workings of a class without affecting any external code that uses the class.

In our exercise, data abstraction is implied in the way members’ salaries are processed. The inner workings of the salary calculations and raise applications are hidden behind member functions. Users of the Employee class need not know how the yearly salary is calculated; they only need to know which function to call to get the result.

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

(Date Class) Create a class called Date that includes three pieces of information as data members - a 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 \(1-12\); if it isn't, set the month to \(1 .\) Provide a set and a get function for each data member. Provide a member function displayDate that displays the month, day and year separated by forward slashes (/). Write a test program that demonstrates class Date's capabilities.

(Set and Get Functions) Explain why a class might provide a set function and a get function for a data member.

Fill in the blanks in each of the following: a) Every class definition contains the keyword __ followed immediately by the class's name. b) A class definition is typically stored in a file with the __ filename extension. c) Each parameter in a function header specifies both a(n) __ and \(a(n)\) __. d) When each object of a class maintains its own copy of an attribute, the variable that represents the attribute is also known as a(n) __. e) Keyword public is a(n) __. f) Return type __ indicates that a function will perform a task but will not return any information when it completes its task. g) Function __ from the \(<\) string \(>\) library reads characters until a newline character is encountered, then copies those characters into the specified string. h) When a member function is defined outside the class definition, the function header must include the class name and the __ followed by the function name to "tie" the member function to the class definition. i) The source-code file and any other files that use a class can include the class's header via \(a(n)\) __ preprocessor directive.

State whether each of the following is true or false. If false, explain why. a) By convention, function names begin with a capital letter and all subsequent words in the name begin with a capital letter. b) Empty parentheses following a function name in a function prototype indicate that the function does not require any parameters to perform its task. c) Data members or member functions declared with access specifier private are accessible to member functions of the class in which they're declared. d) Variables declared in the body of a particular member function are known as data members and can be used in all member functions of the class. e) Every function's body is delimited by left and right braces ( \(\\{\text { and }\\}\) ). f) Any source-code file that contains int main () can be used to execute a program. g) The types of arguments in a function call must be consistent with the types of the corresponding parameters in the function prototype's parameter list.

(Default Constructor) What's a default constructor? How are an object's data members initialized if a class has only an implicitly defined default constructor?

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