Need help on implement a derived class Employee

Need help on implement a derived class Employee

Page 1 of 1

7 Replies - 2112 Views - Last Post: 10 December 2010 - 05:11 AM Rate Topic: -----

#1 JohnnyBoy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 26-September 10

Need help on implement a derived class Employee

Posted 08 December 2010 - 07:20 PM

I have to derive a Employee class from the base class Person. For some reason, there is something wrong for one of my function "void Employee::set_numDepartment(int num)". The program said: "operator=" is ambiguous. And for my operator << function, it cannot access the private members. Hope you guys can help me correct this. Here is my code: please take a look. Thanks.
#ifndef _EMPLOYEE_H
#define _EMPLOYEE_H

#include "person.h"

class Employee:public Person
{
    private:
		int employeeID;
		double salary;
		string *department;
		int numDepartment;

	public:
		
		 Employee(); //default:
		 Employee(int newID,string newName,DayOfYear date,int empID,double newSalary);
			
		 //big 3	
		 ~Employee(); //destructor 
		 Employee(const Employee& emp); //copy constructor
		 void operator=(const Employee& rhs);
		
		 int get_employeeID()const; //return employeeID
		 double get_salary()const; //return salary
		 int get_numDepartment()const; //return numDepartment
		 string get_department(int i)const; //return department[i]

		 void set_employeeID(int id); //id-->employeeID
		 void set_salary(double Sal); //Sal-->ssalary;
		 void set_numDepartment(int num);//num->numDepartment, allocate memory to *department
		 void set_department(int i, string dep);//dep-->department[i]

		 friend ostream & operator <<(ostream & out,const Employee emp);
		 virtual void output()const; //print out a employee's info
};
#endif //_EMPLOYEE_H 

#include "employee.h"
//#include "dayOfYear.h"

//default constructor 
Employee::Employee():Person()
{
	employeeID = 0;
	salary = 0.0;
	numDepartment = 0;
	department = NULL;
}

//a second constructor
Employee::Employee(int newID,string newName,DayOfYear date,int empID,double newSalary):Person(newID,newName,date),employeeID(empID),salary(newSalary),numDepartment(0),department(NULL)
{
	//call the corresponding constructor in the base class to 
	//initialize the inherited members
	/*  employeeID = empID; 
		salary = newSalary;
		numDepartment = 0;
		department = NULL; 
	*/ 
}

//destructor
Employee::~Employee()
{
	if (department !=NULL){
		delete [] department;
		department = NULL;
	}
}

//copy constructor
Employee::Employee(const Employee & emp):Person(emp) //call the copy constructor of the base class
{
	employeeID = emp.employeeID;
	salary = emp.salary;
/* 	
	if (numDepartment == 0)
		department = NULL;
	else
	{
		department = new string [numDepartment]; */ 
	
	if (numDepartment <= 0)
		department = NULL;
	else{ //allocate space to *department and copy the department from emp
		(*this).department = new string [numDepartment];
		if (department ==NULL){
			cerr << "Employee:Employee(const Employee &): Memory allocation error\n";
			exit(-1);
		}  
		for (int i = 0; i < numDepartment; i++)
			department[i] = emp.department[i]; 
	}
}

void Employee::operator=(const Employee & rhs)
{
	(*this).Person::operator=(rhs);
	employeeID = rhs.employeeID;
	salary = rhs.salary;

	/*if (numDepartment > 0)
		delete department;
	numDepartment = rhs.numDepartment;
	department = new string [numDepartment];*/ 

	
	if (numDepartment !=rhs.numDepartment){
		delete [] department;
		numDepartment = rhs.numDepartment;
		department = new string [numDepartment];
		if (department == NULL){
			cerr <<"Employee:operator=(const Employee &):Memory allocation error\n";
			exit(-1);
		} 
		for ( int i = 0; i < numDepartment; i++)
			department[i] = rhs.department [i]; 
	}
	
}

int Employee::get_employeeID()const
{
	return employeeID;
}

double Employee::get_salary()const
{
	return salary;
}

int Employee::get_numDepartment() const
{
	return numDepartment;
}

string Employee::get_department(int i)const 
{
	if(i>=0 && i <numDepartment)
		return department[i];
	else 
		return "NA";
}

