Design a class called Employee. Separate the declaration and methods definition file. The class
should store the following employee information:
name: employee's name which is a pointer points to a C-string
eid: employee ID, type is string
salary: type is double
Define the following member functions:
Constructors: default constructor, parameterized constructor with name, eid, and salary as
parameters, and a copy constructor.
Destructor
Mutator functions: setName(char *name) , setEid(string eid), setSalary(double s)
Accessor functions: getName(), getEid(), getSalary()
Overload assignment operator: overload the assignment operator '=' for deep copy, and make
sure it allows multiple assignment statements, for example e1=e2=e3
Use Lab08.cpp to test your class.
Here is Lab08.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
|
#include "Employee.h"
#include<iostream>
#include<string>
using namespace std;
int main()
{
Employee e1("John","e222", 60000), e2(e1), e3, e4;
e3 = e4 = e2;
e2.setName("Michael");
e2.setSalary(75000);
e3.setName("Aaron");
e3.setSalary(63000);
e4.setName("Peter");
cout << "\nName: " << e1.getName() << "\nID: " << e1.getID() << "\nSalary: " << e1.getSalary() << endl;
cout << "\nName: " << e2.getName() << "\nID: " << e2.getID() << "\nSalary: " << e2.getSalary() << endl;
cout << "\nName: " << e3.getName() << "\nID: " << e3.getID() << "\nSalary: " << e3.getSalary() << endl;
cout << "\nName: " << e4.getName() << "\nID: " << e4.getID() << "\nSalary: " << e4.getSalary() << endl;
return 0;
}
| |
I'm having an issue saying this after the program runs it's reading this
lab8(25397,0x100396340) malloc: *** error for object 0x100002ed0: pointer being freed was not allocated
*** set a breakpoint in malloc_error_break to debug
(lldb)
I'm not sure how to get rid of it, but I believe it has something to do with my deconstructor.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
|
//
// Employee.h
// Test
//
// Created by Jayme Woodfill on 4/2/18.
// Copyright © 2018 Jayme Woodfill. All rights reserved.
//
#ifndef Employee_h
#define Employee_h
#include <iostream>
#include <string>
using namespace std;
class Employee
{
private:
char *name; //Points to a C-string
string eid; //employees id
double salary; //salary
int SIZE = 40;
public:
//Default Constructor
Employee(){
eid = "0";
salary = 0.0;
//Don't know what to set here
}
//Parameteized Constructor
Employee(char *n, string id, double sal)
{
eid = id;
salary = sal;
name = new char[SIZE];
for(int i = 0; i < SIZE; i++)
name[i] = n[i];
}
//Copy Constructor
Employee(const Employee &obj)
{
eid = obj.eid;
salary = obj.salary;
name = new char[SIZE];
for(int i = 0; i < SIZE; i++)
name[i] = obj.name[i];
}
//Destructor
~Employee()
{ delete [] name;}
//Sets the name
void setName(char *n)
{
name = n;
}
//Get's the name
string getName() const
{
return name;
}
//Set the employees id number
void setEid(string id)
{ eid = id; }
//Get the employees id number
string getID () const
{return eid;}
//Set the salary
void setSalary(double s)
{ salary = s;}
//Get salary
double getSalary() const
{ return salary;}
//Get a name
char getName(int index) const
{ return name[index];}
//Overloaded = operator
const Employee operator=(const Employee &right)
{
delete [] name;
eid = right.eid;
salary = right.salary;
name = new char[SIZE];
for(int i = 0; i < SIZE; i++)
name[i] = right.name[i];
return *this;
}
};
#endif /* Employee_h */
| |