본문 바로가기

학습공간/C++, MFC

[3주차] 생성자와 소멸자, 멤버 함수의 활용, 클래스의 활용 (this)

반응형

C++ 6단원 예제 1

// this-> 를 최대한 많이 씀 (인자와 변수를 구분하기위해)
#include <iostream> 
using namespace std;
class Stack
{
public:
 int m_size;
 int m_top;
 int *m_buffer;
 
 void Initialize(int m_size = 10);
 void RemoveAll();
 bool Push(int value);
 bool Pop(int& value);
 
};
void Stack::Initialize(int m_size)
{
 this->m_size = m_size;
 this->m_top = -1;
 this->m_buffer = new int[this->m_size];
 memset(this->m_buffer, 0, sizeof(int) * this->m_size);
}
void Stack::RemoveAll()
{
 this->m_size = 0;
 this->m_top = -1;
 delete[] this->m_buffer;
 this->m_buffer = NULL;
}
bool Stack::Push(int value)
{
 if(this->m_top >= this->m_size - 1)
  return false;
 this->m_buffer[++this-> m_top] = value;
 return true;
}
bool Stack::Pop(int &value)
{
 if(this->m_top < 0)
  return false;
 value = this->m_buffer[this->m_top--];
 return true;
}
int main()
{
 Stack s1;
 s1.Initialize(5);
 int a, i;
 for(i=0;i<5;i++)
 {
  cin >> a;
  s1.Push(a);
 }
 for(i=0;i<5;i++)
 {
  s1.Pop(a);
  cout << a << " ";
 }
 cout <<endl;
 
    s1.RemoveAll();
   
    return 0;
}

C++ 6단원 예제 2
// 초기화 함수 주석처리, 생성자로 대체 
#include <iostream>
using namespace std;
class Stack
{
private:
 int m_size;
 int m_top;
 int *m_buffer;
public:
 // void Initialize(int m_size = 10);
 void RemoveAll();
 bool Push(int value);
 bool Pop(int& value);
 Stack(int size);
};
Stack::Stack(int size)
{
 m_size = size;
 m_top = -1;
 m_buffer = new int[m_size];
 memset(m_buffer, 0, sizeof(int) * m_size);
}
// void Stack::Initialize(int m_size)
// {
// this->m_size = m_size;
// this->m_top = -1;
// this->m_buffer = new int[this->m_size];
// memset(m_buffer, 0, sizeof(int) * this->m_size);
// }
void Stack::RemoveAll()
{
 m_size = 0;
 m_top = -1;
 delete[] m_buffer;
 m_buffer = NULL;
}
bool Stack::Push(int value)
{
 if(this->m_top >= this->m_size - 1)
  return false;
 this->m_buffer[++this->m_top] = value;
 return true;
}
bool Stack::Pop(int &value)
{
 if(this->m_top < 0)
  return false;
 value = this->m_buffer[this->m_top--];
 return true;
}
int main()
{
 Stack s1(5);
 int a;
 for(int i=0;i<5;i++)
 {
  cin >> a;
  s1.Push(a);
 }
 for(i=0;i<5;i++)
 {
  s1.Pop(a);
  cout << a << " ";
 }
 cout << endl;
 
 return 0;
}

C++ 6단원 실습과제 1
#include <iostream>
using namespace std;
#include <time.h>
time_t time(time_t *timer);
struct tm *localtime(const time_t *timer);
class Date
{
protected:
 int m_year;
 int m_month;
 int m_day;
 
public:
 void Print();
 bool SetYear(int year);
 bool SetMonth(int month);
 bool SetDay(int day);
 int GetLastDayOfMonth();
 Date(int year,int month,int day);
 Date();
 ~Date();
};
Date::Date()
{
 cout<<"Date 디폴트 생성자 호출"<<endl;
 typedef long time_t;
 time_t current;
 time(&current);
 tm *today=localtime(&current);
 
 m_year=today->tm_year+1900;
 m_month=today->tm_mon+1;
 m_day=today->tm_mday;
}
Date::Date(int year,int month,int day)
{
 cout<<"Date 인자 있는 생성자 호출"<<endl;
 
    if(SetYear(year) && SetMonth(month) && SetDay(day))
     cout<<"변경한 날짜는 ";
   
    else
    {
     cout<<"잘못된 날짜 입니다.!!"<<endl;
     cout<<"오늘 날짜로 초기화 됩니다.!!"<<endl;
    
     typedef long time_t;
     time_t current;
     time(&current);
     tm *today=localtime(&current);
     m_year=today->tm_year+1900;
     m_month=today->tm_mon+1;
     m_day=today->tm_mday;
    }
    Print();
}
void Date::Print()
{
 cout<<m_year<<"년 "
  <<m_month<<"월 "
  <<m_day<<"일 입니다."<<endl;
}
bool Date::SetYear(int year)
{
 if(year<0 || year>5000)//년도 변경 실패
  return false;
 m_year=year;
 return true;
}
bool Date::SetMonth(int month)
{
 if(month<1 || month>12)//월 변경 실패 
  return false;
 m_month=month;
 return true;
}
bool Date::SetDay(int day)
{
 if(day<1 || day>GetLastDayOfMonth())//일 변경 실패
  return false;
 m_day=day;
 return true;
}
int Date::GetLastDayOfMonth()
{
 int days[12]={31,28,31,30,31,30,31,31,30,31,30,31};
 int isLeapYear=0;
 
    if(m_year%4==0)
    {
     isLeapYear=1;
     if(m_year%100==0)
     {
      isLeapYear=0;
      if(m_year%400==0)
       isLeapYear=1;
     }
    }
    days[1] += isLeapYear;
    return days[m_month-1];
}
Date::~Date()
{
 cout<<"Date 소멸자 호출"<<endl;
}
void main()
{
 Date da;
 cout<<"오늘 날짜는 ";
 da.Print();
 
 int year,month,day;
 cout<<"연, 월, 일을 입력하세요 : ";
 cin>>year>>month>>day;
 Date(year,month,day);
}

