when I have the following member in a class
employee headOfDepartment;
what's wrong these setters and getters?
void department::setHeadOfDepartment( employee depEmployee)
{
headOfDepartment=depEmployee;
}
employee department::getHeadOfDepartment()
{
return headOfDepartment;
}
I've been trying forever to define setters & getters with composition and it keeps getting me this error: "field ‘headOfDepartment’ has incomplete type"
ok Those are the header files:
#ifndef EMPLOYEE_H_
#define EMPLOYEE_H_
using namespace std;
#include <iostream>
#include <vector>
#include "department.h"
#include "project.h"
class department;
class project;
//#include <vector>
employee.h
class employee
{
string Name; //text with spaces
string National_ID; //unique value (text) for each employee
static double Salary; // value of 1500 pounds
char Gender; //character holds f or m
int Available_vacations; //initially starts with 15 days
static double Deduction_per_day; // value of 85.5 pounds
int Available_permission_hours; //initially starts with 20 hours
static double Deduction_per_hour; // value of 15.5 pounds
double Actual_salary; // value with actual salary after deductions
int Vacations; // vacations employee took
int Permessions; // permession hours employee took
int empSerialNum; // object order in vector
department* myDepartment;
vector < project > empProjects;
public:
employee (); // default constructor
employee (string myName, string myNationalID, char myGender,int mySerialNum); // Parameterized constructor
~employee(); // Destractor
//Setters
void setName(string myName);
void setNationalID (string myNationalID);
void setGender (char myGander);
void setAvailableVacation(int myAvVac);
void setAvailablepermissionhours (int myAvPerHours);
void setActualSalary (double actualSalary);
void setVacations(int myVacations);
void setPermessions(int myPermessions);
void setempSerialNum(int mySerialNum);
void setDepartment(department*);
void addProject(project);
//Getters
string getName();
string getNationalID ();
char getGender ();
int getAvailableVacation();
int getAvailablepermissionhours ();
double getActualSalary ();
int getVacations ();
int getPermessions ();
int getempSerialNum();
department* getDepartment();
project* getProjects();
void view (); // View to view Name, ID and actual salary
void View_Detailed (); //call previous function and also shows other details (vacations - permissions - detailed deductions - ... )
void Free_All(); //return all values to default
double Take_vacation(); //this function takes number of days employee need to take as vacation, available vacations reduced by number of days given, if available vacations became 0 salary is deduced by deduction per day set
double Take_permession(); //this function takes hours that employee asked to take, reduce available permission hour by hours given, if available permission become 0 hour salary is reduced by deduction per ho
double Calculate_Actual_Salary();// calculates salary after deductions and returns it
};
#endif
department.h
#ifndef DEPARTMENT_H_
#define DEPARTMENT_H_
using namespace std;
#include <string.h>
#include "employee.h"
#include "project.h"
#include <vector>
class project;
class employee;
class department{
private:
string name;
string ID;
employee headOfDepartment;
vector <project> depprojects; //projects managed by the department
public:
//constructors
department();
department(string, string);
//Setters
void setName(string);
void setID(string);
void setHeadOfDepartment( employee /*const&*/ depEmployee);
void addProject(project);
//Getters
string getName();
string getID();
employee getHeadOfDepartment() /*const*/;
// project getProjects();
};
#endif
project.h
#ifndef PROJECT_H_
#define PROJECT_H_
#include <string.h>
#include "department.h"
#include "project.h"
class department;
class project{
string name;
department* location;
public:
//constructors
project();
project(string proName, department* proDepartment);
//Setters
void setName(string proName);
void setLocation(department* proDepartment);
//Getters
string getName();
department* getLocation();
};
#endif
You need to the include the header file where employee is declared in your header and source files for department
Class employee shall be defined before using it as a type name of an object. Also I advice to add qualifier const for the getter
You are not including the header that defines employee in your department class header, but you have a non-reference non-pointer declaration of type employee in your header.
Related
I have 2 classes. Since Doctor will be considered as Employee, I should be using Employee class functions in Doctor class. Only extra thing that Doctor class has is TITLE. Basically, What I tried is I wanted to send value to Doctor's constructor,set title then send remained value to Employee's class ;however, I could not. This is what I have done so far,
employee.h
#ifndef EMPLOYEE_H
#define EMPLOYEE_H
class Employee {
private:
int ID;
char *firstname;
char *lastname;
int telno;
char *adress;
char *mail;
int salary;
public:
Employee();
Employee(int,char *,char*,int,char*,char*,int);
char* getfmame();
char* getlname();
char* getadress();
char* getmail();
int getID();
int gettel();
int getsalary();
void printall();
};
#endif
Employee.cpp
#include <iostream>
#include "employee.h"
using namespace std;
Employee::Employee() {
firstname = "Empty";
ID=0;
firstname="Empty";
lastname="Empty";
telno=0;
adress="Empty";
mail="Empty";
salary=0;
}
Employee::Employee(int id,char * first,char* last,int tell,char* adres,char* email,int salar){
ID=id;
firstname=first;
lastname=last;
telno=tell;
adress=adres;
mail=email;
salary=salar;
}
char* Employee::getfmame(){ return firstname; }
char* Employee::getlname(){ return lastname; }
char* Employee::getadress(){ return adress; }
char* Employee::getmail(){ return mail; }
int Employee::getID(){ return ID; }
int Employee::gettel(){ return telno; }
int Employee::getsalary(){ return salary; }
void Employee::printall(){
cout<<endl<<"EMLOYEE INFORMATION"<<endl<<"------------------"<<endl;
cout<<endl<<"ID :"<<ID<<endl<<"FIRST NAME: "<< firstname <<endl<<"LAST NAME: "<< lastname << endl << "TELEPHONE NUMBER: "<<telno<<endl<<"ADRESS: "<<adress<<endl<<"MAIL: "<<mail<<endl<<"SALARY: "<<salary<<endl;
}
Doctor.h
#ifndef DOCTOR_H
#define DOCTOR_H
#include "Employee.h"
using namespace std;
class Doctor :Employee {
public:
enum title {Intern=0,Practitioner=1,Assistant=2,Specialist=3,Docent=4,Professor=5,None=6};
Doctor();
Doctor(title a,int id,char * first,char* last,int tell,char* adres,char* email,int salar);
};
#endif
Doctor.cpp
#include <iostream>
#include "Doctor.h"
#include "Employee.h"
using namespace std;
Doctor::Doctor() {
title tit = None ;
}
Doctor::Doctor(title a,int id,char * first,char* last,int tell,char* adres,char* email,int salar) {
title tit=a;
Employee(id,first,last, tell,adres,email,salar);
printall();
cout<<"typed";
}
Main.cpp
#include <iostream>
#include "employee.h"
#include "doctor.h"
using namespace std;
int main(){
Doctor a=Doctor(Doctor::None,12,"a","b",0550550505,"8424 str nu:5","#hotmail",5000);
return 0;
}
Subclass construction in C++ works so that the base class object must be constructed when the subclass' constructor body is executed:
class A {
/* etc. etc. */
public:
void do_stuff();
};
class B : public A {
B() {
// at this point, an A has already been constructed!
A::do_stuff();
}
};
Note that in this example, since we haven't chosen an explicit constructor for the A instance, the default constructor, A::A(), will be used; and if that constructor is unavailable - we get a compilation error. The fact that a constructor for A has been called is what allows us to then use methods of class A - like A::do_stuff() in the example above.
But - how can we specify a different constructor before the body of the B constructor? Or in your case, how can we use the appropriate constructor for Employee before the body of the Doctor constructor?
The answer was suggested by #user4581301: You need to use an member initializer list. Initializations/constructions on this list are performed before the body, and may include the underlying class. I'll demonstrate with a simplified example. Let's suppose an Employee only has an id and a Doctor only has an additional title.
class Employee {
protected:
int id_;
public:
Employee(int id) : id_(id) { };
int id() const { return id_; }
};
class Doctor : public Employee {
protected:
std::string title_;
public:
Doctor(int id, std::string title) : Employee(id), title_(title) { };
const std::string& title() const { return title_; }
};
So, when a Doctor is being constructed, it constructs its underlying Employee instance using the id it got. The constructor body is used for more complex code beyond simple member initializations.
PS:
You might want to initialize the title_ member with std::move(title) rather than just title, see this question for details.
It's confusing when a constructor has more than two or three parameters with compatible types - users are likely to confuse them with each other. You might consider default values for most fields and setting them after construction, or alternatively, using a builder pattern.
address, with two d's, not adress.
Unless you plan on editing char* fields in-place, use const char *.
They way you've written your classes, Doctor methods would not have write acesss to Employee methods; make sure that's what you intended.
I have some other nitpicks but I'll stop now...
I am trying to use constructor initializer for name and time but the visual studio is giving me errors, I do not see any issues with it, it seems fine to me, I tried to see the problem, I tried other things as well, Please Help. Thanks you in advance.
Any help really appreciated, I added all the header file and implementation of Entry class, I know it seems, I added so you can see it.
// Entry.h
#pragma once
#include <iostream>
#include <string>
#include "RelationType.h"
using namespace std;
class Name
{
public:
Name();
Name(string firstName, string middleName, string lastName);
string GetFristName() const;
string GetLastName() const;
string GetMiddleName() const;
char GetMiddleInitial() const;
RelationType ComparedTo(Name otherName) const;
private:
string first;
string last;
string middle;
};
//Entry.cpp
#include "Entry.h"
#include<iostream>
#include <string>
using namespace std;
Entry::Entry() {}
Entry::Entry(string firstName, string middleName, string lastName,
int initHours, int initMinutes, int initSeconds)
: name{ (firstName, middleName, lastName) , //name is where its mad at
time(initHours, initMinutes, initSeconds) } {}
string Entry::GetNameStr () const
{
return (name.GetFirstName() + ' ' + name.GetLastName());
}
string Entry::GetTimeStr () const
{
return (name.FirstName() + ' ' + name.LastName());
}
// Name.h
#pragma once
#include <iostream>
#include <string>
#include "RelationType.h"
using namespace std;
class Name
{
public:
Name();
Name(string firstName, string middleName, string lastName);
string GetFristName() const;
string GetLastName() const;
string GetMiddleName() const;
char GetMiddleInitial() const;
RelationType ComparedTo(Name otherName) const;
private:
string first;
string last;
string middle;
};
// RealtionType.h
#pragma once
#ifndef RELATION
#define RELATION
enum RelationType { BEFORE, SAME, AFTER };
#endif
// TimeOfDay.h
#pragma once
class TimeOfDay
{
public:
//Intentionally missed const, see what happened without const
TimeOfDay(); // zero timepfday object
TimeOfDay(int hours, int minutes, int seconds); //takes 3 parameters
TimeOfDay Increment() const; //increment by 1 sec
void Write() const; //write the timeofday obj to print
bool Equal(TimeOfDay otherTime) const; //true if timeofday obj equals othertime
bool LessThan(TimeOfDay otherTime) const; //const, true if the timeofday obj is
//before itherTime
private:
int hours;
int minutes;
int seconds;
};
Your Entry class constructor code should be something like below
Entry::Entry(string firstName, string middleName, string lastName,
int initHours, int initMinutes, int initSeconds)
: name(firstName, middleName, lastName)
, time(initHours, initMinutes, initSeconds) {}
I have some issues solving a task from class. My algorithm for solving the issues is working fine, but my problem is to read data from 3 different text files into 2 different classes.
Now the 1st text file "hours.txt" gives a string id and hour int, like so:
adam1;170
eve2;170
so, separated by a ";".
The next file contains the same id as before and name, taxid, type, and according to type: wage if PH, or salary and ovtwage if IS.
adam1;Adam Driver;12345678;PH;5;
eve2;Eve Assistant;23456789;IS;650;10
The 3rd file contains only the int 160, which is defined as generalWorkingHours. Now to where my problems are arising. I have experience in reading data from 1 file into 1 class but in this case I have to read data to 2 classes, ph and is depending on the type of the id's(adam1 and eve2). I have been provided two classes like this :
#ifndef IS_H
#define IS_H
#include "Employee.h"
#include <iostream>
using namespace std;
class is: public Employee
{
public:
is();
virtual ~is();
void setSalary(int salary);
int getSalary();
void setOvtWage(int ovtWage);
int getOvtWage();
protected:
private:
int salary;
int ovtWage;
};
#endif // IS_H
and
#ifndef PH_H
#define PH_H
#include "Employee.h"
#include <iostream>
using namespace std;
class ph: public Employee
{
public:
ph();
virtual ~ph();
void setWage(int wage);
int getWage();
protected:
private:
int wage;
};
#endif // PH_H
Both of these classes contain the public "Employee"
#ifndef EMPLOYEE_H
#define EMPLOYEE_H
#include <iostream>
using namespace std;
class Employee
{
public:
Employee();
virtual ~Employee();
void setId(string id);
string getId();
void setName(string name);
string getName();
void setTaxId(string taxid);
string getTaxId();
void setType(string type);
string getType();
void setHours(int hours);
int getHours();
protected:
private:
string id;
string name;
string taxid;
string type;
int hours;
};
#endif // EMPLOYEE_H
Now, usually I would create a function to read a file, and one to parse each line like so:
void Resolver::parseTextLine(string tmp, int & carCnt, carList X[]){
std::size_t found;
found=tmp.find(";");
if (found!=string::npos) {
X[carCnt].point=tmp.substr(0,found);
tmp=tmp.substr(found+1);
}
found=tmp.find(";");
if (found!=string::npos) {
X[carCnt].license=tmp.substr(0,found);
tmp=tmp.substr(found+1);
}
found=tmp.find(";");
if (found!=string::npos) {
X[carCnt].time=atoi(tmp.substr(0,found).c_str());
tmp=tmp.substr(found+1);
}
carCnt++;
}
void Resolver::readDataFromFiles(string carFile, int & carCnt, carList X[]){
carCnt=0;
ifstream finS(carFile.c_str(),ios::in);
bool first=true;
while (!finS.eof()) {
string tmp="";
getline(finS,tmp);
if (tmp!="") {
if (first) {
first=!first;
} else {
parseTextLine(tmp,carCnt,X);
}
}
}
finS.close();
}
NOTE: this is just an idea of how I am trying to solve it, but I have no experience with using multiple files and classes. All the functions are premade and I just need to patch it together somehow.
Assuming all the ids are unique, create a map<string, Employee*> Emp; This will store information of all the employee indexed by id as key ("adam1", "eve2") and object as value.
["adam1"] => [object of ph]
["eve2"] => [object of is]
Now, read file_2 which contains information about PH and IS. Now for each line read you will have all the components separated by ";" appearing in the line. After separating components from the line, you should be able to decide (using type) which derived class should be instantiated.
if(type == PH)
{
//suppose id = "adam"
ph *pEmployeePH = new ph();
//also set wage
//insert [id] in map Emp if not already present
}
if(type == IS)
{
//suppose id is now "eve2"
is *pEmployeeIS = new is();
//also set salary and ovtwage
//insert [id] in map Emp if not already present
}
Once you have your map ready, now read file_1. Now for each line read you will have 2 components separated by ";" appearing in the line. After separating components from the line, you should be able to decide (using id) which element of map Emp you should access/modify in order to set the hours.
Suppose id is "adam1" and hours=170, so now check whether map contains ["adam1"] and if does contain then set the hours as follows: Emp[id].setHours(hours);
I'm currently working on an assignment to expand on a program we previously made, involving the use of header files, and parent classes. In the original, I have 2 header files. Person.h, and OCCCDate.h. In the new one, I am creating one called OCCCPerson.h. It's an incredibly simple class that basically just uses Person, just with 1 added variable.
My problem is, I cant figure out how to use the parent constructor properly.
Here is the Person.h file.
#ifndef PERSON_H
#define PERSON_H
#include <string>
#include "OCCCDate.h"
using namespace std;
class Person{
private:
string firstName;
string lastName;
OCCCDate dob;
public:
Person();
Person(string, string);
Person(string, string, OCCCDate);
string getFirstName();
string getLastName();
void setFirstName(string);
void setLastName(string);
int getAgeInYears();
bool equals(Person);
string toString();
};
#endif
And here is my OCCCPerson.h file
#ifndef OCCCPERSON_H
#define OCCCPERSON_H
#include <string>
#include "OCCCDate.h"
#include "Perosn.h"
using namespace std;
class OCCCPerson : Person{
protected:
string studentID;
public:
OCCCPerson(string firstName, string lastName, OCCCDate dob, string studentID);
OCCCPerson(Person p, string studentID);
string getStudentID();
bool equals(OCCCPerson p);
string toString();
};
#endif;
I cant seem to call on the parents constructor to get things like the firstname, lastname, and dob(date of birth). From my handout, it says the parent constructor has to be initialized with, : Person(parameters), where parameters are things in the parent class. However, I have no idea where to put that. Sorry for writing so much. I just couldn't figure out how to shrink that down.
Oh, and here is OCCCDate.h just in case
#ifndef OCCCDATE_H
#define OCCCDATE_H
#include<string>
using namespace std;
class OCCCDate{
private:
bool OCCCDate_US;
bool OCCCDate_EURO;
int dayOfMonth, monthOfYear, year;
bool dateFormat;
public:
OCCCDate();
OCCCDate(int dayOfMonth, int monthOfYear, int year);
int getDayOfMonth();
int getMonth();
string getNameOfMonth();
int getYear();
string getDate();
int getDifference(OCCCDate d1, OCCCDate d2);
int getDifference(OCCCDate d1);
void setDateFormat(bool);
bool equals(OCCCDate d);
string toString();
};
#endif
And here is my OCCCDate.cpp file
#include<iostream>
#include<ctime>
#include "OCCCPerson.h"
using namespace std;
OCCCPerson::OCCCPerson(string firstName, string lastName, OCCCDate dob, string studentID):Person(firstName, lastName, dob){
string firstName = Person::getFirstName();
string lastName = Person::getLastName();
OCCCDate dob = dob;
this->studentID = studentID;
}
OCCCPerson::OCCCPerson(Person p, string studentID){
Person p = p;
this->studentID = studentID;
}
What you need is member initializer lists.
From cppreference:
In the definition of a constructor of a class, member initializer list specifies the initializers for direct and virtual base subobjects and non-static data members.
A simple example:
class MyClass : BaseClass
{
public:
MyClass(int arg) : BaseClass(arg) {
//Rest of code
}
}
In your case, you can do:
OCCCPerson(string firstName, string lastName, OCCCDate dob, string studentID) :
Person(firstName, lastName, dob) {
this.studentID = studentID;
}
In OCCCPerson.cpp,
OCCCPerson(string firstName, string lastName, OCCCDate dob, string
studentID) : Person(firstName, lastName, dob)
{
//more stuff
}
You basically have to use an initializer list.
Read more here: What are the rules for calling the superclass constructor?
What this does is it calls the parent constructor Person(string, string, OCCCDate) and basically performs this->firstName = firstName. Similarly for lastName and dob. But not studentID because the Person(string, string, OCCCDate) constructor doesn't provide initialization for studentID.
I have 3 files:
SilverLines.h
SilverLines.cpp
main.cpp
At SilverLines.cpp I have:
#include "SilverLines.h."
When I don't do:
#include "SilverLines.h"
at the main.cpp, everything is just fine. The project compiles.
But when I do:
#include "SilverLines.h"
in the main.cpp (Which i need to do), i get these 2 errors:
What's the problem?
> edit:
As you requested, this is the entire *.h code:
#ifndef SILVER_H
#define SILVER_H
#include <iostream>
#include <algorithm>
#include <iterator>
#include <vector>
#include <string>
#include <map>
using namespace std;
typedef unsigned short int u_s_int;
map<string /*phone*/,string /*company*/> companies; // a map of a phone number and its company
string findCompany(const string& phoneNum); //Given a phone number, the function returns the right company
//The Abstract Client Class:
class AbsClient //Abstract Client Class
{
protected:
//string phone_number;
int counter; //CALL DURATION TO BE CHARGED
double charge;
public:
string phone_number; //MOVE TO PROTECTED LATER
void setCharge(double ch) {charge=ch;}
void setCoutner(int count) {counter=count;}
AbsClient(string ph_num): phone_number(ph_num),counter(0), charge(0.0) {} //c'tor that must be given a phone#.
//Initializes the counter and the charge to 0.
virtual void registerCall(string /*phone#*/, int /*minutes*/) = 0; //make a call and charge the customer
void printReport(string /*phone#*/) const; //prints a minutes report
};
//The Temporary Client Class:
class TempClient : public AbsClient //TempClient class (NO DISCOUNT!) 2.3 NIS PER MINUTE, inherits from ABS
{
private:
string company;
public:
//string company;
TempClient(string ph_num, string cmp_name): AbsClient(ph_num), company(cmp_name) {}
virtual void registerCall(string /*phone#*/, int /*minutes*/); //make a call and charge the customer
//virtual void printReport(string phone) const {cout << "the number of minutes: " << this->counter << endl;}
};
//The REgistered Client Class:
class RegisteredClient : public AbsClient //RegisteredClient, 10% DISCOUNT! , inherits from ABS
{
protected:
string name;
string id;
long account; //ACCOUNT NUMBER
private:
static const int discount = 10; //STATIC DISCOUNT OF 10% FOR ALL Registered Clients
public:
RegisteredClient(string ph_num, string n, string i, long acc_num): AbsClient(ph_num), name(n) , id(i) , account(acc_num) {}
virtual void registerCall(string /*phone#*/, int /*minutes*/); //make a call and charge the customer
//virtual void printReport(string /*phone#*/) const {cout << "the number of minutes: " << this->counter << endl;}
};
//The VIP Client Class
class VIPClient : public RegisteredClient //VIP Client! X% DISCOUNT! also, inherits from RegisteredClient
{
private: //protected?
int discount; //A SPECIAL INDIVIDUAL DISCOUTN FOR VIP Clients
public:
VIPClient(string ph_num, string n, string i, long acc_num, int X): RegisteredClient(ph_num,n,i,acc_num), discount(X) {}
virtual void registerCall(string /*phone#*/, int /*minutes*/); //make a call and charge the customer
//virtual void printReport(string /*phone#*/) const {cout << "the number of minutes: " << this->counter << endl;}
};
//The SilverLines (the company) class
class SilverLines // The Company Class
{
protected:
vector<AbsClient*> clients;
vector<string> address_book; //DELETE
public:
//static vector<AbsClient*> clients;
//SilverLines();
void addNewClient(string /*phone#*/);
void addNewClient(string /*phone#*/, string /*name*/ , string /*id*/ , u_s_int /*importance lvl*/ , long /*account#*/);
void registerCall(string /*phone#*/ , int /*call duration*/);
void resetCustomer(string /*phone#*/); //given a phone#, clear the appropriate customer's data ('charge' and 'counter')
void printReport(string /*phone#*/) const;
~SilverLines(){
vector<AbsClient*>::iterator it;
for(it = clients.begin(); it != clients.end(); ++it)
delete(*it);
}
};
#endif
paercebal's Answer:
Your code has multiple issues (the
using namespace std;
for one), but the one failing the compilation is the declaration of a global variable in a header:
map<string /*phone*/,string /*company*/> companies;
I'm quite sure most of your sources (main.cpp and SilverLines.cpp, at least) include this header.
You should define your global object in one source file:
// SilverLines.h
extern map<string /*phone*/,string /*company*/> companies;
and then declare it (as extern) in the header:
// global.cpp
extern map<string /*phone*/,string /*company*/> companies;
Chances are that your "SilverLines.h" actually DOES define something, instead of just declaring it. I'm not going to try and decipher the error message, though :-)
Seems there are some multiple definitions coming due to inclusion of the SilverLines.h twice.
Pl. use a header guard like below in your SilverLines.h header file and then incude it.
This may help.
#ifndef SILVER_H
#define SILVER_H
//Write the contents of SilverLines.h here.
#endif // SILVER_H
You may have another error in your code.
ALso make sure that you are not writing your main or any other piece of code in .h file.
.h file should just contain the prototypes of the functions followed by a semi colon.
Thank you guys I worked it out:
findCompany() is now a global function, and map<string,string> companies is a static field inside it. It compiles great.