标签:test ++ class cpp void include mat vat std
class Time {
friend void printTime(Time &t);
public:
Time(int hour, int min, int sec);
private:
int m_hour;
int m_min;
int m_sec;
};
Time::Time(int hour, int min, int sec) {
m_hour=hour;
m_min=min;
m_sec=sec;
}
测试:
void printTime(Time &t){
cout <<t.m_hour<<":"<<t.m_min<<":"<<t.m_sec<<endl;
}
int main() {
Time t(4,5,6) ;
printTime(t);
return 0;
}
#include <iostream>
#include <typeinfo>
/**
* 友元函数
*/
using namespace std;
class Match;
class Time {
friend Match;//友元类的声明
public:
Time(int hour, int min, int sec);
private:
void printTime();
int m_hour;
int m_min;
int m_sec;
};
class Match {
public:
Match(int hour, int min, int sec);
void testTime();
private:
Time m_timer;
int m_hour;
int m_min;
int m_sec;
};
Time::Time(int hour, int min, int sec) {
m_hour = hour;
m_min = min;
m_sec = sec;
}
void Time::printTime() {
cout << m_hour << ":" << m_min << ":" << m_sec << endl;
}
Match::Match(int hour, int min, int sec) : m_timer(hour, min, sec) {
}
void Match::testTime() {
m_timer.printTime();
cout << m_hour << ":" << m_min << ":" << m_sec << endl;
}
int main() {
Match m(5, 43, 33);
m.testTime();
return 0;
}
标签:test ++ class cpp void include mat vat std
原文地址:https://www.cnblogs.com/wuyanzu/p/11874346.html