void Employee::set_employeeID(int id)
{
	employeeID = id;
}

void Employee::set_salary(double Sal)
{
	salary = Sal;
}

void Employee::set_numDepartment(int num)
{
	numDepartment = num;
	if (num > 0){
		department = new string [num];
		if(department == NULL){
			cerr<< "Employee:set_numDepartment(int ): Memory allocation error\n";
			exit (-1);
		}
		for (int i = 0; i < num; i++)
			department[i] = NULL;
	} 
}

void Employee::set_department(int i, string dep)
{
	if ( i >=1 && 1<=numDepartment)
		department[i-1] = dep;
}

ostream& operator<<(ostream& out, const Employee& emp)
{
	DayOfYear bday;
	bday = emp.get_birthday();

	out << "------------------------\n";
	out << "id="<<emp.get_id()<<endl;
	out << "name=" <<emp.get_name() << endl;	
	out << "birthday=" <<bday.get_day();
	out << "-"<<bday.get_month();
	out <<"-"<<bday.get_year() << endl;
	out << "#emails=" << emp.get_num_emails()<<endl;
	out << " 1st email=" << emp.getEmail(0) << endl;
	
	out << "EmployeeID" <<emp.employeeID<< endl;
	out << "salary="<<emp.salary<< endl;
	out << "#Department=" <<emp.numDepartment<< endl;
	for ( int i = 1; i <= emp.numDepartment;i++)
		out << "department[" << i << "]=" <<emp.department[i-1]<<" ";
	out << endl;
	out << "----------------------------\n";
	return out;
}


void Employee::output() const
{
   DayOfYear bday;
   bday = get_birthday();
   
   cout << "------------------------------\n";
   cout << "id=" << get_id() <<endl;
   cout   << "name=" <<  get_name() << endl;//private-is-private rule 
   cout << "birthday=" << bday.get_day(); 
   cout << "-" << bday.get_month();
   cout << "-" << bday.get_year() << endl;
   cout << "#emails=" <<get_num_emails()<<endl;
   cout << " 1st email=" << getEmail(0)<<endl;
   cout << "EmployeeID" <<employeeID << endl;
   cout << "salary="<<salary<< endl;
   cout << "#Department=" <<numDepartment<< endl;
	for ( int i = 1; i <=numDepartment;i++)
   cout << "department[" << i << "]=" <<department[i-1]<<" ";
   cout << endl;
   cout << "----------------------------\n";

}







Is This A Good Question/Topic? 0
  • +

Replies To: Need help on implement a derived class Employee

#2 OnlineCop  Icon User is offline

  • D.I.C Head

Reputation: 19
  • View blog
  • Posts: 73
  • Joined: 16-April 10

Re: Need help on implement a derived class Employee

Posted 08 December 2010 - 09:10 PM

For line 22, you have this:
void operator=(const Employee& rhs);

Now, if you were to have a function such as Employee newEmployee = oldEmployee, then the operator= would need to return something. Instead, it's returning void. You will want to return the Employee type, not 'void'.
Was This Post Helpful? 0
  • +
  • -

#3 jimblumberg  Icon User is offline

  • member icon

Reputation: 5303
  • View blog
  • Posts: 16,520
  • Joined: 25-December 09

Re: Need help on implement a derived class Employee

Posted 08 December 2010 - 09:16 PM

In your class definition:

friend ostream & operator <<(ostream & out,const Employee emp);



The last parameter should be a reference parameter.

Jim
Was This Post Helpful? 0
  • +
  • -

#4 JohnnyBoy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 26-September 10

Re: Need help on implement a derived class Employee

Posted 08 December 2010 - 09:41 PM

View Postjimblumberg, on 08 December 2010 - 08:16 PM, said:

In your class definition:

friend ostream & operator <<(ostream & out,const Employee emp);



The last parameter should be a reference parameter.

Jim


Thx this is very helpful lol. a really little mistake i didnt find it out:)
Was This Post Helpful? 0
  • +
  • -

#5 JohnnyBoy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 26-September 10

Re: Need help on implement a derived class Employee

Posted 08 December 2010 - 09:50 PM

