엔지니어가 되고 싶은 공돌이
13. initialization section & class member variable 본문
- initialization section
#include <iostream> // 명시적 호출과 초기화 색션
#include <string>
using namespace std;
class DayOfYear
{
public:
DayOfYear(string m, int d);
DayOfYear(string m);
DayOfYear();
void input();
void output();
string getMonth();
int getDay();
void setMonthDay(string m, int d);
private:
string month;
int day;
};
int main()
{
DayOfYear date1, date2, date3;
// 명시적 호출
date1 = DayOfYear("May",5);
date1 = DayOfYear("Feb");
date1 = DayOfYear();
// 출력
date1.output();
date2.output();
date3.output();
return 0;
}
//초기화 섹션
DayOfYear::DayOfYear(string m, int d):month(m), day(d)
{
}
DayOfYear::DayOfYear(string m):month(m), day(1)
{
}
DayOfYear::DayOfYear():month("Jan"), day(1)
{
}
void DayOfYear::input()
{
cout << "Month & day: ";
cin >> month >> day;
}
void DayOfYear::output()
{
cout << "Month = " << month << endl;
cout << "Day = " << day << endl;
}
string DayOfYear::getMonth()
{
return month;
}
int DayOfYear::getDay()
{
return day;
}
void DayOfYear::setMonthDay(string m, int d)
{
month = m;
day = d;
}
- classs member variable
#include <iostream> // 클래스의 상속과 초기화 섹션
#include <string>
using namespace std;
class DayOfYear
{
public:
DayOfYear(string m, int d);
DayOfYear(string m);
DayOfYear();
void input();
void output();
string getMonth();
int getDay();
void setMonthDay(string m, int d);
private:
string month;
int day;
};
class Holiday
{
public:
Holiday(string m, int d, bool h);
Holiday();
void output();
private:
DayOfYear date;
bool tf;
};
int main()
{
Holiday children("May", 5, true), today("April", 11, false);
// 출력
children. output();
today.output();
return 0;
}
//Holiday 생성자
Holiday::Holiday(string m, int d, bool h):date(m,d), tf(h)
{
//month = m; 오류
//day = d;
//tf = h;
}
Holiday::Holiday(): date(), tf(false)
{
}
void Holiday::output()
{
date.output();
cout << "holiday =" << tf << endl;
}
//DayofYear 생성자
DayOfYear::DayOfYear(string m, int d):month(m), day(d)
{
}
DayOfYear::DayOfYear(string m):month(m), day(1)
{
}
DayOfYear::DayOfYear():month("Jan"), day(1)
{
}
void DayOfYear::input()
{
cout << "Month & day: ";
cin >> month >> day;
}
void DayOfYear::output()
{
cout << "Month = " << month << endl;
cout << "Day = " << day << endl;
}
string DayOfYear::getMonth()
{
return month;
}
int DayOfYear::getDay()
{
return day;
}
void DayOfYear::setMonthDay(string m, int d)
{
month = m;
day = d;
}
'C++ Language > Base C++' 카테고리의 다른 글
12. Constructor (0) | 2021.04.07 |
---|---|
11. Class Code (0) | 2021.04.06 |
10. Struct Code (0) | 2021.04.05 |
09. Array Code (0) | 2021.04.04 |
08. Class (0) | 2021.04.03 |
Comments