Basically, I don\'t know why my strings keep converting to const char[6] instead
ID: 3764592 • Letter: B
Question
Basically, I don't know why my strings keep converting to const char[6] instead of const string&.
Please help me identify this problem.
Here is a brief coding below:
Employee Class:
class Employee
{
private:
string lname;
string fname;
string ID;
char sex;
Date *Bdate;
public:
Employee()
{
lname=" ";
fname=" ";
ID="";
sex='F';
Bdate=new Date(1,1,2010);
}
Employee(const string& ln,const string& fn,const string& id,char s,Date m1)
{
lname=ln;
fname=fn;
ID=id;
sex=s;
Bdate=new Date(m1.d,m1.m,m1.y);
}
void setempfName(const string& f)
{
fname=f;
}
void getemplname(const string& l)
{
lname=l;
}
void getID(const string& id)
{
ID=id;
}
string getempfName()
{
return fname;
}
string getemplname()
{
return lname;
}
string getID()
{
return ID;
}
void putData() const
{
cout<<"Employee Name:"<<fname<<" "<<lname<<endl;
cout<<"Employee id:"<<ID<<endl;
cout<<"Sex:"<<sex<<endl;
cout<<"Birth Date:"<<Bdate->d<<"-"<<Bdate->m<<"-"<<Bdate->y<<endl;
}
virtual double monthlyEarning()=0;
};
Staff:
Staff():Employee()
{
hourlyRate=1;
}
Staff(const string& l,const string& f,const string& id,char s,Date m,double rate):Employee(l,f,id,s,m)
{
setHourlyRate(1);
}
void setHourlyRate(double rate)
{
hourlyRate=rate;
}
double getHourlyRate()
{
return hourlyRate;
}
double monthlyEarning()
{
return hourlyRate*160;
}
void putData()
{
cout<<" STAFF INFORMATION:"<<endl;
Employee::putData();
cout<<" Monthly Earning:"<< monthlyEarning() <<endl;
}
};
Main:
int main()
{
Employee * ePtr[10];
Staff s1;
ePtr[0] = new Staff("John","jerry","12345",'F',new Date(1,12,1965),10);
Staff s2("jerry","K","11111",'M',new Date(1,5,1987),12);
e[0]=s1;
e[1]=s2;
s1.putData();
s2.putData();
}
Please help with this problem, this is the fifth time I posted this problem and I've been getting spam as answers.
Explanation / Answer
Here is the code for you....
#include <iostream>
using namespace std;
/*Date Class.*/
class Date
{
public:
int d;
int m;
int y;
public:
Date()
{
d=1;
m=1;
y=2010;
}
Date(int d1,int m1,int y1)
{
d=d1;
m=m1;
y=y1;
}
Date(const Date &s)
{
d=s.d;
m=s.m;
y=s.y;
}
};
/*Employee Class.*/
class Employee
{
private:
string lname;
string fname;
string ID;
char sex;
Date *Bdate;
public:
Employee()
{
lname=" ";
fname=" ";
ID="";
sex='F';
Bdate=new Date(1,1,2010);
}
Employee(const string& ln,const string& fn,const string& id,char s,Date m1)
{
lname=ln;
fname=fn;
ID=id;
sex=s;
Bdate=new Date(m1.d,m1.m,m1.y);
}
void setempfName(const string& f)
{
fname=f;
}
void getemplname(const string& l)
{
lname=l;
}
void getID(const string& id)
{
ID=id;
}
string getempfName()
{
return fname;
}
string getemplname()
{
return lname;
}
string getID()
{
return ID;
}
void putData() const
{
cout<<"Employee Name:"<<fname<<" "<<lname<<endl;
cout<<"Employee id:"<<ID<<endl;
cout<<"Sex:"<<sex<<endl;
cout<<"Birth Date:"<<Bdate->d<<"-"<<Bdate->m<<"-"<<Bdate->y<<endl;
}
virtual double monthlyEarning()=0;
};
//Staff Class.
class Staff:public Employee
{
double hourlyRate;
public:
Staff():Employee()
{
hourlyRate=1;
}
Staff(const string& l,const string& f,const string& id,char s,Date m,double rate):Employee(l,f,id,s,m)
{
setHourlyRate(1);
}
void setHourlyRate(double rate)
{
hourlyRate=rate;
}
double getHourlyRate()
{
return hourlyRate;
}
double monthlyEarning()
{
return hourlyRate*160;
}
void putData()
{
cout<<" STAFF INFORMATION:"<<endl;
Employee::putData();
cout<<" Monthly Earning:"<< monthlyEarning() <<endl;
}
};
int main()
{
Employee * ePtr[10];
Staff s1;
Date d1 (1,12,1965);
ePtr[0] = new Staff("John","jerry","12345",'F',d1,10);
Date d2 (1,5,1987);
Staff s2("jerry","K","11111",'M',d2,12);
ePtr[0]=&s1;
ePtr[1]=&s2;
s1.putData();
s2.putData();
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.