CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Jun 2002
    Posts
    114

    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.

  2. #2
    Join Date
    Sep 2002
    Posts
    1,747
    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

  3. #3
    Join Date
    Jun 2002
    Posts
    1,417
    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();
    };

  4. #4
    Join Date
    Jun 2002
    Posts
    114
    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.

  5. #5
    Join Date
    Dec 2002
    Posts
    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.

  6. #6
    Join Date
    Jun 2002
    Posts
    114
    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()?

  7. #7
    Join Date
    Dec 2002
    Posts
    5
    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
  •  





Click Here to Expand Forum to Full Width

Featured