Accessing Classes in Classes

This is a discussion on Accessing Classes in Classes within the C++ Programming forums, part of the General Programming Boards category; In a nutshell, I am trying to access a private data member of a class, which is in a class ...

  1. #1
    Registered User GrNxxDaY's Avatar
    Join Date
    Jul 2002
    Posts
    140

    Arrow Accessing Classes in Classes

    In a nutshell, I am trying to access a private data member of a class, which is in a class (yes, a class in a class)
    I want Phonebook::display() to show Phonebook.addr.house

    I get an error telling me I can't access it because it's private. So... do I have to put that display() function inside Address?

    Code:
    #include <iostream>
    #include <cstdlib>
    
    class Address
    {
     private:
        unsigned int house;
        unsigned int street;
     public:
        Address(unsigned int h = 0, unsigned int s = 0)
          {house = h; street = s;}
    };
    
    class Phonebook
    {
     private:
            unsigned int fax;
            unsigned int phone;
            Address addr;
    
     public:
      Phonebook(unsigned int f = 0, unsigned int p = 0, Address a = 0)
        {fax = f; phone = p; addr = a;}
      void display();
    };
    
    void Phonebook::display()
    {
     std::cout << addr.house;
    }
    
    int main()
    {
     Phonebook brian(5821945, 5827052, (8724, 87));
     brian.display();
     
     system("pause");
     return 0;
    }
    AOL: GrNxxDaY
    IDE: Dev-C++ Beta 5 (v4.9.4.1)
    Project: Eye of Sahjz (text-RPG)
    If you think I may need help, please IM me.

  2. #2
    Registered User
    Join Date
    Jul 2002
    Posts
    273
    you probably shouldn't do that. private is for a good reason. But you could certainly make it public. On the other hand you could make Phonebook a friend of Address. Whatever. I don't care
    always looking, make an offer. get me out of this place.

  3. #3
    Banned frenchfry164's Avatar
    Join Date
    Oct 2001
    Posts
    1,552
    one thing I can see is you're trying to make a class equal 0. You can't make a class equal a number, unless you memset() it or something.

  4. #4
    Registered User GrNxxDaY's Avatar
    Join Date
    Jul 2002
    Posts
    140
    Originally posted by frenchfry164
    one thing I can see is you're trying to make a class equal 0. You can't make a class equal a number, unless you memset() it or something.
    I dun get any errors though *shrug*.
    AOL: GrNxxDaY
    IDE: Dev-C++ Beta 5 (v4.9.4.1)
    Project: Eye of Sahjz (text-RPG)
    If you think I may need help, please IM me.

  5. #5
    Registered User GrNxxDaY's Avatar
    Join Date
    Jul 2002
    Posts
    140
    I want to get this right though. I have here a simple example of what I am trying to do.
    Code:
    class Date
    {
     private:
       int day, month, year;
    };
    
    class Employee
    {
     private:
       int id;
       Date datehired;
     
     public:
       Employee(int i = 0, Date d /*what goes here*/)
         {id = i; datehired = d;}
    };
    
    int main()
    {
     Employee brian(0, (7, 29, 02));
     
     return 0;
    }
    What would go in the Employee constructor parameter? Where type Date is specified.
    AOL: GrNxxDaY
    IDE: Dev-C++ Beta 5 (v4.9.4.1)
    Project: Eye of Sahjz (text-RPG)
    If you think I may need help, please IM me.

  6. #6
    Seeking motivation... endo's Avatar
    Join Date
    May 2002
    Posts
    537
    You dont have to have anything there, just put that parameter first.
    Code:
    Employee::Employee( Date d, int i = 0 )
    {
       id = i; 
       datehired = d;
    }
    
    Date briansdate( 1, 2, 3 );
    Employee brian( 100, briansdate );
    You might want to include some error handling first though, to ensure a valid date.
    Last edited by endo; 07-29-2002 at 03:30 PM.

  7. #7
    Registered User GrNxxDaY's Avatar
    Join Date
    Jul 2002
    Posts
    140
    Is there a way to put it in the Employee destructor though? i really need it in there for what I am doing.
    AOL: GrNxxDaY
    IDE: Dev-C++ Beta 5 (v4.9.4.1)
    Project: Eye of Sahjz (text-RPG)
    If you think I may need help, please IM me.

  8. #8
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    There is no reason to have private data if it cannot be accessed!
    There must be a mechanism to do so:

    Code:
    class Date
    {
     private:
       
      int day, month, year;
    
      public:
    
      void SetDate(int _day, int _month, int year)
      { day = _day; //...etc..}
    
      int GetDay(){ return day; }
    };


    However, for now you should completely abandon private utilization till you actually need it. Mostly it is used to protect members you don't want changed from programmers who don't have access to the source, or to foolproof the class, etc.
    Here's a good example:

    Code:
    
    class Foo {
    
    private:
    
    time_t moment_of_creation; //..time this instance was created...
    
    public:
    
    time_t get_time_of_creation() { return moment_of_creation;  }
    
    Foo() { moment_of_creation = clock(); }
    
    };

    As you can see, if this code were in a compiled lib, and a programmer using the objects tried to do this:

    Foo bar;

    //...code...

    bar.moment_of_creation -= some_adjustment;

    As you can see, he cannot do so. These types of reasons were part of the driving force of such mechanisms, but in most applications, using private is really unnecesary...hope that helps.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  9. #9
    Registered User GrNxxDaY's Avatar
    Join Date
    Jul 2002
    Posts
    140
    What?

    I'm trying to put a class as a data member in a different class. For example, the Date class in the Employee class. I want to be able to initialize an employee object like this:
    Employee brian(0, 17, (7,29,02));
    Where 0 and 17 are private data members of the Employee class, and 7, 29, and 02 are data members of the Date class inside the employee class.

    How would I set up the constructors to do so?
    AOL: GrNxxDaY
    IDE: Dev-C++ Beta 5 (v4.9.4.1)
    Project: Eye of Sahjz (text-RPG)
    If you think I may need help, please IM me.

  10. #10
    Registered User
    Join Date
    Jun 2002
    Posts
    267
    You can't do it like that.... use spacing in the argument list instead
    Code:
    class Date;
    class Employee
    {
    public:
    	Employee(int a, int b, int c, int d, int e)
    	{
    		x = a;
    		y = b;
    		Date(c, d, e);
    	}
    	class Date
    	{
    	public:
    		Date(int c, int d, int e)
    		{
    			x = c;
    			y = d;
    			z = e;
    		}
    	private:
    		int x,y,z;
    	};
    private:
    	int x,y;
    };
    Like Employee brian(0,17, 7,29,02)
    Last edited by d00b; 07-30-2002 at 12:11 AM.

  11. #11
    Registered User GrNxxDaY's Avatar
    Join Date
    Jul 2002
    Posts
    140
    wow, i think that is what i'm looking for d00by.
    can you explain why you put the class declaration thingy inside the other class? advanatages/disadvantages/necessary/why? all that good stuff.
    AOL: GrNxxDaY
    IDE: Dev-C++ Beta 5 (v4.9.4.1)
    Project: Eye of Sahjz (text-RPG)
    If you think I may need help, please IM me.

  12. #12
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Here's one way to do it: since only the class itself can access it's private members, let it display them itself...




    Code:
    
    
    
    
    
    //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>//
    //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<//
    //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>//
    //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<//
    
    
    class Date {
    
    int day, month, year;
    
    public:
    
    
    //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
    
    
    Date(int _day = 0, int _month = 0, int _year = 0): day(_day), month(_month), year(_year){}
    
    
    //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
    
    
    
    void SetDate(int _day = 0, int _month = 0, int _year = 0)
     {
      day = _day, month = _month, year = _year;
     }
    
    
    //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
    
    
    void Display(){ cout << day << "/" << month << "/" << year << endl; }
    
    
    //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
    
    };
    
    
    //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>//
    //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<//
    //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>//
    //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<//
    //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>//
    //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<//
    //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>//
    //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<//
    
    
    
    
    class Employee {
    
    public:
    
    char name[100];
    
    Date date_of_hire;
    
    
    //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
    
    
    
    Employee(char *_name = "Unknown", int day = 0, int month = 0, int year = 0)
     {
      strcpy(name, _name);
    
      date_of_hire.SetDate(day, month, year);
     }
    
    
    //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
    
    
    
     Employee(char *_name, Date today)
     {
      strcpy(name, _name);
    
      date_of_hire = today;
     }
    
    
    //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
    
    
    void Display()
     {
      cout << "Employee: " << name << endl << "Date Of Hire: ";
    
      date_of_hire.Display();
    
      getch();
     }
    
    //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
    
    };
    
    //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>//
    //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<//
    //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>//
    //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<//
    
    
    
    int main(int argc, char *argv[])
    {
      Date tuesday( 7, 30, 2002 );
    
      Employee jack("Jack", tuesday);
    
      Employee jill("Jill", 7, 28, 2001);
    
      Employee anonymous;
    
      jack.Display();
      jill.Display();
      anonymous.Display();
    
      return 0;
    }


    Also note the beauty of C++ is that
    Date a(1, 10, 98);
    Date b;
    b = a;

    'b' now contains an exact copy of a.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  13. #13
    Registered User GrNxxDaY's Avatar
    Join Date
    Jul 2002
    Posts
    140
    yes, that is what i'm trying to do. i'm not sure i understand your syntax though.
    Date(int _day = 0, int _month = 0, int _year = 0): day(_day), month(_month), year(_year){}
    this can be summed up in 1 word: WHAT?

    Also, is it necessary to have that setdate member function? why not let the Date constructor do it? Like Date(7, 30, 02); ?

    Maybe I need a new book that teaches classes. OOO i konw! i'll go to Barnes' and Noble and read all the C++ books there. ahaa i genious*
    AOL: GrNxxDaY
    IDE: Dev-C++ Beta 5 (v4.9.4.1)
    Project: Eye of Sahjz (text-RPG)
    If you think I may need help, please IM me.

  14. #14
    Registered User
    Join Date
    Apr 2002
    Posts
    95
    Perhaps I'm off a bit being a newbie but why not make the contained class protected.

  15. #15
    Registered User GrNxxDaY's Avatar
    Join Date
    Jul 2002
    Posts
    140
    hey sebasatiani! i think that code of yours is going to work! thanks!!.

    i dont know what protected means, so im gonna ignore it and hope i never need it =)) (spoken like a true n00b)
    AOL: GrNxxDaY
    IDE: Dev-C++ Beta 5 (v4.9.4.1)
    Project: Eye of Sahjz (text-RPG)
    If you think I may need help, please IM me.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Accessing classes
    By Mavix in forum C# Programming
    Replies: 2
    Last Post: 02-16-2008, 09:43 PM
  2. Help accessing classes and derived classes
    By hobbes67 in forum C++ Programming
    Replies: 8
    Last Post: 07-14-2005, 03:46 PM
  3. accessing classes and reversing inputs
    By 2fastwrx in forum C++ Programming
    Replies: 6
    Last Post: 12-06-2004, 09:16 AM
  4. 2 Classes Accessing Eachother
    By Chronom1 in forum C++ Programming
    Replies: 2
    Last Post: 10-11-2003, 06:10 PM
  5. Replies: 8
    Last Post: 07-27-2003, 02:52 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21