| | |
Help with instance of one class in another
Thread Solved
![]() |
•
•
Join Date: Oct 2008
Posts: 29
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
C++ 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
C++ 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!
•
•
Join Date: Oct 2008
Posts: 29
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.
C++ 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
•
•
Join Date: Oct 2008
Posts: 29
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
C++ 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
C++ 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
- 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
| Thread Tools | Search this Thread |
api array based binary bitmap c++ c/c++ cdialogbar char class classes code coding compile console conversion convert count delete deploy desktop developer directshow dissertations dll double-linkedlist download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker loan loop looping loops map math matrix memory multiple news node number online output pagerank pointer problem program programming project python random read recursion recursive reference risk rpg string strings superclass temperature template test text text-file tree tutorial url validator variable vector video win32 windows winsock wordfrequency wxwidgets





