stack2.cpp
<<< stack.h >>>
#ifndef STACK_H
#define STACK_H
#include <iostream>
const int STACK_SIZE = 100;
// C는 관련된 데이터와 연산들을 구조체형 포인터로 묶는다
// oop는 맴버변수로묶는다
// C : typedef struct (name) / cpp : struct (name)
class Stack{
// 맴버변수
private :
int *pArr;
size_t size;
int tos;
// 맴버함수
public :
// Stack();
Stack(size_t size = STACK_SIZE); // default 인자
Stack(const Stack &rhs); // 복사 생성자
~Stack();
Stack& operator=(const Stack &rhs);
bool operator==(const Stack &rhs);
void push(int data);
int pop();
};
#endif
<<< stack.cpp >>>
#include <cassert>
#include "stack.h"
// 맴버함수는 객체를 통해서호출이 된다.
// 자기 자신을 호출한 객체를 알아야 한다. 그래서
// 맴버함수는 묵시적으로 this라는 포인터를 가진다. this = 자기자신을 호출한 객체를 가르키는 포인터
/*
Stack::Stack()
{
this->pArr = new int[100];
assert(this->pArr );
this->size = size;
this->tos = 0;
}
*/
Stack::Stack(size_t size)
{
this->pArr = new int[size];
assert(this->pArr /* != NULL */);
this->size = size;
this->tos = 0;
}
Stack::Stack(const Stack &rhs)
{
// operator=( 과 같은내용이지만
// 복사생성자는 self X, return X, delete X
this->pArr = new int[rhs.size];
for (int i = 0; i < rhs.tos; ++i)
this->pArr[i] = rhs.pArr[i];
this->size = rhs.size;
this->tos = rhs.tos;
}
Stack::~Stack()
{
delete [] this->pArr;
}
Stack& Stack::operator=(const Stack &rhs)
{
// self-assignment test, 포인터가 힙공간을 쓸때 프리를 하며 원본이 날라가는 문제
if (this != &rhs)
{
delete[] this->pArr;
this->pArr = new int[rhs.size];
for (int i = 0; i < rhs.tos; ++i)
this->pArr[i] = rhs.pArr[i];
this->size = rhs.size;
this->tos = rhs.tos;
}
return *this; // 자기 자신 객체 넘기고 끝
}
bool Stack::operator==(const Stack &rhs)
{
if (this->size != rhs.size || this->tos != rhs.tos)
return false;
for (int i = 0; i < this->tos; ++i)
if (this->pArr[i] != rhs.pArr[i])
return false;
return true;
}
void Stack::push(int data)
{
assert(this->tos != this->size);
this->pArr[this->tos++] = data;
}
int Stack::pop()
{
assert(this->tos );
return this->pArr[--this->tos];
}
<<< testStack.cpp >>>
#include <iostream>
#include "stack.h"
int main(void)
{
Stack s1(10), s2(100);
Stack s3(s2); // 복사 생성자
//s1.initStack(10);
//s2.initStack(100);
s1.push(100);
s1.push(200);
s1.push(300);
s2.push(900);
s2.push(800);
s2.push(700);
Stack s4; // s4(100) 안주려면 기본생성자 함수 추가한다.
s4 = s2;
if (s2 == s4)
std::cout << "s2 and s4 are equal." << std::endl;
else
std::cout << "s2 and s4 are not equal." << std::endl;
//std::cout << s4 << std::endl;
std::cout << "s1 1st pop() : " << s1.pop() << std::endl;
std::cout << "s1 2nd pop() : " << s1.pop() << std::endl;
std::cout << "s1 3rd pop() : " << s1.pop() << std::endl;
std::cout << "s2 1st pop() : " << s2.pop() << std::endl;
std::cout << "s2 2nd pop() : " << s2.pop() << std::endl;
std::cout << "s2 3rd pop() : " << s2.pop() << std::endl;
//s1.~Stack();
//s2.~Stack();
return 0;
}
/*
for1003@ubuntu:~/oop/stack2$ g++ -c stack.cpp
for1003@ubuntu:~/oop/stack2$ g++ -c testStack.cpp
^[[Afor1003@ubuntu:~/oop/stack2$ g++ -o testStack testStack.cpp stack.cpp
^[[Afor1003@ubuntu:~/oop/stack2$ ./testStack
s2 and s4 are equal.
s1 1st pop() : 300
s1 2nd pop() : 200
s1 3rd pop() : 100
s2 1st pop() : 700
s2 2nd pop() : 800
s2 3rd pop() : 900
'C++' 카테고리의 다른 글
complex2 (0) | 2015.04.30 |
---|---|
Queue.cpp (0) | 2015.04.29 |
testString.cpp (0) | 2015.04.29 |
complex.cpp 종합 (0) | 2015.04.28 |
complex (0) | 2015.04.27 |
stack.c 복습 (0) | 2015.04.27 |
Queue (0) | 2015.04.27 |
C Queue3 복습 (0) | 2015.04.27 |