C++ 6단원 복사생성자 복사소멸자

#include <iostream> 
using namespace std;
class Stack{
 int m_size;
 int m_top;
 int *m_buffer;
public:
 Stack(int size = 10);
 Stack(const Stack &s);
 void Initialize(int size);
 void RemoveAll();
 bool Push(int value);
 bool Pop(int &value);
 ~Stack();
};
Stack::Stack(int size){
 Initialize(size);
 cout << "생성자 호출\n";
 
}
Stack::Stack(const Stack &s){
 cout << "복사생성자 호출\n";
 m_size = s.m_size;
 m_top = s.m_top;
 m_buffer = new int[m_size];
 memcpy(m_buffer, s.m_buffer, sizeof(int) * (m_top+1));
}
void Stack::Initialize(int size){
 m_size = size;
 m_top = -1;
 m_buffer = new int [m_size];
 memset(m_buffer, 0, sizeof(int) * m_size);
}
void Stack::RemoveAll(){
 m_size = 0;
 m_top = -1;
 delete [] m_buffer;
 m_buffer = NULL;
}
bool Stack::Push(int value){
 if(m_top >= m_size -1 ) return false;
 m_buffer[++m_top] = value;
 return true;
}
bool Stack::Pop(int & value){
 if(m_top < 0) return false;
 value = m_buffer[m_top--];
 return true;
}
Stack::~Stack(){
 RemoveAll();
 cout << "소멸자 호출 \n";
}
void main(){
 Stack s1;
 s1.Push(123);
 s1.Push(456);
 s1.Push(789);
 int data;
 
 Stack *s2 = new Stack(s1);
 cout<< "스택 s1에서 꺼낸 데이터 : ";
 while(s1.Pop(data))
  cout<< data<<" ";
 cout<<"\n";
 
 cout << "스택 s2에서 꺼낸 데이터 : ";
 
 while(s2->Pop(data))
  cout<< data<<" ";
 cout<<"\n";
 
 delete s2;
}

 

C++ 6단원 실전 연습문제 3

#include <iostream>
using namespace std;
class Point
{
protected:
 const int m_id;
 int m_x,m_y;
public:
 void Print();
 bool IsEqual(const Point& p);
 void SetXY(int x,int y);
 Point(int id,int x=0,int y=0);
};
void Point::Print()
{
 cout<<m_id<<"번 점의 좌표 : ("
  <<m_x<<","<<m_y<<")"<<endl;
}
bool Point::IsEqual(const Point& p)
{
 return (m_x==p.m_x && m_y==p.m_y);
}
void Point::SetXY(int x,int y)
{
 m_x=x;
 m_y=y;
}
Point::Point(int id, int x,int y):m_id(id)
{
 m_x=x;
 m_y=y;
}
int main()
{
 Point p1(100);
 p1.Print();
 Point p2(101,10,20);
 p2.Print();
 Point p3(102,30);
 p3.Print();
 return 0;
}

 

C++ 6단원 실습과제 2

#include <iostream>
using namespace std;
#include <time.h>
time_t time(time_t *timer);
struct tm *localtime(const time_t *timer);
class Date
{
protected:
 int m_year;
 int m_month;
 int m_day;
public:
 void Print();
 int GetLastDayOfMonth();
 void DateAddDay(unsigned int n);
 Date(int year,int month,int day);
 Date();
 ~Date();
};
void Date::DateAddDay(unsigned int n)
{
 m_day+=n;
 while(m_day>GetLastDayOfMonth())
 {
  m_day-=GetLastDayOfMonth();
  m_month++;
  if(m_month>12)
  {
   m_month=1;
   m_year++;
  }
 }
}
Date::Date()
{
 cout<<"Date 디폴트 생성자 호출"<<endl;
 typedef long time_t;
 time_t current;
 time(&current);
 tm *today=localtime(&current);
 
 m_year=today->tm_year+1900;
 m_month=today->tm_mon+1;
 m_day=today->tm_mday;
}
void Date::Print()
{
 cout<<m_year<<"년 "
  <<m_month<<"월 "
  <<m_day;
}
int Date::GetLastDayOfMonth()
{
 int days[12]={31,28,31,30,31,30,31,31,30,31,30,31};
 int isLeapYear=0;
 
 if(m_year%4==0)
 {
  isLeapYear=1;
  if(m_year%100==0)
  {
   isLeapYear=0;
   if(m_year%400==0)
    isLeapYear=1;
  }
 }
 days[1] += isLeapYear;
 return days[m_month-1];
}
Date::~Date()
{
 cout<<"Date 소멸자 호출"<<endl;
}
void main()
{
 Date da;
 cout<<"오늘 날짜는 ";
 da.Print();
 cout<<"일 입니다."<<endl;
 int input;
 cout<<"몇 일 후 날짜를 구하려고 합니까?? : ";
 cin>>input;
 da.Print();
 cout<<"의 " <<input<<"일 후 날짜는 ";
 da.DateAddDay(input);
 da.Print();
 cout<<"일 입니다."<<endl;
}

반응형