Click here to Skip to main content
15,071,480 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
I have created the two class. I want to access a class variable in different class.

C++
#include <iostream>
using namespace std;

class A{
    
    public:
    A() { id = 0;}
    void setId(int num) { id  = num;}
    int getId() { return id; }
    
    private:
    int id;
};

class B{
    
    public:
    void getValue()
    {
    A obj;
    int id = obj.getId();
    std::cout << "Id : " << id << std::endl;
 }
};

int main()
{
    A obj;
    obj.setid(20);

    B b;
    b.getvalue();
    std::cout << obj.getid() << std::endl;
    return 0;
}


What I have tried:

I have tried to access a class variable in different class.
Output is not as expected:
Id : 0
20
Posted
Updated 15-Mar-21 10:29am
v2
Comments
KarstenK 14-Mar-21 14:29pm
   
the normal way is to use a get function, to ensure capsulation standards

In C++, you can access the values in two ways:

1. Encapsulation (or, by changing the protection level to the public, or protected in case of inheritance)
2. Creating a friend class

The first method is the easiest one, and I think is safer as well. You can simply change the access-level to the public or protected (if "class B" is a child of "class A"). Or, you can create getter/setter methods in the classes; which you are already doing.

For the friend class, you can add a declaration suggesting that "class B" is a friend of "class A" and thus can access the fields. For more on the friend types in C++, please see Friendship and inheritance - C++ Tutorials[^] and friend declaration - cppreference.com[^].

What's going on in your code?
The problem with your code is that you are not reusing the same A object in your B class.

In you class B, you have this:
C++
void getValue()
{
    A obj;
    int id = obj.getId();
    std::cout << "Id : " << id << std::endl;
 }

In the second line, you create a new instance of the A object. And you print the fresh value (which by default is zero in the constructor).

If you want to access the value of the A object, that you just set, try passing it to the method:

C++
void getValue(A *obj)
{
    int id = obj->getId();
    std::cout << "Id : " << id << std::endl;
}

I have used the pointer to an A object, you can read more on this here: Pointers - C++ Tutorials[^].

Lastly, you need to pass the A object to this method and this should show the correct result for you.

I created a dummy program for you to test: C++ Shell[^]. It shows the output that you expect.
   
Comments
Member 15025528 14-Mar-21 10:47am
   
Thanks Afzaal.
It's working fine.
Can I make unique pointer of one class that should point another class variable?
Afzaal Ahmad Zeeshan 14-Mar-21 15:43pm
   
You can, but that's overengineering. :)
It is entirely what is expected: You create an instance of A in Main called obj, and a separate instance of A in the B function getvalue. In main, you set a vlaue to the ID property of the A instance, in B.getvale you don't.
Because they are separate instances, they have different values.

Think of it this way: I take you for a drive in my car, and you put your mobile in the glove box. I drop you off, and drive away. You remember your mobile and go your your car - same make, same model, same colour - and open the glove box.
Is your mobile in there?

Of course not: they are different physical cars! Each car is a separate instance of the class Car.

And the same thing happens in C++: you create two different objects, they have different values! It is possible to make what you are trying work using static variables, but you need to understand instances and how they work first!
   
Comments
Member 15025528 14-Mar-21 10:50am
   
Thanks for a very good and understandable explanation.
I Understood the flow.
OriginalGriff 14-Mar-21 10:56am
   
You're welcome!
Aren't you just needing to set id as a static variable within the class? i.e. just add 'static' ....

static int id;
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900