#include
#include
#include
#include
class employee
{
private:
int nSalary;
char name[80];
int nDept;
public:
void SetSalary(int);
void PrintSalary();
void SetName(char *aString);
protected:
};//end class employee
void employee::SetSalary(int s)
{
nSalary=s;
}//end void employee::SetSalary
void employee:: PrintSalary()
{
cout<<"nSalary of "<}//end void employee:: PrintSalary()
void employee::SetName(char *aString)
{
if(strlen(aString)<=79)
{
strcpy(name,aString);
}
else
{
strcpy(name, "Default" );
}
}//end void employee::SetName(char *aString)
class manager:public employee //Derived class
{
private:
int nOffice;
public:
void SetOffice(int);
void PrintOffice();
protected:
};//end class manager:public employee
void manager::SetOffice(int o)
{
nOffice=o;
}//end void manager::SetOffice(int o)
void manager:: PrintOffice()
{
cout<<"nOffice of "<}//end void manager:: PrintOffice()
class employee
{
private:
int nSalary;
int nDept;
public:
void SetSalary(int);
void PrintSalary();
void SetName(char *aString);
char name[80];
protected:
};//end class employee
u can do thisOriginally posted by thoreldan:havent read thru the code...
but it's bad to declare name as public
the standard way is to make a public assessor to access the name instead
Hmm let me learn something here.Originally posted by thoreldan:havent read thru the code...
but it's bad to declare name as public
the standard way is to make a public assessor to access the name instead
try it...see if it worksOriginally posted by LatecomerX:Hmm let me learn something here.
So you mean something like, under "public:" of the employee class, you have a method named GetName() which basically returns the private "name" property?
PS:
Argh you answered my question before I posted it. lol.
error C2248: 'name' : cannot access private member declared in class 'employee'Originally posted by eagle:have u tried
cout<<"nOffice of "<
because I think derived class means it is still something within the manager class
yup it works.thanks.Originally posted by thoreldan:try it...see if it works
Your variable "name" in the employee class is private; it's protected. You can't access it this way. Will need to declare it as public if you want to access it.Originally posted by Xcert:error C2248: 'name' : cannot access private member declared in class 'employee'
yup.LatecomerX's method also works...but my problem is that "name" must be declared as private(a requirement) so I cant suka suka change it to public.Originally posted by ndmmxiaomayi:Your variable "name" in the employee class is private; it's protected. You can't access it this way. Will need to declare it as public if you want to access it.
nowadays primary sch learn how to program robots.Originally posted by croco2006:mi o level nia
Private can only access with private... as far as I know. LateComerX's method will work as well, because it's within the same class. It's not like another class is trying to access the private class.Originally posted by Xcert:yup.LatecomerX's method also works...but my problem is that "name" must be declared as private(a requirement) so I cant suka suka change it to public.
'name' : cannot access private member declared in class 'employee'
always use a accessor instead...Originally posted by ndmmxiaomayi:Your variable "name" in the employee class is private; it's protected. You can't access it this way. Will need to declare it as public if you want to access it.
I stand corrected. I've always used public.Originally posted by thoreldan:always use a accessor instead...
declaring instance variable as public is very bad programming.
anyway.. private and protected can mean different thing
Originally posted by ndmmxiaomayi:I stand corrected. I've always used public.![]()
Me curious here, why is it a bad programming practice to declare such vars as public?Originally posted by thoreldan:always use a accessor instead...
declaring instance variable as public is very bad programming.
anyway.. private and protected can mean different thing
See wrongly.Originally posted by LatecomerX:Me curious here, why is it a bad programming practice to declare such vars as public?
And to mayi: in the future, please spell my username as "LatecomerX', not with the "C" capitalized. A big "C" there looks...kinda weird.