View PostOnlineCop, on 08 December 2010 - 08:10 PM, said:

For line 22, you have this:
void operator=(const Employee& rhs);

Now, if you were to have a function such as Employee newEmployee = oldEmployee, then the operator= would need to return something. Instead, it's returning void. You will want to return the Employee type, not 'void'.


Thx for your comment, i will try it later. If i still get any problem i will post here again:)
Was This Post Helpful? 0
  • +
  • -

#6 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon


Reputation: 6966
  • View blog
  • Posts: 14,572
  • Joined: 16-October 07

Re: Need help on implement a derived class Employee

Posted 09 December 2010 - 05:55 AM

Ok, so your department is actually a dynamic list of strings? Any reason not to use a vector? If not, use a vector.

The methods get_numDepartment() and get_department(int) make sense, but set_numDepartment(int) and set_department(int, string) are questionable. If I'm adding a department, why do I care about the position in the array. More, why should I be able to redefine the number of departments at runtime via a public method? I would prefer to see something like add_department(string) and remove_department(string) instead.

You current set_numDepartment is a memory leak:
numDepartment = num; // wait, how many were the before?
if (num > 0) {
	department = new string [num]; // if I had any, I just blew them away, lost reference to them, and created a memory leak



For the =, I'd prefer a "copy constructor instead... Wait, you have a copy constructor. Why do you need an =?

Your operator<< and output() do the same thing? Stop that. Reuse your code. The operator<< needn't be friend. You can do this:

class Employee:public Person {
	// ...
	public:
		 virtual void output(ostream & out) const; //print out a employee's info
		 virtual void output()const; //print out a employee's info
};
ostream & operator <<(ostream & out,const Employee &emp);
#endif //_EMPLOYEE_H 

ostream & operator <<(ostream & out,const Employee &emp) { emp.output(out); return out; }

void Employee::output() const { emp.output(std::cout); }


Was This Post Helpful? 0
  • +
  • -

#7 JohnnyBoy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 26-September 10

Re: Need help on implement a derived class Employee

Posted 09 December 2010 - 09:52 PM

View Postbaavgai, on 09 December 2010 - 04:55 AM, said:

Ok, so your department is actually a dynamic list of strings? Any reason not to use a vector? If not, use a vector.

The methods get_numDepartment() and get_department(int) make sense, but set_numDepartment(int) and set_department(int, string) are questionable. If I'm adding a department, why do I care about the position in the array. More, why should I be able to redefine the number of departments at runtime via a public method? I would prefer to see something like add_department(string) and remove_department(string) instead.

You current set_numDepartment is a memory leak:
numDepartment = num; // wait, how many were the before?
if (num > 0) {
	department = new string [num]; // if I had any, I just blew them away, lost reference to them, and created a memory leak



For the =, I'd prefer a "copy constructor instead... Wait, you have a copy constructor. Why do you need an =?

Your operator<< and output() do the same thing? Stop that. Reuse your code. The operator<< needn't be friend. You can do this:

class Employee:public Person {
	// ...
	public:
		 virtual void output(ostream & out) const; //print out a employee's info
		 virtual void output()const; //print out a employee's info
};
ostream & operator <<(ostream & out,const Employee &emp);
#endif //_EMPLOYEE_H 

ostream & operator <<(ostream & out,const Employee &emp) { emp.output(out); return out; }

void Employee::output() const { emp.output(std::cout); }



Yeah.. i know there is something weird for those set_numDepartment(int) and set_Department ~I gonna redo this two function change to add or remove and let see:). So, i just use a operator << is fine ? and dont have to use the output at the same time?
Was This Post Helpful? 0
  • +
  • -

#8 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon


Reputation: 6966
  • View blog
  • Posts: 14,572
  • Joined: 16-October 07

Re: Need help on implement a derived class Employee

Posted 10 December 2010 - 05:11 AM

View PostJohnnyBoy, on 09 December 2010 - 10:52 PM, said:

So, i just use a operator << is fine ? and dont have to use the output at the same time?


The reason you would need to friend the operator << is if it needed access to private ( or protected ) parts of your class. However, if you have a public const method that already does the printing job, then the operator << doesn't need to be friended. The << can just use the public method.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1