class Student{
public:
Student(int test)
:key(705)
{
if(test == key)
{cout << "A student is being verified with a correct key: "<< test << endl;
allow=1;
}
else
{
cout << "Wrong key" ;
}
}
friend void printResult();
private:
const int key;
int allow;
};
void printResult()
{
if(allow==1)
{
cout<< " Maths: 75 \n Science:80 \n English: 75" << endl;
}
}
int main()
{
int testkey;
cout << "Enter key for Bob: ";
cin >> testkey;
Student bob(testkey);
printResult();
}
The function printResult can't seem to access the variable allow, which is private, from the Student class. Did I prototype the printResult in the wrong place or was the syntax wrong? AFAIK, we can prototype friends anywhere in the class.