Click to See Complete Forum and Search --> : Questions about private data and other classes
sjaguar13
December 5th, 2002, 04:04 PM
If I had a date class with the private data month, day, and year, and I had a base class A with children classes B, and C, how would I use the month from the date in B? I have a B, and C variable, but I didn't use A. I don't know if that makes a difference.
galathaea
December 5th, 2002, 04:14 PM
Does the date class expose any methods for getting that data?
stober
December 5th, 2002, 04:17 PM
Is this what you mean? If not, please post an example. If it is, then class A cannot access the private members of class base.
class base
{
public:
base();
~base();
private:
int day,month,year;
};
class A public base
{
public:
A();
~A();
};
sjaguar13
December 5th, 2002, 05:04 PM
class Date {
public:
Date( int = 1, int = 1, int = 1990 );
int getMonth();
private:
int month;
int day;
int year;
};
int Date::getMonth()
{
return month;
}
class A {
private:
int Blah1
Date Blah2
};
class B : public A {
public:
B( const char *, const char *, double = 0.0, int = 0, Date = 0 );
}
B::B(...)
{
Blah2 = DatePassedIn;
if(Blah2.month = = Blah1 )
{
............
}
}
I need to get the integer from the one class that's not related at all, and the integer from the base class.
Yale
December 5th, 2002, 05:15 PM
B cannot access Blah1 and Blah2 at all, unless public methods are provided by A.
The only possible way for B to get access to A's field without other being able to would be for A to have Blah1 and Blah2 protected.
sjaguar13
December 6th, 2002, 01:14 AM
I made some public methods for A, but it still doesn't work. Well, the int Blah1 seems to work, but not the date. I have:
int A::getMonth()
{ return Blah2.month; }
but it says I cannot access private data from Date. Should I have a public function, getMonth(), in the Date class? How would I call that from A, Blah2.getDate()?
Yale
December 6th, 2002, 01:26 AM
Blah2's month field is private in Date.
So A cannot have access to it. For A to access Blah2's month you need a public method in Date, getMonth(), that return month (or this->month, to be more explicit). And A::getMonth call Blah2.getMonth() instead of Blah2.month.
Indeed, it might be more flexible to define a getDate() method
You still need A's public getMonth, otherwise you wouldn't be able to have access to Blah2 from B.
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.