Home > Back-end >  C function problem
C function problem

Time:09-22

Pass the program, what circumstance, please advice
 
# include
using namespace std;
The class CDate;//class reference in advance statement


The class CTime
{
Public:
CTime (int h, m int, int) s: hour (h), minute (m), second (s) {}
Friend void CDate: : display (CTime t);//declare the CDate display function as friend yuan
Void the display ()
{
cout}
Private:
Int hour;
Int minute;
Int second;
};
The class CDate
{
Public:
CDate (int y, int m, int d) : the year (y), month (m), date (d) {}
Void the display (CTime t)
{
T.d isplay ();
cout}
Private:
Int year;
Int the month;
Int the date;
};

Int main ()
{
CTime t,11,11 (11);
CDate d (12,12,12);
D.d isplay (t);
system("pause");
return 0;
}

CodePudding user response:

I think you are understanding of a friend is not quite right,
Void display CTime (t)//use the parameter t, t.d isplay () call don't need a friend
The class is not used in CTime CDate: : the display, so a friend declaration is unnecessary

CodePudding user response:

reference 1st floor u010165006 response:
I think your understanding of a friend is not quite right,
Void display CTime (t)//use the parameter t, t.d isplay () call don't need a friend
The class is not used in CTime CDate: : display, so a friend declaration is redundant


I'm wrong, should be the CDate: : display and not have access to the class private or protected member CTime, so a friend declaration is superfluous,
-- -- -- -- -- --
Feel the case is not very suitable for use of a friend, if you want to use, I this also can change:
 # include 
using namespace std;
The class CDate;//class reference in advance statement


The class CTime
{
Public:
CTime (int h, m int, int) s: hour (h), minute (m), second (s) {}
//friend void CDate: : display (CTime & amp; T);//declare the CDate display function as friend yuan
Friend class CDate;
Void the display ()
{
cout}
Private:
Int hour;
Int minute;
Int second;
};
The class CDate
{
Public:
CDate (int y, int m, int d) : the year (y), month (m), date (d) {}
Void the display (CTime & amp; T)
{
//t.d isplay ();
coutcout}
Private:
Int year;
Int the month;
Int the date;
};

Int main ()
{
CTime t,11,11 (11);
CDate d (12,12,12);
D.d isplay (t);
system("pause");
return 0;
}



I use VC6.0 and VC2010 Express edition,
//friend void CDate: : display (CTime & amp; T);//can't
Friend class CDate;//can
I don't know why?

CodePudding user response:

Appropriate adjust the code sequence,
 # include 
using namespace std;
The class CTime.//class reference in advance statement

The class CDate
{
Public:
CDate (int y, int m, int d) : the year (y), month (m), date (d) {}
Void the display (CTime & amp; T);
Private:
Int year;
Int the month;
Int the date;
};

The class CTime
{
Public:
CTime (int h, m int, int) s: hour (h), minute (m), second (s) {}
Friend void CDate: : display (CTime & amp; T);//declare the CDate display function as friend yuan
Void the display ()
{
cout}
Private:
Int hour;
Int minute;
Int second;
};

Void CDate: : display (CTime & amp; T)
{
//t.d isplay ();
coutcout}

Int main ()
{
CTime t,11,11 (11);
CDate d (12,12,12);
D.d isplay (t);
system("pause");
return 0;
}

CodePudding user response:

Thank you
  • Related