Employee Class in C++ with Salary Calculation and Information Display Methods

In this exercise, you will create a C++ class called "Employee" with methods to calculate the salary and display employee information. The class will have attributes such as name, position, and hours worked, and the methods will calculate the salary based on the hourly rate and hours worked. Additionally, there will be a method to display the employee's information. This exercise will help you understand how to create and manipulate class objects and methods in C++.

Group

Object-Oriented Programming in C++

Objective

1. Define a class called "Employee" with attributes like name, position, hourly rate, and hours worked.
2. Implement a method called "calculateSalary" that calculates the salary based on the hourly rate and hours worked.
3. Implement another method called "displayInformation" to display the employee's name, position, and salary.
4. In the main function, create an instance of the Employee class and call the "calculateSalary" and "displayInformation" methods.

Create an Employee class with methods to calculate salary and display information.

Example C++ Exercise

 Copy C++ Code
#include <iostream>  // Include the standard input/output library
#include <string>  // Include the string library
using namespace std;

// Define the 'Employee' class
class Employee {
private:
    string name;  // Employee's name
    string position;  // Employee's position
    double hourlyRate;  // Employee's hourly rate
    double hoursWorked;  // Hours worked by the employee

public:
    // Constructor to initialize the Employee object
    Employee(string n, string p, double rate, double hours) {
        name = n;
        position = p;
        hourlyRate = rate;
        hoursWorked = hours;
    }

    // Method to calculate the salary based on hourly rate and hours worked
    double calculateSalary() {
        return hourlyRate * hoursWorked;  // Salary = hourly rate * hours worked
    }

    // Method to display the employee's information
    void displayInformation() {
        cout << "Employee Name: " << name << endl;
        cout << "Position: " << position << endl;
        cout << "Salary: $" << calculateSalary() << endl;
    }
};

int main() {
    // Create an Employee object with sample data
    Employee emp("John Doe", "Software Developer", 50.0, 160);  // Name, position, hourly rate, hours worked

    // Display the employee's information
    emp.displayInformation();  // Output: Employee Name, Position, and Salary

    return 0;  // Return 0 to indicate successful execution
}

 Output

Employee Name: John Doe
Position: Software Developer
Salary: $8000

Share this C++ Exercise


More C++ Programming Exercises of Object-Oriented Programming in C++

Explore our set of C++ Programming Exercises! Specifically designed for beginners, these exercises will help you develop a solid understanding of the basics of C++. From variables and data types to control structures and simple functions, each exercise is crafted to challenge you incrementally as you build confidence in coding in C++.