question

i have this question i dont know how to access to display function and make it friend in member class.and i dont know if my const id number is correct or not ?.
Display() function , which is a friend function that prints the member's information.
Note that:
oEach member has a unique membership number which is generated at the first time when a member joins the club and doesn’t not change after that.
oMembership period is the number of years since a member joined the club until 2015.

thank you so much and this is my try and i didnot finish yet.


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
70
71
72
73
#include<iostream>
#include<string>
using namespace std;
class Date {
private:
  int day;
  int month;
  int year;
public: 
 Date(int d, int m, int y)
{
setDay(d);
setMonth(m);
setYear(y);
}
 int getDay() {
     return day;
 }
 int getMonth() { 
     return month;
 }
 int getYear() { 
     return year;
 }
 void setDay(int d){ 
     day = d;
 }
 void setMonth(int m){
     if(m < 1||m >12)
  {
    month =1;
    cout << "Month set as 1" << endl;
  }
  else
   month = m;
 }
 void setYear(int y){ 
     year = y;
 }
  
// display date function
 void displayDate()
{
  cout << getMonth() << "/" << getDay() << "/" << getYear() << endl;
}
 
};
class Member{
string Members_name;
const int Membership_number;
Date Membership_date;
int Membership_period;
public:
	Member(string n,Date d,int p):Members_name(n),Membership_number(1),Membership_date(d),Membership_period(p){}
string getmembersname(){return Members_name; }
void setmembersname(string n){Members_name=n;}
int getmembershipnumber(){return Membership_number;}
Date getmembershipdate(){return Membership_date;}
void setmembershipdate(Date d){Membership_date=d;}
int getmembershipperiod(){return Membership_period; }
void setmembershipperiod(int p){Membership_period=p;}





int main(){



system("pause");
return 0;
}
Your code only has public member functions.

Also, it doesn't compile because you never closed your Member class.
Last edited on
I don't see why you would want to create a friend display function to print the public attributes of the Member class. I would just make a public: display function for the Member class directly. But if you're trying to make a friend function then you'll need to create its prototype like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class Member
{
friend void display(Member&); // Reference a Member object

private: // or protected:
string Members_name;
const int Membership_number;
Date Membership_date;
int Membership_period;

public:
Member(string n,Date d,int p):Members_name(n),Membership_number(1),Membership_date(d),Membership_period(p){}
string getmembersname(){return Members_name; }
void setmembersname(string n){Members_name=n;}
int getmembershipnumber(){return Membership_number;}
Date getmembershipdate(){return Membership_date;}
void setmembershipdate(Date d){Membership_date=d;}
int getmembershipperiod(){return Membership_period; }
void setmembershipperiod(int p){Membership_period=p;}

} // you forgot to put a closing curly brace 


Then you can define your prototype in a .cpp file and #include your class.h file.
thank you so much for your help ,but what i mean is diffrenet ,i put commants on my code which i need to print but i dont know how to print this fnction once , ihop you can help me with it
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include<iostream>
#include<string>
using namespace std;
class Date {
private:
  int day;
  int month;
  int year;
public: 
 Date(int d, int m, int y)
{
setDay(d);
setMonth(m);
setYear(y);
}
 int getDay() {
     return day;
 }
 int getMonth() { 
     return month;
 }
 int getYear() { 
     return year;
 }
 void setDay(int d){ 
     day = d;
 }
 void setMonth(int m){
     if(m < 1||m >12)
  {
    month =1;
    cout << "Month set as 1" << endl;
  }
  else
   month = m;
 }
 void setYear(int y){ 
     year = y;
 }
  
// display date function
 void displayDate()
{
  cout << getMonth() << "/" << getDay() << "/" << getYear() << endl;
}
 
};
class Member{
friend void display(Member&);
private:
string Members_name;
const int Membership_number;
Date Membership_date;
int Membership_period;
public:
	Member(string n,Date d,int p):Members_name(n),Membership_number(1),Membership_date(d),Membership_period(p){}
string getmembersname(){return Members_name; }
void setmembersname(string n){Members_name=n;}
int getmembershipnumber(){return Membership_number;}
Date getmembershipdate(){return Membership_date;}
void setmembershipdate(Date d){Membership_date=d;}
int getmembershipperiod(){return Membership_period; }
void setmembershipperiod(int p){Membership_period=p;}

friend void display(Member& b){
  cout << b.getMonth() << "/" << b.getDay() << "/" << b.getYear() << endl;
  //cout<<"the name is :"<<getmembername();
  //cout<<"the member is :"<<getmembershipnumber();
  //cout<<"the member ship period:"<<getmembershipperiod();

}
};

int main(){
Member obj("savanh",4,7,2015,56);
obj.display();//which is print all the information in Member class and the date in display fun in Date class together.


system("pause");
return 0;
}





Don't make it a friend function; just make it a normal member function. There is no reason to use the friend keyword in this case.
Topic archived. No new replies allowed.