Define friend function

I am going to be very honest here and admit that this is one of twenty homework questions I have in my C++ programming class. I have been able to complete all of the other questions and even most of this one but I have been stuck now for over eight hours on trying to define the friend function below. Help, or at least give me a very good hint because the reading is not helping and tutoring online does not real seem to be available. They are just a lot less present than you would think tutors would be. Thanks to any that would help.

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
#include <iostream>
#include <cstdlib>
using namespace std;

class dateType 
{
friend void dateFriend (dateType dateFriendObject);
public: 
void setDate(int month, int day, int year); 
int getDay() const; 
int getMonth() const;
int getYear() const; 
void printDate() const; 
dateType(int month = 1, int day = 1, int year = 1900); 
private: 
int dMonth; 
int dDay; 
int dYear; 
};


void dateFriend (dateType dateFriendObject)
{
    // A function that takes as parameters two objects of type dateType and returns true 
    // if the date represented by the first object comes before the date 
    // represented by the second object; otherwise, the function returns false. 
}
void dateType::setDate(int month, int day, int year) 
{ 
dMonth = month; dDay = day; dYear = year;
 }
int dateType::getDay() const
{ 
return dDay; 
 } 
int dateType::getMonth() const
{ 
return dMonth;
 }
int dateType::getYear() const
{ 
return dYear;
 } 
void dateType::printDate() const
{ 
cout << dMonth << "-" << dDay << "-" << dYear;
 } 
dateType::dateType(int month, int day, int year) 
{ 
dMonth = month; 
dDay = day; 
dYear = year; 
 }
 
int main ()
{


return 0;
}
Here is an example:

1
2
3
4
5
6
7
8
9
10
11
12
class Foo {
public :
	Foo() : x() {}

private :
	int x;
	friend void set_x_to_one(Foo&);
};

void set_x_to_one(Foo& foo) {
	foo.x = { 1 };
}


Notice that the function 'set_x_to_one' is not a member function of 'Foo', it is a friend function. A friend function is one that has access to a class' private members (in this case 'x') and doesn't belong to said class.
This is done by declaring the function inside the class with the friend keyword (line 7). It does not matter under which access specification (public, private, protected) you place the declaration - in my example I've elected to place it under the 'private' keyword, but I could have made it public - it makes no difference.

The actual function definition goes outside of the class (line 10). Notice how we didn't use the scope resolution operator (::) since the function doesn't belong to any class.

Finally, line 11 of my example is why we made the function a friend in the first place. I would not be able to access object foo's 'x' member variable like this because it is private, unless I had a getter or something similar. If you comment-out line 7 of my example, you will see that your compiler will give you an error (or your IDE will highlight it for you) on line 11 because suddenly 'set_x_to_one' is just a global function, and does not have access to 'x'.
Last edited on
First you need to make your friend function public. If it is private you will not be able to call it outside of the class.
You also need to set the parameters of your function to take in two objects. Currently you only ask for one, which won't allow you to compare it to anything.
Using the friend function, you can directly access members of the class inside it.
1
2
3
4
5
6
7
8
9
10
11
class dateType
{
public:
	friend void dateFriend(dateType, dateType);
}

void dateFriend(dateType obj1, dateType obj2)
{
//Make comparison such as obj1.dYear < obj2.dYear etc
}
So this is what I ended up with, it compiles, but it still doesn't look right.
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
66
67
68
69
#include <iostream>
#include <cstdlib>
using namespace std;

class dateType 
{


friend void dateFriend (dateType dateFriendObject1, dateType dateFriendObject2);
public: 
void setDate(int month, int day, int year); 
int getDay() const; 
int getMonth() const;
int getYear() const; 
void printDate() const; 
dateType(int month = 1, int day = 1, int year = 1900); 
private: 
int dMonth; 
int dDay; 
int dYear; 
};


void dateFriend (dateType dateFriendObject1, dateType dateFriendObject2)
{
dateFriendObject1.dYear;
dateFriendObject2.dYear;
if(dateFriendObject1.dYear <= dateFriendObject2.dYear)
{cout <<"True" << endl;}
else
{cout << "False" << endl;}
     
     // A function that takes as parameters two objects of type dateType and returns true 
    // if the date represented by the first object comes before the date 
    // represented by the second object; otherwise, the function returns false. 
}
void dateType::setDate(int month, int day, int year) 
{ 
dMonth = month; dDay = day; dYear = year;
 }
int dateType::getDay() const
{ 
return dDay; 
 } 
int dateType::getMonth() const
{ 
return dMonth;
 }
int dateType::getYear() const
{ 
return dYear;
 } 
void dateType::printDate() const
{ 
cout << dMonth << "-" << dDay << "-" << dYear;
 } 
dateType::dateType(int month, int day, int year) 
{ 
dMonth = month; 
dDay = day; 
dYear = year; 
 }
 
int main ()
{

system("pause");
return 0;
}
First you need to make your friend function public.

No a friend function has no access specifier.

See this link for more information:
http://en.cppreference.com/w/cpp/language/friend
And from that link:
Access specifiers have no effect on the meaning of friend declarations (they can appear in private: or in public: sections, with no difference)


I do question the need for this friend function. You have access functions available so this function could be a normal non-class function that uses the access functions instead of trying to directly access the variables directly.

Like most things that don't make sense on this site, it is being done this way because the book is forcing us to do it that way. Thanks for the help guys!
Topic archived. No new replies allowed.