-1

Imagine I have a class and it has a private value for example this value name is a, then I set it's value to 10.

How I can access to this variable with its value (10) in another class?

(I do not want to use friend function and friend class)

a.h

#include <iostream>
using namespace std;
static int s=0;

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

class a
{
    private:
    public:
        void sets(int );
};

a.cpp

#include "a.h"

void a::sets(int y){
    cin >> y;
    s=y;
}

main.cpp

#include"a.h"

int main()
{
    int i=0;
    int q;
    a a1;
    a1.sets(q);
    cout << s+1 << endl;
    for (i=1; i<5; i++){
        if (s == i) cout << "ok";
    }
}
cbuchart
  • 10,847
  • 9
  • 53
  • 93
keyvan
  • 29
  • 5
  • Make a getter function for `a`. – Joseph Evans Apr 11 '17 at 18:52
  • 5
    An answer to this question can be found in any book/tutorial dealing with C++ classes. Please take the time to go through such a book. That will be more helpful for you in the long run than getting an answer to this specific question. – R Sahu Apr 11 '17 at 18:52
  • Why not a pulic member `int get(){return privateMember;}`? – Rama Apr 11 '17 at 18:52
  • no its not my mean say code – keyvan Apr 11 '17 at 18:52
  • Why you use a static global variable (`s`) instead of a private member? – Rama Apr 11 '17 at 18:56
  • if i donot use static it get me error multi difinition s – keyvan Apr 11 '17 at 18:58
  • And the reason for that is every time a.h is included another `s` is defined. The linker doesn't know which which `s` is the real `s`, so it stops and complains. `static` makes a different `s` in every [translation unit](http://stackoverflow.com/questions/1106149/what-is-a-translation-unit-in-c) so the linker stops complaining, but now you have different `s`s. To do this right, and you don't need this here, Rama's answer is a better solution, you need to declare `extern int s;` in the header and in one and only one cpp file `int s;` to allocate storage for the single `s`. – user4581301 Apr 11 '17 at 19:55
  • [When to use extern in C++](http://stackoverflow.com/questions/10422034/when-to-use-extern-in-c) – user4581301 Apr 11 '17 at 19:56

3 Answers3

0

If you do not want to use friendship, add a public interface to this variable (setter and/or getter class members).

bipll
  • 11,747
  • 1
  • 18
  • 32
0

Either make the variable public or add a function to get the value that returns it, like int getA() const { return a; } .

Jesper Juhl
  • 30,449
  • 3
  • 47
  • 70
0

Use a private member s in your class a instead of a global variable, and access it via public Getter function GetS

Example:

#include <iostream>
using namespace std;

class a
{
    private:
        int s;

    public:
        void SetS() { cin >> s; }
        int GetS() const { return s; } 
};

int main()
{
    a a1;
    a1.SetS();
    int s1=a1.GetS();
    cout << s1+1 << endl;
    for (int i=1; i<5; i++){
            if (s1 == i) cout << "ok";
    }
}
Rama
  • 3,222
  • 2
  • 11
  • 26