I have this code that makes a list of employee where there are also some manager, the differences between a normal employee and a manager is that a manager can get a Bonus that must be added to his salary, but this sum must be done from an overloading of operator +, so these are the header files where you can find: clas Employee, subclass Manager, class Node, class LinkedList and Main class.
Employee
#ifndef EMPLOYEE_H
#define EMPLOYEE_H
#include<string>
using namespace std;
class Employee
{
private:
string name;
int salary;
public:
Employee(string n, int s){
name = n;
salary = s;
}
virtual ~Employee() {}
string getName(){
return name;
}
int getSalary(){
return salary;
}
virtual void PrintInfo(){
cout << "'" << getName() << "'" << "--" << getSalary() << endl;
}
void setSalary(int n){
this->salary = n;
}
};
class Manager:public Employee
{
private:
int bonus;
public:
Manager(string name,int salary,int bonus): Employee(name, salary){
this->bonus = bonus;
}
virtual ~Manager() {}
int getBonus(){
return bonus;
}
void PrintInfo(){
int salaryTot;
salaryTot = getSalary() + getBonus();
cout << "'" << getName() << "'" << " -- " << getSalary() << " -- " << getBonus() << " -- " << salaryTot << endl;
}
};
#endif // EMPLOYEE_H
--Node
#ifndef NODO_H
#define NODO_H
#include"Employee.h"
class Nodo
{
private:
Nodo *Next; // int next
Employee *data; // data
public:
Nodo():Next{nullptr}{};
virtual ~Nodo() {
delete data;
}
Nodo *SetNext(Nodo *ptr){
Next = ptr;
return Next;
}
Nodo *getNext(){
return Next;
}
void SetEmployee(Employee *data){
this->data = data;
}
Employee *getEmployee(){
return data;
}
};
#endif // NODO_H
--LinkedList
#ifndef LLIST_H
#define LLIST_H
#include "Employee.h"
#include "Nodo.h"
class LinkedList{
private:
Nodo *First;
Nodo *Last;
public:
LinkedList():First(nullptr), Last(nullptr){}
~LinkedList();
void addNodo(Employee *tmp);
Nodo *getFirst(){
return First;
}
Nodo *getLast(){
return Last;
}
};
void LinkedList::addNodo(Employee *data){
Nodo *tmp = new Nodo();
tmp->SetEmployee(data);
if(!First){
First = tmp;
Last = tmp;
}
else
Last = Last->SetNext(tmp);
}
LinkedList::~LinkedList(){
Nodo *tmp = getFirst();
while(tmp){
Nodo *del = tmp;
tmp = tmp->getNext();
delete del;
}
}
#endif // LLIST_H
--Main
#include <iostream>
#include <string>
#include"LinkedList.h"
using namespace std;
int main(){
Employee *emp1=new Employee(string("Adam Sandler"),1000);
Employee *emp2=new Employee(string("Lionel Messi"),2000);
Employee *emp3=new Manager(string("Cristiano Ronaldo"),2000,2000);
Employee *emp4=new Manager(string("Ed Sheeran"),3000,3000);
//Create an Employee List
LinkedList list;
list.addNodo(emp1);
list.addNodo(emp2);
list.addNodo(emp3);
list.addNodo(emp4);
//Print of every employee info
Nodo *stmp;
int sommaTot = 0;
stmp = list.getFirst();
while (stmp != nullptr){
stmp->getEmployee()->PrintInfo();
sommaTot = sommaTot + stmp->getEmployee()->getSalary();
stmp = stmp->getNext();
}
cout << "The total is: " << sommaTot << endl;
return 0;
}
Now, what should I do to implement the + operator overload to make the sum of all the salaries (including those of the employees who are managers where I must also add the bonus)?
The output of this code is:
'Adam Sandler'--1000
'Lionel Messi'--2000
'Cristiano Ronaldo' -- 2000 -- 2000 -- 4000
'Ed Sheeran' -- 3000 -- 3000 -- 6000
The total is: 8000
Aucun commentaire:
Enregistrer un commentaire