03 Dec, 03:12PM in sunny Singapore!

Object-Oriented Programming

Subscribe to Object-Oriented Programming 37 posts

Please Login or Signup to reply.
  • Mas Selamat Kastari
    Xcert's Avatar
    9,998 posts since Dec '01
    • 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. Crying or Very sad

  • caleb_chiang's Avatar
    7,941 posts since Jul '05
  • eagle's Avatar
    17,976 posts since Aug '01
  • caleb_chiang's Avatar
    7,941 posts since Jul '05
  • Moderator
    ndmmxiaomayi's Avatar
    53,307 posts since Aug '05
  • eagle's Avatar
    17,976 posts since Aug '01
    • have u tried

      cout<<"nOffice of "<<<" is Office "<

      because I think derived class means it is still something within the manager class

  • Moderator
    LatecomerX's Avatar
    2,331 posts since May '07
    • 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
  • manyu882's Avatar
    1,830 posts since Jun '05
  • thoreldan's Avatar
    3,905 posts since Jun '03
    • 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

  • thoreldan's Avatar
    3,905 posts since Jun '03
    • 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

      u can do this

      public:

      char* PrintName();

      -------------
      inside just return the name lor

  • Moderator
    LatecomerX's Avatar
    2,331 posts since May '07
    • 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

      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.
      So what's so bad about making a property/var public?

      Edited by LatecomerX 24 Sep `07, 10:06PM
  • thoreldan's Avatar
    3,905 posts since Jun '03
    • 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

  • Mas Selamat Kastari
    Xcert's Avatar
    9,998 posts since Dec '01
    • Originally posted by eagle:
      have u tried

      cout<<"nOffice of "<<<" is Office "<

      because I think derived class means it is still something within the manager class

      error C2248: 'name' : cannot access private member declared in class 'employee'

  • Mas Selamat Kastari
    Xcert's Avatar
    9,998 posts since Dec '01
    • Originally posted by thoreldan:
      try it...see if it works

      yup it works.thanks.

      care to explain more on ur method of accessing a private member variable of a private class?

  • Moderator
    ndmmxiaomayi's Avatar
    53,307 posts since Aug '05
    • 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
  • croco2006's Avatar
    164 posts since Nov '06
  • Mas Selamat Kastari
    Xcert's Avatar
    9,998 posts since Dec '01
    • 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.

  • Mas Selamat Kastari
    Xcert's Avatar
    9,998 posts since Dec '01
    • Originally posted by croco2006:
      mi o level nia

      nowadays primary sch learn how to program robots. Laughing Mr. Green

  • Moderator
    ndmmxiaomayi's Avatar
    53,307 posts since Aug '05
    • 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'

  • eagle's Avatar
    17,976 posts since Aug '01
  • thoreldan's Avatar
    3,905 posts since Jun '03
    • 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

  • Moderator
    ndmmxiaomayi's Avatar
    53,307 posts since Aug '05
    • 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

      I stand corrected. I've always used public. Embarassed

  • thoreldan's Avatar
    3,905 posts since Jun '03
  • Moderator
    LatecomerX's Avatar
    2,331 posts since May '07
    • 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. Embarassed

      Edited by LatecomerX 27 Sep `07, 11:07PM
  • Moderator
    ndmmxiaomayi's Avatar
    53,307 posts since Aug '05
    • 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. Embarassed

      See wrongly. Embarassed

Please Login or Signup to reply.