-
-
code:
#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 "<<<" is "<<
}//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 "<<<" is Office "<<
}//end void manager:: PrintOffice()
anyone knows how can I print out the name of the manager in the last line.
Employee is the base class,manager is the derived class.
manager.name is obviously not correct.
-
-
-
I guess you can try declaring the employee.name as a public variable instead, so that it can be accessed by its derived classes. But anyway, I'm not that good in OOP, so please correct me if I'm wrong.
Hence, the code should be:
code:
class employee
{
private:
int nSalary;
int nDept;
public:
void SetSalary(int);
void PrintSalary();
void SetName(char *aString);
char name[80];
protected:
};//end class employee
Edited by LatecomerX 24 Sep `07, 10:01PM
-
-
-
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 insteadHmm 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.
So what's so bad about making a property/var public?Edited by LatecomerX 24 Sep `07, 10:06PM
-
-
-
Originally 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.try it...see if it works
-
-
-
Originally posted by Xcert:error C2248: 'name' : cannot access private member declared in class 'employee'
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.
Edited by ndmmxiaomayi 27 Sep `07, 6:32PM
-
-
-
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.
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 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.
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.
eagle's method is actually utilizing another class to access the employee class, so it runs foul, producing an error. The error is quite easy to understand:
'name' : cannot access private member declared in class 'employee'
-
-
-
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.
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 thoreldan:always use a accessor instead...
declaring instance variable as public is very bad programming.
anyway.. private and protected can mean different thing
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.
Edited by LatecomerX 27 Sep `07, 11:07PM
-
