Grupo
Programación Orientada a Objetos en C++
Ojetivo
1. Define una clase llamada "Empleado" con atributos como nombre, puesto, tarifa por hora y horas trabajadas.
2. Implementa un método llamado "calculateSalary" que calcule el salario basándose en la tarifa por hora y las horas trabajadas.
3. Implementa otro método llamado "displayInformation" para mostrar el nombre, el puesto y el salario del empleado.
4. En la función principal, crea una instancia de la clase "Empleado" y llama a los métodos "calculateSalary" y "displayInformation".
Crea una clase "Empleado" con métodos para calcular el salario y mostrar información.
Ejemplo de Código C++
Mostrar Código C++
#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
}
Salida
Employee Name: John Doe
Position: Software Developer
Salary: $8000
Comparte este ejercicio C++