|
-
December 5th, 2002, 05:04 PM
#1
Questions about private data and other classes
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.
-
December 5th, 2002, 05:14 PM
#2
Does the date class expose any methods for getting that data?
*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/
"It's hard to believe in something you don't understand." -- the sidhi X-files episode
galathaea: prankster, fablist, magician, liar
-
December 5th, 2002, 05:17 PM
#3
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.
Code:
class base
{
public:
base();
~base();
private:
int day,month,year;
};
class A public base
{
public:
A();
~A();
};
-
December 5th, 2002, 06:04 PM
#4
Code:
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.
-
December 5th, 2002, 06:15 PM
#5
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.
-
December 6th, 2002, 02:14 AM
#6
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:
Code:
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()?
-
December 6th, 2002, 02:26 AM
#7
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|