Help with using private members in other classes

I'm pretty new to programming as a whole, and I have a question about private members and stuff. Alright, so let's say I have two classes.
1
2
3
4
5
6
7
8
9
class A
{
    public:
        A();
        int functionA();
        A Object;
    private:
        int a;
}

and
1
2
3
4
5
6
7
8
class B
{
    public:
        B();
        void functionB();
    private:
        int b;
}

What if I need to use int a from the first class in functionB? How do I do that?

In my project functionB just simply needs to print int a out, it doesn't change its value.

1
2
3
4
void B::functionB()
{
    cout << Object.a;
}

So I tried making int a a public member, it still doesn't work. (and yes, I've included the necessary stuff in the header)

Another thing I tried is declaring class B as a friend class in the first one:

1
2
3
4
5
6
7
8
9
10
class A
{
    public:
        A();
        int functionA();
        A Object;
        friend class B;
    private:
        int a;
}


It still doesn't work. It doesn't give me any errors, but nothing gets printed out.. However when I tried printing it out in main, it worked. So what do I need to do?
Last edited on
1
2
3
4
5
6
7
8
9
class A
{
    public:
        A();
        int functionA();
        A Object; //can't have this, size would be infinite
    private:
        int a;
} //missing semicolon 
when asking about code make sure that the snips that you provide do reproduce the issue.


1
2
3
4
5
6
7
8
9
10
11
void B::functionB(const A &obj){
   std::cout << obj.a;
}
//or
void B::functionB(int n){
   std::cout << n;
}

void A::foo(const B &b){
   b.functionB( a.n );
}
OK, so basically I have 2 .cpp files and 2 headers:

Selection.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#ifndef SELECTION_H_INCLUDED
#define SELECTION_H_INCLUDED

#include <iostream>
#include <string>

using namespace std;

class Selection
{
    public:
        string setName(int);
        string getName();
    private:
        string selectedName;
};

#endif // SELECTION_H_INCLUDED 


Selection.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include "Selection.h"

string Selection::setName(int numberIndex)
{
    switch(numberIndex)
    {
        case 1:
            selectedName = "John";
            break;
        case 2:
            selectedName = "Mary";
            break;
        case 3:
            selectedName = "James";
            break;
        default:
            break;
    }

    return selectedName;
}

string Selection::getName()
{
    return selectedName;
}


PrintInfo.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#ifndef PRINTINFO_H_INCLUDED
#define PRINTINFO_H_INCLUDED

#include <iostream>
#include <string>

using namespace std;

class PrintInfo
{
    public:
        void printInformation(string);
    private:
};

#endif // PRINTINFO_H_INCLUDED 


PrintInfo.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include "PrintInfo.h"

void PrintInfo::printInformation(string name)
{
    if(name == "John")
    {
        cout << "Info about 'John'" << endl;
    }
    if(name == "Mary")
    {
        cout << "Info about 'Mary'" << endl;
    }
    if(name == "James")
    {
        cout << "Info about 'James'" << endl;
    }
}


And here's the main function:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
#include <string>

#include "Selection.h"
#include "PrintInfo.h"

using namespace std;

int main()
{
    int n = 0;
    string name;

    Selection Selection;
    PrintInfo PrintInfo;

    while(n < 1 || n > 3)
    {
        cout << "Please enter a value from 1-3" << endl;
        cin >> n;
    }

    Selection.setName(n);
    name = Selection.getName();
    PrintInfo.printInformation(name);

    return 0;
}


OK, so this stuff here is working.

What I was thinking, on the other hand, is that maybe I could include "Selection.h" in PrintInfo.cpp, made selectedName public, and use it in printInformation (the function), so it doesn't need the string parameter anymore, and just have something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include "PrintInfo.h"
#include "Selection.h"

void PrintInfo::printInformation()
{
    if(selectedName == "John")
    {
        cout << "Info about 'John'" << endl;
    }
    if(selectedName == "Mary")
    {
        cout << "Info about 'Mary'" << endl;
    }
    if(selectedName == "James")
    {
        cout << "Info about 'James'" << endl;
    }
}


This way I wouldn't need to declare name in main:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
#include <string>

#include "Selection.h"
#include "PrintInfo.h"

using namespace std;

int main()
{
    int n = 0;

    Selection Selection;
    PrintInfo PrintInfo;

    while(n < 1 || n > 3)
    {
        cout << "Please enter a value from 1-3" << endl;
        cin >> n;
    }

    Selection.setName(n);
    PrintInfo.printInformation();

    return 0;
}


So what I'm asking is.. is there any way I could somehow make the latter version work?

EDIT: Now that I look at it, both functions from Selection return the same variable.. I know it doesn't really make sense this way but I hope my question is still clear.

If it's still not clear.. well.. how do I use functions/variables (let them be private or public) in other classes? Cause simply including the header in the other class doesn't work.
Last edited on
Why is PrintInfo even a class? What object does it represent?
Why not just add a print function to your Selection class?
Last edited on
Yeah, I'm sorry, I know it's not the best example, but I can't think of a better one.

OK, whatever, I managed to get the last version to work. Sorta..

main

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
#include <string>

#include "Selection.h"
#include "PrintInfo.h"

using namespace std;

int main()
{
    int n = 0;
    string name;

    Selection Selection;
    PrintInfo PrintInfo;

    while(n < 1 || n > 3)
    {
        cout << "Please enter a value from 1-3" << endl;
        cin >> n;
    }

    Selection.setName(n);
    PrintInfo.printInformation();
    cout << Selection.selectedName;

    return 0;
}


Selection.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#ifndef SELECTION_H_INCLUDED
#define SELECTION_H_INCLUDED

#include <iostream>
#include <string>

using namespace std;

class Selection
{
    public:
        string setName(int);
        string getName();
        void print();
        string selectedName;
    private:
};

#endif // SELECTION_H_INCLUDED 


Selection.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include "Selection.h"

string Selection::setName(int numberIndex)
{
    switch(numberIndex)
    {
        case 1:
            selectedName = "John";
            break;
        case 2:
            selectedName = "Mary";
            break;
        case 3:
            selectedName = "James";
            break;
        default:
            break;
    }

    return selectedName;
}

string Selection::getName()
{
    return selectedName;
}

void Selection::print()
{
    cout << selectedName;
}


PrintInfo.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#ifndef PRINTINFO_H_INCLUDED
#define PRINTINFO_H_INCLUDED

#include <iostream>
#include <string>
#include "Selection.h"

using namespace std;

class PrintInfo
{
    public:
        void printInformation();
    private:
        Selection RandomObject;
};

#endif // PRINTINFO_H_INCLUDED 


PrintInfo.cpp

1
2
3
4
5
6
#include "PrintInfo.h"

void PrintInfo::printInformation()
{
    cout << RandomObject.selectedName;
}


Alright, so I made selectedName public, I included "Selection.h" in PrintInfo.h, I also declared an object to access the stuff from Selection, and printInformation should, theoretically, print the selected name out. Yet it doesn't. But if I print it out in main, it works.. Why is that? Why doesn't printInformation.. work?
¿how do you expect the compiler to figure out that `RandomObject' refers to the `Selection' object in main?

You need to review the material on classes and instances (objects), and learn to differentiate them.
Topic archived. No new replies allowed.