Help with instance of one class in another

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Oct 2008
Posts: 29
Reputation: jbrock31 is an unknown quantity at this point 
Solved Threads: 1
jbrock31 jbrock31 is offline Offline
Light Poster

Help with instance of one class in another

 
0
  #1
Nov 21st, 2008
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
  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
  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!
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 939
Reputation: MosaicFuneral is just really nice MosaicFuneral is just really nice MosaicFuneral is just really nice MosaicFuneral is just really nice MosaicFuneral is just really nice 
Solved Threads: 92
MosaicFuneral's Avatar
MosaicFuneral MosaicFuneral is offline Offline
Posting Shark

Re: Help with instance of one class in another

 
0
  #2
Nov 21st, 2008
Can I see the part where you try to access the NumDays()?
"Jedenfalls bin ich überzeugt, daß der Alte nicht würfelt."
"I became very sensitive to what will happen to all this and all of us." -Two geniuses named Albert
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 29
Reputation: jbrock31 is an unknown quantity at this point 
Solved Threads: 1
jbrock31 jbrock31 is offline Offline
Light Poster

Re: Help with instance of one class in another

 
0
  #3
Nov 21st, 2008
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.

  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
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 29
Reputation: jbrock31 is an unknown quantity at this point 
Solved Threads: 1
jbrock31 jbrock31 is offline Offline
Light Poster

Re: Help with instance of one class in another

 
0
  #4
Nov 21st, 2008
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
  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
  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. }
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC