Simple Question, I think

This is a discussion on Simple Question, I think within the C++ Programming forums, part of the General Programming Boards category; I have two classes A and B. class A has it's member functions as public and its data types as ...

  1. #1
    Unregistered
    Guest

    Simple Question, I think

    I have two classes A and B.

    class A has it's member functions as public and its data types as private. Class B also has it member functions as public and data types as private. BUT (here's my problem) in class B one of the datatypes is A example....

    // This is class B
    private:
    A a;
    char* string;
    static int count;

    i have to access class A WITHOUT using inheritance, only using class composition. When i compile i get the error "var1" cannot access private member of class A. If i cant use inheritance how do i access its members. Secondly, in class B i have to use the default constructor to initialize the the data types from class A.
    example....
    class A has....
    int a, b,c

    i've done it like this...but its wrong

    B(a.a,a.b,a.c) //class B constructor intializing data types from class A

    anyone has any ideas?
    Hopefully this gives u an understanding....

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Perhaps you could make a public function (method) that returns the value you wish to have.
    Code:
    class MyFunc
    {
      public:
        int ReturnValue();
      private:
        int PrivateValue;
    };
    
    int MyFunc::ReturnValue()
    {
      return PrivateValue;
    }
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    Registered User
    Join Date
    Jan 2002
    Posts
    559
    a ought to initialize in its constructor. Are you providing default values for the constructors in A? Otherwise, provide default values when you declare a in B (and have the appropriate constructor in A).
    Also, do you #include whatever header file A is in in B? That's your most likely problem.

  4. #4
    Registered User
    Join Date
    Mar 2002
    Posts
    203
    since class A's private members can only be accessed by its own member functions, then you need class B's constructor to call the public member functions of class A that modify the private members (accessors). I may be wrong or be doing it the long/hard way.

  5. #5
    Registered User
    Join Date
    Jan 2002
    Posts
    559
    Originally posted by Syneris
    since class A's private members can only be accessed by its own member functions, then you need class B's constructor to call the public member functions of class A that modify the private members (accessors). I may be wrong or be doing it the long/hard way.
    To modify the private members of A, yes, you need public methods in A accessible to B. But to create an instance of A, the constructors should do the job. I bet #including in whatever file B is in in whatever file A is defined in will do the trick.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple question regarding variables
    By Flakster in forum C++ Programming
    Replies: 10
    Last Post: 05-18-2005, 09:10 PM
  2. Simple class question
    By 99atlantic in forum C++ Programming
    Replies: 6
    Last Post: 04-21-2005, 12:41 AM
  3. Simple question about pausing program
    By Noid in forum C Programming
    Replies: 14
    Last Post: 04-02-2005, 09:46 AM
  4. simple question.
    By InvariantLoop in forum Windows Programming
    Replies: 4
    Last Post: 01-31-2005, 12:15 PM
  5. simple fgets question
    By theweirdo in forum C Programming
    Replies: 7
    Last Post: 01-27-2002, 06:58 PM

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