-6

Request: I have solved my problem so, i request everyone to please stop down voting the question. I have written my Answer below my question.

I have three classes i.e.

class-A ,

class-B ,

class-C.

  • A parameter named int value is declared in class A
  • I want to initialize it in class B i.e. A.value = 0;
  • and finally i want to use that value in class C

Problem: How do i access the same value?

Solution: I HAVE ALREADY SOLVED THE PROBLEM. AND THIS IS MY SOLUTION

My three classes;

class A
{
    int value;    
}

class B
{
    void setValue();    
}

class C
{
    void useValue;    

}

Now, the methods of the above classes

void B:setValue()
{
    A object;
    object.value = 10;    
    useValue(object);
}

void C:useValue(A object)
{

    cout<<"Parameter: "<<object.value;
}
skm
  • 5,015
  • 8
  • 43
  • 104

2 Answers2

2

SOLUTION 1: What you can do is to pass the object you need to work on as a parameter to the functions in class B and C.

Look at the following piece of code:

class A
{
    public:
    int parameter;
};

class B
{
    public:
    void setParameter(A &obj);
};

class C
{
    public:
    void accessParameter(A &obj);
};

//defining the functions

void B::setParameter(A &object)
{   
    object.parameter = 10;
}

void C::accessParameter(A &object)
{    
    std::cout<<"The value of parameter is: "<<object.parameter;
}

I hope this solves your problem. If not, post in the comments.

NOTE: You will need to make an object of A in main function and pass it to the methods of class B and C.


SOLUTION 2: Another thing you can do is set up an inheritance chain:

C inherits B which inherits A.

So,

class A
{
    public:
    int parameter;
};

class B : public A
{
    public:
    void setParameter(){
        parameter = 10; 
    }
};

class C : public B
{
    public:

    C(){
        setParameter();
    }

    void accessParameter(){
        std :: cout << parameter;
    }

};

SOLUTION 3

Another solution would be making parameter a static field.

Code:

#include<iostream>

class A
{
    public:
    static int parameter;
};

int A::parameter;

class B
{
    public:
    void setParameter();
};

class C
{
    public:
    void accessParameter();
};

//defining the functions

void B::setParameter()
{   
    A::parameter = 10;
}

void C::accessParameter()
{    
    std::cout<<"The value of parameter is: "<< A::parameter;
}

int main(){

    B objB;
    objB.setParameter();
    C objC;
    objC.accessParameter(); 
    return 0;
}
Crystal Meth
  • 589
  • 1
  • 4
  • 16
  • thanks but in my case, they is no connection between them, i mean they are not connected through the same `main()` – skm Feb 25 '14 at 18:03
  • 1
    Frankly, it would be better if you gave a proper problem statement including all the conditions. Please give the exact question that has been asked and not what you interpret the question as. I hope you understand. – Crystal Meth Feb 25 '14 at 18:06
  • I am not copying the question from somewhere or given to me by someone. I am facing this problem in my Project so, i have written the simplified version of my problem instead of posting the hi-fi code. – skm Feb 25 '14 at 18:09
  • thanks but didn't i mention clearly this time that they are independent then how can i use inheritance? – skm Feb 25 '14 at 18:17
  • In simpler words the problem is to set the parameter value in `class-B` which remain same for `class-2` as well by keeping my structure same. – skm Feb 25 '14 at 18:19
  • why do i need void `(A &object)` as argument in `void B::setParameter(A &object)` in your solution-3 – skm Feb 25 '14 at 18:22
  • yes, i thought so. I tried your method but then i am getting an error that `undefined reference to A::parameter ` for the line `A::parameter = 10;` – skm Feb 25 '14 at 18:25
  • @skm : Made some modifications. Code now works properly. If this helped please +1 and accept the answer. ;) – Crystal Meth Feb 25 '14 at 18:39
  • I checked in a seperate file, it is working but in my project where each class is defined in separate "header.h" files it is giving me error. I have not forgotten to use `#include "header.h"` . Anyways i am giving it +1 – skm Feb 25 '14 at 18:51
  • @skm: Headers are not for initialization. They are for providing interface declarations. [Taken from here!](http://stackoverflow.com/questions/18860895/way-to-initialize-static-members-in-the-header) – Crystal Meth Feb 26 '14 at 14:51
  • But when/where did i say that i am initializing in headers? – skm Feb 26 '14 at 16:34
  • @skm : I think you need to research the problem a bit more. I am just a student with not much experience with headers. That was something I found that I thought might help you. :) – Crystal Meth Feb 27 '14 at 09:07
0

object is local variable here. It is deleted when program lives setParameter() function

void B::setParameter()
{
    A object;
    object.parameter = 10;
}
Avt
  • 16,927
  • 4
  • 52
  • 72
  • Yes, i just forgot that fact but i have updated my question. I must access the value in from other class – skm Feb 25 '14 at 17:48