How to make classes access eachothers values?

Wow! It's been a LONG time since coding but am just getting back to it and have lost SOOO much knowledge. I just have a question about classes...more specifically inheritance I think. Say for instance I have 2 classes. One called playerClass and one called getInfoClass. My problem lies in getting the two classes to talk to one another. I gather info from the user in the getInfoClass and need to set a lot of variables in the playerClass from the values entered using functions inside the getInfoClass. I cannot remember but would you use inheritance to get the classes to talk to one another??? Here is a snippet of some of the code.

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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
  class playerClass
{
public:
        playerClass();
        void setName(string);
        void setAge(string);
        void setGender(string);
        void setBodyType(string);
        void printName();
private:
        ///The value of these 4 variables below will come from the getInfoClass
        string name;
        string age;
        string gender;
        string bodyType;
};


class getInfoClass
{
public:
        getInfoClass();
        void errorCheckAge(int&);
        void getPlayerInfo();
        void errorCheckInfo(string&, vector<string>);
        void capitalizeFirstLetters(string&);

private:
        int age;
        string name;
        string gender;
        string bodyType;
        bool isThereError;
        vector<string>genderVector;
        vector<string>bodyTypeVector;
};





///HERE IS THE DEFINTION OF GETTING THE INFO. AFTER A VALUE IS ENTERED I WANT
///TO SET THE VALUE RIGHT IN THIS CLASS DEFINTION
void getInfoClass::getPlayerInfo()
{
    cout<<"What is your name? ";
    getline(cin, name);
    capitalizeFirstLetters(name);
    cout<<endl;

    cout<<"What is your age?\nEnter between 13 and 99: ";
    cin>>age;
    errorCheckAge(age);
    cout<<endl;

    cout<<"What is your gender?\nEnter male of female: ";
    cin>>gender;
    errorCheckInfo(gender, genderVector);
    cout<<endl;

    cout<<"What is your body type?\nEnter small, average, or massive: ";
    cin>>bodyType;
    errorCheckInfo(bodyType, bodyTypeVector);
    cout<<endl;
}



Time to dust off my book......thanks for any replies!
I'm new to inheritence so I'm not sure if it is possible using inheritence although I think it is possible by the use of operator=

even you could without inheritence by only overloading operator=

This method would work however the playerClass will grow bigger because it will contain all the elements of getInfoClass
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
32
class playerClass
{
public:
        playerClass();
        void setName(string);
        void setAge(string);
        void setGender(string);
        void setBodyType(string);
        void printName();
private:       //could be public depending on how you would like to use getInfoClass
       getInfoClass Info;     //must initialise Info in the constructor if private
};


class getInfoClass
{    friend class playerClass;
public:
        getInfoClass();
        void errorCheckAge(int&);
        void getPlayerInfo();
        void errorCheckInfo(string&, vector<string>);
        void capitalizeFirstLetters(string&);

private:
        int age;
        string name;
        string gender;
        string bodyType;
        bool isThereError;
        vector<string>genderVector;
        vector<string>bodyTypeVector;
};
Last edited on
A little late on the reply but thanks to both of you. Inheritance was the way to go.
Topic archived. No new replies allowed.