Dec 9, 2011 at 7:08pm UTC
Hi,
I have 3 classes:Company,Department and Employees.Company has collection of departments and each department has collection of employees.
In the department i push_back the elements:
1.HR 2.Technical
In the Employees i added names and service:
For HR DEPT i added two employees:
S,2
Y,3
For Techincal dept added 1 employee:
Z ,3
With my code i am able to get the total service of first dept(2+3=5).
Now i need to add 3 to that total service of first department.So the total output should be(5+3=8) in order to get the total experience of all employees of all departments.
What will be the appropriate way to get the expected output?
Dec 9, 2011 at 8:03pm UTC
Below is the code.Thanks in advance.
Company.h
#include "Department.h"
#include<iostream>
#include<vector>
//#include<list>
class Company
{
public:
Company();
~Company();
void add();
void exp() const;
private:
std::vector<Department*>depts;
};
Company.cpp
#include "Company.h"
#include<iostream>
#include<string>
using namespace std;
Company::Company()
{
}
Company::~Company()
{
}
//Adding department Name and Count of Faculty
void Company::add()
{
int f;string name;
Department* d;
d=new Department();
cout<<"Enter the dept";
getline(cin,name);
d->setDeptName(name);
cout<<"Enter the Employee count";
cin>>f;
cin.ignore();
d->setEmployeeCount(f);
depts.push_back(d);
d->add();
d->add();
}
//Calculating the total experience
void Company::exp() const
{
vector<Department*>::const_iterator iter;
for(iter=depts.begin();iter!=depts.end();++iter)
{
(*iter)->totalExperience();
}
}
Department.h
#include "Employee.h"
#include<iostream>
#include<vector>
class Department
{
public:
Department(const std::string& n="",int c=0);
~Department();
void setDeptName(const std::string& n);
void setEmployueeCount(int c);
std::string getDeptName() const;
int getEmployeeCount() const;
void add();
double totalExperience() const;
private:
std::string deptname;
int count;
std::vector<Employuee*>emps;
}
Department.cpp
#include "Department.h"
#include<iostream>
#include<string>
using namespace std;
Department::Department(const string& n,int c)
{
setDeptName(n);
setEmployeeCount(c);
}
Department::~Department(void)
{
}
void Department::setEmployeeCount(int c)
{
count=c;
}
int Department::getEmployeeCount() const
{
return count;
}
void Department::setDeptName(const string& n)
{
deptname=n;
}
string Department::getDeptName() const
{
return deptname;
}
void Department::add()
{
int f;string name;
Employee* e;
e=new Employee();
cout<<"Enter the Employee Name"<<endl;
getline(cin,name);
e->setName(name);
cout<<"Enter the Experience"<<endl;
cin>>f;
cin.ignore();
e->setYears(f);
emps.push_back(e);
}
double Department::totalExperience() const
{
int count=0;
vector<Employee*>::const_iterator iter;
for(iter=emps.begin();iter!=emps.end();++iter)
{
count=count+(*iter)->getYears();
}
return count;
}
Employee.h
#include<iostream>
class Employee
{
public:
Employee(const std::string& n="",int s=0);
~Employee();
void setYears(int s);
int getYears() const;
void setName(const std::string& n);
std::string getName() const;
private:
std::string empName;
int service;
};
Employee.cpp
#include "Employee.h"
#include<iostream>
using namespace std;
Employee::Employee(const string& n,int s)
{
setName(n);
setYears(s);
}
Employee::~Employee(void)
{
}
void Employee::setYears(int s)
{
service=s;
}
int Employee::getYears() const
{
return service;
}
void Employee::setName(const string& n)
{
facultyName=n;
}
string Employee::getName() const
{
return facultyName;
}