0
시간에 addminutes에 친구 기능을 쓰고 그에 따라 시간을 변경하려고합니다. 예 : 시간 t3 (11,58,0) addMinutes (t3,10); t3.print()는 12:08:00에 작성해야합니다. 은 컴파일 할 때 '시간'의 개인 구성원 인 코드입니다. '시간'의 개인 구성원입니다. 어떤 기능을 정의했는지 알려주시겠습니까? 여기서 해.친구 기능 개인 데이터 멤버에 액세스 할 수 없습니다
/*Header File for Time */
Time.h
#ifndef RATIONALNO_TIME_H
#define RATIONALNO_TIME_H
#include<iostream>
using namespace std;
class Time{
friend void addMinutes(Time&,const int&);
public:
Time(const int&,const int&,const int&);
void print()const;
private:
int hour;
int minute;
int second;
};
#endif //RATIONALNO_TIME_H
/*Source File*/
Time.cc
#include "Time.h"
Time::Time(const int& h,const int& m,const int& s)
{
if(h>=0 && h<=23) {
hour = h;
}else
hour=0;
if(m>=0 && m<=59) {
minute = m;
}else
minute=0;
if(s>=0 && s<=59) {
second = s;
}else second=0;
}
void Time::print() const {
cout<<hour;
cout<<minute;
cout<<second<<endl;
cout<<((hour==0||hour==12)?12:hour%12)<<":"<<minute<<":"<<second<<(hour<12?"AM":"PM")<<endl;
}
void addMinutes(Time& time1,int & m)
{
int hr;
if(time1.minute+m>59)
{
hr=time1.hour+1;
}
}
int main()
{
Time t1(17,34,25);
t1.print();
Time t3(11,58,0);
t3.print();
addMinutes(t3,10);
t3.print();
}
친구 선언 및 정의의 프로토 타입을주의 깊게 읽습니다. – molbdnilo
const 참조에 의해'int'와 같은 프리미티브를 전달하지 마십시오. 그것은 무의미한 징벌입니다. – molbdnilo