#include <Turboc.h>
class Time
{
private:
int hour,min,sec;
public:
Time() { }
Time(int h, int m, int s) { hour=h; min=m; sec=s; }
void OutTime() {
printf("%d:%d:%d\n",hour,min,sec);
}
int &operator [](int what) {
switch (what) {
case 0:
return hour;
case 1:
return min;
default:
case 2:
return sec;
}
}
const int &operator [](int what) const {
switch (what) {
case 0:
return hour;
case 1:
return min;
default:
case 2:
return sec;
}
}
};
void main()
{
Time A(1,1,1);
const Time B(7,7,7);
A[0]=12;
printf("현재 %d시입니다.\n",A[0]);
//B[0]=8;
printf("현재 %d시입니다.\n",B[0]);
}
여기에서 int &operator [](int what) 이게 먼지 잘모르겟구
Time(int h, int m, int s) { hour=h; min=m; sec=s; } 이부분도 잘모르겟어요
구조체 구조가 이렇개 될수잇나요?
연산자오버로딩이라고다른액션을지정해주는거구요컨스트럭터라고클래스를초기화할때사용하는함수입니다
저 클래스로 [ ]연산자 쓸때 기능을정의해주는거라보면댐 연산자오버로딩 - DCW