I've had to make a program which shows the name
, surname
and the year
of birth of a child. Everything is working well, excepting the friend
function, which have to access the private
/protected
variable birth_year
and to show 0
if his birth_year
is bigger than 2007
, else to show 1
.
Here's the code:
#include <iostream>
#include <string.h>
using namespace std;
class child{
protected:
char name[20],surname[20];
int birth_year;
public:
child(char*,char*,int);
getyear();
show();
friend void itsolder();
};
child::child(char* n, char* p, int a)
{
strcpy(name,n);
strcpy(surname,p);
birth_year=a;
}
child::getyear()
{
return birth_year;
}
child::show()
{
cout << "Name: " << name << endl;
cout << "Surname: " << surname << endl;
cout << "Year of birth: " << birth_year << endl;
}
void itsolder(child&year)
{
if (year.birth_year>2007)
cout << "0" << endl;
else
cout << "1" << endl;
}
int main()
{
child c1("Richard", "John", 1997);
c1.show();
cout << c1.getyear() << endl;
itsolder(c1.birth_year)
return 0;
}
Here are the errors:
int child::birth_year
isprotected
;- "within this context" - is where I've put the condition in the
friend
function and where I called it, intomain()
; - invalid initialization of reference of type
child&
from expression of typeint