-
C++ (
http://www.daniweb.com/forums/forum8.html)
| jbrock31 | Nov 21st, 2008 12:38 am | |
| Help with instance of one class in another 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
#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
#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! |
| MosaicFuneral | Nov 21st, 2008 1:12 am | |
| Re: Help with instance of one class in another Can I see the part where you try to access the NumDays()? |
| jbrock31 | Nov 21st, 2008 1:17 am | |
| 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.
#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 |
| jbrock31 | Nov 21st, 2008 11:10 am | |
| 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
#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
#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();
} |
| All times are GMT -4. The time now is 2:08 pm. | |
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC