In this tutorial, we’ll build a simple C++ application for managing employment details. We’ll create classes for employees, managers, and departments, and explain each step with comments for beginners to understand.
1. Employee Class
#include <iostream>
#include <string>
using namespace std;
// Employee class represents individual employees in the organization
class Employee {
private:
int id; // Unique identifier for each employee
string name; // Name of the employee
double salary; // Salary of the employee
public:
// Constructor
Employee(int id, string name, double salary) {
this->id = id;
this->name = name;
this->salary = salary;
}
// Getters and Setters
int getId() {
return id;
}
void setId(int id) {
this->id = id;
}
string getName() {
return name;
}
void setName(string name) {
this->name = name;
}
double getSalary() {
return salary;
}
void setSalary(double salary) {
this->salary = salary;
}
// Method to display employee details
void displayDetails() {
cout << "Employee ID: " << id << endl;
cout << "Name: " << name << endl;
cout << "Salary: " << salary << endl;
}
};
Explanation:
- The
Employeeclass represents individual employees in the organization. - It has three private attributes:
id,name, andsalaryto store employee information. - The constructor initializes these attributes when a new employee object is created.
- Getter and setter methods are provided to access and modify the attributes.
- The
displayDetailsmethod prints the employee’s ID, name, and salary to the console.
2. Manager Class
#include <iostream>
#include <string>
#include "Employee.h" // Include the header file for the Employee class
using namespace std;
// Manager class represents managers in the organization
class Manager : public Employee {
private:
string department; // Department managed by the manager
public:
// Constructor
Manager(int id, string name, double salary, string department) : Employee(id, name, salary) {
this->department = department;
}
// Getter and Setter for department
string getDepartment() {
return department;
}
void setDepartment(string department) {
this->department = department;
}
// Overriding displayDetails method to include department
void displayDetails() {
Employee::displayDetails(); // Call the displayDetails method of the base class
cout << "Department: " << department << endl;
}
};
Explanation:
- The
Managerclass represents managers in the organization and inherits from theEmployeeclass. - It adds an additional private attribute
departmentto store the department managed by the manager. - The constructor initializes all attributes, including those inherited from the
Employeeclass. - Getter and setter methods are provided for the
departmentattribute. - The
displayDetailsmethod overrides the method in the base class to include the department information when displaying manager details.
3. Department Class
#include <iostream>
#include <string>
#include <vector>
#include "Employee.h" // Include the header file for the Employee class
using namespace std;
// Department class represents departments in the organization
class Department {
private:
string name; // Name of the department
vector<Employee*> employees; // Vector to store pointers to employees in the department
public:
// Constructor
Department(string name) {
this->name = name;
}
// Getter and Setter for name
string getName() {
return name;
}
void setName(string name) {
this->name = name;
}
// Method to add an employee to the department
void addEmployee(Employee* employee) {
employees.push_back(employee);
}
// Method to display department details
void displayDetails() {
cout << "Department Name: " << name << endl;
cout << "Employees:" << endl;
for (Employee* emp : employees) {
emp->displayDetails();
}
}
};
Explanation:
- The
Departmentclass represents departments in the organization. - It has two private attributes:
nameto store the department name andemployeesto store pointers toEmployeeobjects in the department. - The constructor initializes the department name.
- Getter and setter methods are provided for the
nameattribute. - The
addEmployeemethod adds an employee to the department’s list of employees by storing a pointer to the employee. - The
displayDetailsmethod prints the department name and the details of all employees in the department by iterating through the vector of employee pointers.
Written By
Welcome to DevTechTutor.com, your ultimate resource for mastering web development and technology! Whether you're a beginner eager to dive into coding or an experienced developer looking to sharpen your skills, DevTechTutor.com is here to guide you every step of the way. Our mission is to make learning web development accessible, engaging, and effective.
Other Articles
Previous