Ion
Ion

Reputation: 101

How to access nested classes's data?

I'm learning C++ OOP and I met a problem with nested classes. I'm implementing a Notebook application which is formed from activities. In the CActivity class I have nested classes CAdress and CDate, which are friends with CActivity, so I should be able to access their data. However, when I'm creating a CActivity object, only CActivity's data (lchild, rchild and activity_name) is accessible.

I want to access the CDate and CAdress for everytime I'm creating an CActivity object to save their data. Notebook class would represent a Binary Search Tree where activities are sorted by date.

Here I will provide the code:

class CActivity {
    friend class Notebook;
private:
     CActivity* lchild, * rchild;
     string activity_name;

     class CAdress
     {
         friend class CActivity;
     private:
         string city_name;
         int city_code;
         string street;
         int street_number;
     public:
         void setCity(string name) { this->city_name = name; }
         void setCityCode(int code) { this->city_code = code; }
         void setStreet(string str) { this->street = str; }
         void setNumber(int nr) { this->street_number = nr; }
         CAdress(string name, int code, string street, int nr)
         {
              setCity(name);
              setCityCode(code);
              setStreet(street);
              setNumber(nr);
         };

     };
     class CDate
     {
         friend class CActivity;
     private:
         int day;
         int month;
         int year;
     public:
         void setDay(int dd) { this->day = dd; }
         void setMonth(int mm) { this->month = mm; }
         void setYear(int yyyy) { this->year = yyyy; }
         CDate(int dd, int mm, int yyyy)
         {
             setDay(dd);
             setMonth(mm);
             setYear(yyyy);
         }; 

     };

public:
    CActivity(string activity){
        activity_name = activity;
        lchild = rchild = NULL;

    }
};

I don't really understand where's the problem... Please help with an advice. Thank you!

Upvotes: 0

Views: 113

Answers (1)

sohel14_cse_ju
sohel14_cse_ju

Reputation: 2521

Nested class CAdress, CData has no object! CActivity class should have access to CAddress, CData class. You can put CAddress or CData objects-member in CActivity to provide access to them. Should expose public access.

class CActivity {
    public : 
            CData m_cData;
            CAddress m_address;

....

}

Upvotes: 2

Related Questions