943,899 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 1977
  • C++ RSS
Nov 21st, 2008
0

Help with instance of one class in another

Expand Post »
Hello everyone.

I need help using an instance of a class in another. Below are two header files. NumDays.h and TimeOff.h

I need to have instances of the NumDays class in the TimeOff class as members. When i try to create the instance called maxSickDays, i do not get access to the NumDays member functions. What is wrong with my code? Thanks in advance.

NumDays.h
C++ Syntax (Toggle Plain Text)
  1. #ifndef NUMDAYS
  2. #define NUMDAYS
  3. #include <iostream>
  4.  
  5. class NumDays
  6. {
  7.  
  8. private:
  9. int hours;
  10. float days;
  11. float calcDays();
  12.  
  13. public:
  14. NumDays()
  15. { hours = 0; days = 0.0; }
  16.  
  17. void setHours(int hrs)
  18. { hours = hrs; }
  19. int getHours()
  20. { return hours; }
  21. float getDays();
  22.  
  23. NumDays operator + ( const NumDays& );
  24. NumDays operator - ( const NumDays& );
  25. NumDays operator --();
  26. NumDays operator ++();
  27. NumDays operator ++ ( int );
  28. NumDays operator -- ( int );
  29.  
  30. };
  31.  
  32. #endif

and now TimeOff.H
C++ Syntax (Toggle Plain Text)
  1. #ifndef TIMEOFF
  2. #define TIMEOFF
  3. #include <iostream>
  4. #include "NumDays.h"
  5. #include <string>
  6. using namespace std;
  7.  
  8. class TimeOff
  9. {
  10. private:
  11. string name;
  12. int idNum;
  13. NumDays maxSickDays;
  14.  
  15. public:
  16. TimeOff();
  17.  
  18.  
  19. void setName ( string n )
  20. { name = n; }
  21. void setIdNum ( int id )
  22. { idNum = id; }
  23.  
  24. string getName()
  25. { return name; }
  26. int getIdNum()
  27. { return idNum; }
  28.  
  29. };
  30.  
  31. #endif

Thank you!
Similar Threads
Reputation Points: 22
Solved Threads: 14
Junior Poster in Training
jbrock31 is offline Offline
73 posts
since Oct 2008
Nov 21st, 2008
0

Re: Help with instance of one class in another

Can I see the part where you try to access the NumDays()?
Reputation Points: 888
Solved Threads: 114
Nearly a Posting Virtuoso
MosaicFuneral is offline Offline
1,270 posts
since Nov 2008
Nov 21st, 2008
0

Re: Help with instance of one class in another

Oops. I took all the calls to that when it would not work. Below is TimeOff.h where i am trying to use the maxSickDays instance and then its setHours member function. Thank you.

C++ Syntax (Toggle Plain Text)
  1. #ifndef TIMEOFF
  2. #define TIMEOFF
  3. #include <iostream>
  4. #include "NumDays.h"
  5. #include <string>
  6. using namespace std;
  7.  
  8. class TimeOff
  9. {
  10. private:
  11. string name;
  12. int idNum;
  13. NumDays maxSickDays;
  14.  
  15. public:
  16. TimeOff();
  17.  
  18.  
  19. void setName ( string n )
  20. { name = n; }
  21. void setIdNum ( int id )
  22. { idNum = id; }
  23.  
  24. void setMaxSickDays( int h )
  25. { maxSickDays.setHours(h); } /// trying to access this instance of NumDays
  26.  
  27.  
  28. string getName()
  29. { return name; }
  30. int getIdNum()
  31. { return idNum; }
  32.  
  33.  
  34.  
  35. };
  36.  
  37. #endif
Reputation Points: 22
Solved Threads: 14
Junior Poster in Training
jbrock31 is offline Offline
73 posts
since Oct 2008
Nov 21st, 2008
0

Re: Help with instance of one class in another

I figured out my problem. When i defined the mutator and accessor functions in the cpp file instead of the header i was able to access the nested class' member variables and functions. Below are my revised header and cpp files.

TimeOff.h
C++ Syntax (Toggle Plain Text)
  1. #ifndef TIMEOFF
  2. #define TIMEOFF
  3. #include <iostream>
  4. #include "NumDays.h"
  5. #include <string>
  6. using namespace std;
  7.  
  8. class TimeOff
  9. {
  10. private:
  11. string name;
  12. int idNum;
  13. NumDays maxSickDays;
  14.  
  15.  
  16. public:
  17. TimeOff();
  18.  
  19.  
  20. void setName ( string n )
  21. { name = n; }
  22. void setIdNum ( int id )
  23. { idNum = id; }
  24. void setMaxSickDays( int );
  25.  
  26.  
  27.  
  28. float getMaxSickDays();
  29. string getName()
  30. { return name; }
  31. int getIdNum()
  32. { return idNum; }
  33.  
  34.  
  35.  
  36. };
  37.  
  38. #endif

now TimeOff.cpp
C++ Syntax (Toggle Plain Text)
  1. #include "TimeOff.h"
  2. #include "NumDays.h"
  3. #include <iostream>
  4. #include <string>
  5. using namespace std;
  6.  
  7. TimeOff::TimeOff()
  8. {
  9. name = "";
  10. idNum = 0;
  11. maxSickDays.setHours(0);
  12. // sickTaken.setHours(0);
  13. // maxVacation.setHours(0);
  14. // vacTaken.setHours(0);
  15. // maxUnpaid.setHours(0);
  16. // unpaidTaken.setHours(0);
  17. }
  18. void TimeOff::setMaxSickDays ( int hrs )
  19. {
  20. maxSickDays.setHours(hrs);
  21.  
  22. }
  23. float TimeOff::getMaxSickDays()
  24. {
  25. return maxSickDays.getDays();
  26. }
Reputation Points: 22
Solved Threads: 14
Junior Poster in Training
jbrock31 is offline Offline
73 posts
since Oct 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: UDP-Sockets chat application question
Next Thread in C++ Forum Timeline: Crash Windows





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC