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
Show 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