Huzaifa ..OOP ASSIGNMENT 6

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 12

LAB ASSIGNMENT: #6

1. Create a class called "Employee" with the following private


attributes:
a) Employee_name
b) Employee_id
c) Employee_salary
d) Service_length
e) Annual_allowance
Employee_id can not be changed during the entire service period,
declare it as constant and must be assigned at object creation.Assign
values to all the variables (except salary) at object creation
However, the salary can be changed with service. The salary can be
calculated using the following formula
“Salary= 50000+service_length*5000+annual_allowance” Write a
function to calculate and display the salary:
#include <iostream>
#include <string>

using namespace std;

class Employee {
private:
string name;
const int id;
int service_length;
int allowance;
double salary;

public:
void calculateSalary() {
salary = 50000 + service_length * 5000 + allowance;
}

Employee(string n, int i, int s, int a) : name(n), id(i), service_length(s),


allowance(a) {
calculateSalary();
}

void displayDetails() const {


cout << "Name: " << name << "\nID: " << id << "\nService Length: " <<
service_length
<< "\nAllowance: " << allowance << "\nSalary: " << salary << endl;
}

void updateServiceLength(int s) {
service_length = s;
calculateSalary();
}
void updateAllowance(int a) {
allowance = a;
calculateSalary();
}
};

int main() {
Employee emp("Huzaifa Ali Khan",1,2, 15000);
emp.displayDetails();
emp.updateServiceLength(6);

emp.updateAllowance(15000);
cout<<"------updated allowance and service length-----"<<endl;
emp.displayDetails();

return 0;
}
2. Change your program in (1) to generate unique employee id for
each employee along with total number of employees.
#include <iostream>
#include <string>

using namespace std;

class Employee {
private:
string name;
const int id; // Unique ID for each employee
int service_length;
int allowance;
double salary;
// Static member to keep track of the total number of employees
static int employeeCount;

public:
// Constructor to initialize employee data
Employee(string n, int s, int a) : name(n), id(++employeeCount),
service_length(s), allowance(a) {
calculateSalary();
}

// Method to calculate salary based on service length and allowance


void calculateSalary() {
salary = 50000 + service_length * 5000 + allowance;
}

// Method to display employee details


void displayDetails() const {
cout << "Name: " << name << "\nID: " << id << "\nService Length: " <<
service_length
<< "\nAllowance: " << allowance << "\nSalary: " << salary << endl;
}

// Method to update service length and recalculate salary


void updateServiceLength(int s) {
service_length = s;
calculateSalary();
}

// Method to update allowance and recalculate salary


void updateAllowance(int a) {
allowance = a;
calculateSalary();
}
};

// Initialize static member variable


int Employee::employeeCount = 0;

int main() {
Employee emp1("Huzaifa ",2, 5000);
emp1.displayDetails();

Employee emp2("M.Arraik Ali", 4, 15000);


emp2.displayDetails();

emp1.updateServiceLength(5);
emp1.updateAllowance(18000);
cout << "|------ Updated allowance and service length for Huzaifa Ali Khan
-----|" << endl;
emp1.displayDetails();
return 0;
}

3 : Change the program in (1) such that it counts number of


employees in the company while recording other details. Add a
mechanism to set and display employee records as different
functions (make display function as constant). You need to update
the record of employee count, when first two employees leave the
organization. Need to show the results for at least 5 employees:
#include <iostream>
#include <string>

using namespace std;

class Employee {
private:
string name;
const int id; // Unique ID for each employee
int service_length;
int allowance;
double salary;

// Static member:
static int employeeCount;

// Function to calculate salary based on service length and allowance


void calculateSalary() {
salary = 50000 + service_length * 5000 + allowance;
}

public:
// Constructor to initialize employee data
Employee(string n, int s, int a) : name(n), id(++employeeCount),
service_length(s), allowance(a) {
calculateSalary();
}

// Function to display employee details (constant function)


void displayDetails() const {
cout << "Name: " << name
<< "\nID: " << id
<< "\nService Length: " << service_length
<< "\nAllowance: " << allowance
<< "\nSalary: " << salary << endl;
}

// Static function to get the current employee count


static int getEmployeeCount() {
return employeeCount;
}

// Function to update service length and recalculate salary


void updateServiceLength(int s) {
service_length = s;
calculateSalary();
}

// Function to update allowance and recalculate salary


void updateAllowance(int a) {
allowance = a;
calculateSalary();
}

// an employee leaving the company


static void employeeLeaves() {
if (employeeCount > 0) {
--employeeCount; // Decrement the employee count
}
}
};

// Initialize static member


int Employee::employeeCount = 0;

int main() {
// Create (5 employees)
Employee emp1("Arraik", 3, 10000);
Employee emp2("ALI", 5, 15000);
Employee emp3("umar", 2, 8000);
Employee emp4("huzaifa", 4, 12000);
Employee emp5("ahmad", 6, 18000);

// Display details of the employees


cout << "Employee 1 details:\n";
emp1.displayDetails();
cout << "\nEmployee 2 details:\n";
emp2.displayDetails();
cout << "\nEmployee 3 details:\n";
emp3.displayDetails();
cout << "\nEmployee 4 details:\n";
emp4.displayDetails();
cout << "\nEmployee 5 details:\n";
emp5.displayDetails();

// Display total employee


cout << "\nTotal employees in the company: " <<
Employee::getEmployeeCount() << endl;

// first two employees leaving


Employee::employeeLeaves();
Employee::employeeLeaves();

// Display updated employee:


cout << "\nAfter two employees leave, total employees: " <<
Employee::getEmployeeCount() << endl;

return 0;
}

You might also like