Thread: Acessing the data from one object from another object

  1. #1
    Registered User
    Join Date
    Mar 2008
    Location
    New York
    Posts
    24

    Acessing the data from one object from another object

    Hi ,

    I am a beginner in C++.

    I want to know how can we access data member of a class from another class. I am facing problem in visualizing the things in writing separate .h and .cpp file.

    Here I explain the things.We have three classes.

    In a.cpp (assuming the a.h files is wriiten )
    Code:
    #include "a.h"
     A::A()
    {
        int ii=1;
     }
    void  A:: fun_a()
    {
        cout<<"Value in A:"<<ii<<endl;
    }
    In b.h
    Code:
    #include "a.h"
    Class B
    {
        int jj;
        A b_aa;
    public:
        void fun_b();
        void change_ii();
    };
    In b.cpp
    Code:
    #include "b.h"
     B::B()
    {
        jj=2;
    }
    
    void B::fun_b()
    {
          cout<<"value in A:" << ii <<"value in B:" <<jj;
     }
    void B::change_ii()
    {
        ii=5;
    }
    // suppose both the functions are called and the value of ii is 5 and jj is 2
    //Now I want to access the data ii from and jj from the C class.
    //I want to assign kk a member of C class or a simple variable the value of ii (i.e. 5).

    How to go about it ?

    If declare a member of type B in our C class..it will carete another instance of B but does not contains the data in the live B object.

    Is passing a reference to the B class in the contructor of C class is asolution? If so, whow to do that. I am getting confused in implementin this concept.

    And what other ways are there?

    Thanks and regards,
    Alex



    Back to Top

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    Code:
    A::A()
    {
        int ii=1;
    }
    This looks like a mistake as ii is a local variable. You probably intended to write:
    Code:
    A::A()
    {
        ii=1;
    }
    But you might as well use the constructor's initialisation list:
    Code:
    A::A() : ii(1) {}
    Anyway, the ii member variable of A is (presumably) private. You would need to provide some kind of getter such that B and C objects can access it.

    If declare a member of type B in our C class..it will carete another instance of B but does not contains the data in the live B object.

    Is passing a reference to the B class in the contructor of C class is asolution? If so, whow to do that. I am getting confused in implementin this concept.
    I suggest that you give meaningful names to the classes and functions and explain what you are trying to solve. Sure, metasyntactic names are fine for demonstrating syntax, but if you want to know if a solution is applicable, you need to present the problem.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Mar 2008
    Location
    New York
    Posts
    24
    Thanks for ur reply.

    Will you please tell me how to acess that data members.
    I was using those kinds of name just to convey the idea.

    The real life problem may be like this:

    I have a HttpEngine class and it stores the data received from the parsed from the server in a XMLData class.

    I want to display this data in a different class called MyView.Where I want to access it.

    How is it implemented....

    regards,

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    If ii is not static, it exists only in instances of A. In B::fun_b you probably want to access ii in the instance called b_aa that is declared in B.
    Code:
    void B::fun_b()
    {
          cout<<"value in A:" << b_aa.ii <<"value in B:" <<jj;
     }
    Of course, if that doesn't work because ii is private in A, that means that ii of A was not meant to be accessed directly in the first place.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. xor linked list
    By adramalech in forum C Programming
    Replies: 23
    Last Post: 10-14-2008, 10:13 AM
  2. using this as synchronization object
    By George2 in forum C# Programming
    Replies: 0
    Last Post: 03-22-2008, 07:49 AM
  3. Mmk, I give up, lets try your way. (Resource Management)
    By Shamino in forum Game Programming
    Replies: 31
    Last Post: 01-18-2006, 09:54 AM
  4. [question]Analyzing data in a two-dimensional array
    By burbose in forum C Programming
    Replies: 2
    Last Post: 06-13-2005, 07:31 AM
  5. can't insert data into my B-Tree class structure
    By daluu in forum C++ Programming
    Replies: 0
    Last Post: 12-05-2002, 06:03 PM