So here's the thing...I'm still learning to deal with classes, I was told to do this program for class and so far I think I'm doing well, but i got completely block when it told me to write a code that creates three objects. I'm not exactly sure how to do that. Can someone help me out?
The instructions and the code are below so you guys can see what I've done so far. I'd like to learn as much as possible since "Programing 2" will be completely of "Object Oriented programming", I've been told.
2. Employee Class
Write a class named Employee that has the following member variables:
• name. A string that holds the employee’s name.
• idNumber. An int variable that holds the employee’s ID number.
• department. A string that holds the name of the department where the employee
works.
• position. A string that holds the employee’s job title.
The class should have the following constructors:
• A constructor that accepts the following values as arguments and assigns them to the appropriate member variables: employee’s name, employee’s ID number, department,and position.
• A constructor that accepts the following values as arguments and assigns them to the appropriate member variables: employee’s name and ID number. The department and position fields should be assigned an empty string ( "" ).
• A default constructor that assigns empty strings ( "" ) to the name , department , and position member variables, and 0 to the idNumber member variable.
Write appropriate mutator functions that store values in these member variables and accessor functions that return the values in these member variables. Once you have written the class, write a separate program that creates three Employee objects to hold the following data.
Name ID Number Department Position
Susan Meyers 47899 Accounting Vice President
Mark Jones 39119 IT Programmer
Joy Rogers 81774 Manufacturing Engineer
The program should store this data in the three objects and then display the data for each employee on the screen.
Program:
employee.h
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
|
#pragma once
#include<iostream>
using namespace::std;
class Employee {
private:
string name,
department,
position;
int idNumber;
public:
Employee();
Employee(double, string, string, string);
~Employee();
void setname(string);
void setidNumber(double);
void setdepartment(string);
void setposition(string);
string getname() const;
double getidNumber() const;
string setdepartment() const;
string setposition() const;
};
|
Employee.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 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
|
#include"Employee.h"
Employee:: Employee() {
idNumber = 0.0;
name = "";
department = "";
position = "";
}
Employee::Employee(double numero, string nombre, string departamento, string posicion) {
name = nombre;
idNumber = numero;
department = departamento;
position = posicion;
}
Employee::Employee(double numero, string nombre, string departamento, string posicion) {
name = nombre;
idNumber = numero;
department = "";
position = "";
}
Employee::~Employee() {
}
void Employee::setname(string nombre) {
name = nombre;
}
void Employee::setidNumber(double numero) {
idNumber = numero;
}
void Employee::setdepartment(string departamento) {
department = departamento;
}
void Employee::setposition(string posicion) {
position = posicion;
}
string Employee::getname() const {
return name;
}
double Employee::getidNumber() const {
return idNumber;
}
string Employee::setdepartment() const {
return department;
}
string Employee::setposition() const {
return position;
}
|
Employee_main.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 28
|
#include<iostream>
#include<string>
#include"Employee.h"
using namespace::std;
int main() {
string nombre, departamento, posicion;
double numero;
cout << "Enter your name: ";
cin >> nombre;
cout << "Enter your Id number: ";
cin >> numero;
cout << "Enter your department: ";
cin >> departamento;
cout << "Enter your posicion: ";
cin >> posicion;
system("pause");
return 0;
}
|