Help with instance of one class in another
Please support our C++ advertiser: Programming Forums
Thread Solved
![]() |
•
•
Posts: 27
Reputation:
Solved Threads: 1
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
and now TimeOff.H
Thank you!
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
cplusplus Syntax (Toggle Plain Text)
#ifndef NUMDAYS #define NUMDAYS #include <iostream> class NumDays { private: int hours; float days; float calcDays(); public: NumDays() { hours = 0; days = 0.0; } void setHours(int hrs) { hours = hrs; } int getHours() { return hours; } float getDays(); NumDays operator + ( const NumDays& ); NumDays operator - ( const NumDays& ); NumDays operator --(); NumDays operator ++(); NumDays operator ++ ( int ); NumDays operator -- ( int ); }; #endif
and now TimeOff.H
cplusplus Syntax (Toggle Plain Text)
#ifndef TIMEOFF #define TIMEOFF #include <iostream> #include "NumDays.h" #include <string> using namespace std; class TimeOff { private: string name; int idNum; NumDays maxSickDays; public: TimeOff(); void setName ( string n ) { name = n; } void setIdNum ( int id ) { idNum = id; } string getName() { return name; } int getIdNum() { return idNum; } }; #endif
Thank you!
•
•
Posts: 27
Reputation:
Solved Threads: 1
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.
cplusplus Syntax (Toggle Plain Text)
#ifndef TIMEOFF #define TIMEOFF #include <iostream> #include "NumDays.h" #include <string> using namespace std; class TimeOff { private: string name; int idNum; NumDays maxSickDays; public: TimeOff(); void setName ( string n ) { name = n; } void setIdNum ( int id ) { idNum = id; } void setMaxSickDays( int h ) { maxSickDays.setHours(h); } /// trying to access this instance of NumDays string getName() { return name; } int getIdNum() { return idNum; } }; #endif
•
•
Posts: 27
Reputation:
Solved Threads: 1
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
now TimeOff.cpp
TimeOff.h
cplusplus Syntax (Toggle Plain Text)
#ifndef TIMEOFF #define TIMEOFF #include <iostream> #include "NumDays.h" #include <string> using namespace std; class TimeOff { private: string name; int idNum; NumDays maxSickDays; public: TimeOff(); void setName ( string n ) { name = n; } void setIdNum ( int id ) { idNum = id; } void setMaxSickDays( int ); float getMaxSickDays(); string getName() { return name; } int getIdNum() { return idNum; } }; #endif
now TimeOff.cpp
cplusplus Syntax (Toggle Plain Text)
#include "TimeOff.h" #include "NumDays.h" #include <iostream> #include <string> using namespace std; TimeOff::TimeOff() { name = ""; idNum = 0; maxSickDays.setHours(0); // sickTaken.setHours(0); // maxVacation.setHours(0); // vacTaken.setHours(0); // maxUnpaid.setHours(0); // unpaidTaken.setHours(0); } void TimeOff::setMaxSickDays ( int hrs ) { maxSickDays.setHours(hrs); } float TimeOff::getMaxSickDays() { return maxSickDays.getDays(); }
![]() |
Similar Threads
Other Threads in the C++ Forum
- about reference of a instance in class (C++)
- How To create an instance of a Class in Visual Basic (Visual Basic 4 / 5 / 6)
- How can I create an explicit instance of a class? (VB.NET)
- Instantiating class objects?? (C++)
- problems accessing class instances (Java)
- Accessing a variable of another class (Java)
- problems with referring to class variables (Java)
- writing a class without the class declaration? (C++)
- Bank account class (C++)
Other Threads in the C++ Forum
- Previous Thread: UDP-Sockets chat application question
- Next Thread: Crash Windows
•
•
•
•
Views: 705 | Replies: 3 | Currently Viewing: 1 (0 members and 1 guests)





Linear Mode