블로그 이미지
권남

카테고리

분류 전체보기 (227)
Temp (1)
CA (0)
Game (0)
nGrinder (0)
Java (17)
JavaScript (1)
JBOSS (0)
EJBCA (0)
Tomcat (1)
DB (6)
Cache (6)
LINUX (4)
Frameworks (11)
C++ (32)
C (96)
Mosquitto (3)
STB (0)
JNI (10)
Android (5)
WebSocket (1)
PDF (0)
AVR (13)
LINUX Device Driver (8)
Arduino (4)
ekgu (0)
resume, portfolio (1)
Dynamics AX (X++) (4)
comp (0)
Total
Today
Yesterday

달력

« » 2025.2
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28

공지사항

최근에 올라온 글

stack2.cpp

C++ / 2015. 4. 28. 18:05

<<< 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
Posted by 권남
, |

최근에 달린 댓글

글 보관함