본문 바로가기

과제모음

[C++]상수(Const)

반응형

[[연습문제 1]]

>>Date.h<<

#ifndef _Date_H_
#define _Date_H_

class Date{
public:
 Date(int _Y, int _M, int _D);
 void OutDate()const;
 int GetYear()const;
 int GetMonth()const;
 int GetDay()const;
 
private:
 
 int Year;
 int Month;
 int Day;
};

#endif

>>main.cpp<<

#include <iostream>
#include "Date.h"
using std::cin; using std::cout; using std::endl;


int main()
{
 Date d1(2007, 3, 5);
 d1.OutDate();
 cout << d1.GetYear() << '/' << d1.GetMonth() << '/' << d1.GetDay() << endl; 
 
 return 0;
}

>>Date.cpp<<

#include <iostream>
#include "Date.h"
using std::cout;

Date::Date(int _Y, int _M, int _D)
{
 Year = _Y; Month = _M; Day = _D; 
}

void Date::OutDate()const
{
 cout << "입력된 날짜는" << Year << "년 " << Month << "월 " << Day << "일 입니다.\n";
}
int Date::GetYear()const
{
 return Year;
}
int Date::GetMonth()const
{
 return Month;
}
int Date::GetDay()const
{
 return Day;
}

[[연습문제 2]]

>>Telephone.h<<

#ifndef _Telephone_H_
#define _Telephone_H_

class Telephone{
public:
 Telephone(int b, char *_pN, char *pT);
 void OutTelephone()const;
 void SetName(char *modN);
 char* GetName();
private:
 const int addNum;
 char name[20];
 char pN[20];
};
#endif

>>main.cpp<<

#include <iostream>
#include "Telephone.h"
using std::cin; using std::cout; using std::endl;


int main()
{
 Telephone t1(1, "tiger", "111-222-3333");
 t1.OutTelephone();
 char modifyName[20];
 cout << "수정할 이름 입력 : ";
 cin.getline(modifyName, 20);

 t1.SetName(modifyName);
 cout << "수정된 이름은 " << t1.GetName() << "입니다\n";

 t1.OutTelephone();
 
 return 0;
}

>>Telephone.cpp<<

#include <iostream>
#include "Telephone.h"
using std::cin; using std::cout; using std::endl;


Telephone::Telephone(int b, char *_pN, char *pT):addNum(b)
{
 strcpy(name, _pN);
 strcpy(pN, pT); 
}

void Telephone::OutTelephone()const
{
 cout << "번지 : " << addNum << "\t" << name << " : " << pN << endl << endl; 
}

void Telephone::SetName(char *modN)
{
 strcpy(name, modN);
}


char* Telephone::GetName()
{
 return name;
}


반응형

'과제모음' 카테고리의 다른 글

[C++]복사생성자  (0) 2010.01.22
[C++]Const & this & Static 변수  (0) 2010.01.22
[C++]소멸자2(Destructor)  (0) 2010.01.22
[C++]소멸자(Destructor)  (0) 2010.01.22
[C++]생성자(Constructor)  (0) 2010.01.22