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.