반응형

9.6> 다음 AbstractStack은 정수 스택 클래스로서 추상 클래스이다. 이를 상속받아 정수를 푸시, 팝하는 IntStack 클래스를 만들고 사용 사례를 보여라.

<코드>

#include <iostream>

using namespace std;

class AbstractStack{

public:

        virtual bool push(int n)=0; //스택에 n 푸시한다. 스택이 full이면 false 리턴

        virtual bool pop(int &n)=0; //스택에서 팝한 정수를 n 저장하고 스택이 empty이면 false 리턴

        virtual int size()=0; //현재 스택에 저장된 정수의 개수 리턴

};

class IntStack : public AbstractStack{

private:

        int *p;

        int cap; //전체 크기(배열 크기)

        int index; //인덱스 -1 empty

public:

        IntStack(int cap,int index);

        ~IntStack();

        bool push(int n);

        bool pop(int &n);

        int size();

        void show();

};

IntStack::IntStack(int cap,int index=-1) { //cap 만큼 동적 배열 생성

        this->cap=cap;

        this->index=index;

        p=new int[cap];}

 

IntStack::~IntStack(){delete []p;} //동적 배열 반환

bool IntStack::push(int n){ //배열 끝에 원소 추가, 꽉찼으면 false 리턴

        index++;

        if(index>=cap) {index--; return false;}

        else {p[index]=n; return true;}

}

bool IntStack::pop(int &n){ //마지막 원소 pop, 텅비었으면 false 리턴

        int tmp=p[index];

        index--;

        if(index<-1) {index++; return false;}

        else{n=tmp; return true;} //인자로 받은 참조에 pop 값을 넣는다.

}

void IntStack::show(){ //현재 존재하는 원소 순회 출력

        for(int i=0;i<index+1;i++){cout<<p[i]<<" ";}

        cout<<endl;

}

int IntStack::size() {return index+1;} //현재 존재하는 원소 개수

int main(void){

        int pp; //팝된 정수를 넣을 변수

        IntStack is(5); //cap 5짜리 인트 스택

        int getpush=0; //푸시할 정수를 받을 변수

        while(1){

       

        int menu=0; //메뉴 선택용 변수

        cout<<"1.푸시 2. 3.저장된 개수 (종료는 -1) >> ";

        cin>>menu;

        if(menu==1){ //1이면 푸시

               int ps;

               cout<<"푸시 정수를 입력하세요>> ";

               cin>>ps;

               if(is.push(ps)){cout<<"푸시 성공!"<<endl;} else{cout<<"푸시 실패!"<<endl;}

               is.show();

        }

        else if(menu==2){ //2

               if(is.pop(pp)){cout<<" 성공! 정수 : "<<pp<<endl;}else{cout<<" 실패!"<<endl;}

               is.show();

        }

        else if(menu==3){ //3이면 개수 출력

               cout<<"현재 길이 : "<<is.size()<<endl;

               is.show();    

        }

        else if(menu==-1){break;} //-1이면 루프

        else{cout<<"잘못된 입력입니다."<<endl;

        cin.clear();

        cin.ignore(100,'\n'); //문자 입력시 버퍼 비워주기 위함..

        continue;} //이상한 입력하면 다시 메뉴

        }

        return 0;

}

 

<결과창>


반응형

'컴퓨터 & 프로그래밍 & 전자공학 > C++' 카테고리의 다른 글

논리 게이트  (0) 2017.12.31
while, do while loop class  (0) 2017.12.31
ForLoopAdder 클래스  (0) 2017.12.31
KmToMile class  (0) 2017.12.31
WonToDollar Class  (0) 2017.12.31
반응형

5.5>추상 클래스의 서브 클래스 만들기에 필요한 추상 메소드 오버라이딩과 super()의 사용에 관한 문제이다. 다음과 같은 MyPoint 추상 클래스가 있다.




MyPoint를 상속받는 MyColorPoint 클래스를 작성하라. MyColorPoint의 생성자는 MyColorPoint(int x, int y, String color)로 하라. 그리고 다음과 같은 main() 메소드를 삽입하여 실행되도록 하라.





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
29
30
31
32
33
34
35
36
37
38
39
40
package HW1_JAVA;
abstract class MyPoint{ // 추상 클래스 MyPoint
    int x; int y;  //int형 변수 x,y 선언
    public MyPoint(int x,int y){  //생성자 MyPoint int형 변수인 인자 x,y
        this.x=x; this.y=y; //객체의 x에 인자 x 대입, 객체의 y에 인자 y 대입
    }
    protected abstract void move(int x,int y); //추상 메소드 move,int형 인자 x,y
    protected abstract void reverse(); //추상 메소드 reverse
    protected void show(){ // 객체의 x와 y를 출력하는 메소드 show
        System.out.println(x+","+y);
    }
}
class MyColorPoint extends MyPoint{  //MyPoint를 상속하는 MyColorPoint 클래스
    String s; //스트링형 오브젝트 s
    public MyColorPoint(int x, int y, String Color){ //생성자 인자는 int형 x,y와 string Color
        super(x,y); //슈퍼클래스의 생성자 호출
        this.s=Color; //String 레퍼런스 변수 Color가 가리키는 문자열을 this.s가 가리키게함
    }
    protected void move(int x,int y){ //상속받은 추상 메소드 오버라이딩
        this.x=x; this.y=y; //객체의 x에 인자 x 대입, 객체의 y에 인자 y 대입
    }
    protected void reverse(){ //상속받은 추상 메소드 오버라이딩
        int temp; // int형 변수 temp, 빈 물컵 역할
        temp=this.x;  //x와 y를 스왑하는 과정
        this.x=this.y;
        this.y=temp;
    }
    protected void show(){ //슈퍼클래스의 메소드 show 오버라이딩
        System.out.println(x+","+y+","+s);  //x와 y와 s를 출력하는 메소드 show
    }
}
public class Abstract {
    public static void main(String[] args) {//MyPoint형 객체를 가리키는 레퍼런스 변수 p
        MyPoint p=new MyColorPoint(2,3,"blue"); //MyColorPoint클래스의 객체 생성, 업캐스팅
        p.move(34); // 동적 바인딩에 의해 MyColorPoint에서 오버라이딩한 메소드move가 호출됨
        p.reverse(); //동적 바인딩에 의해 MyColorPoint에서 오버라이딩한 메소드reverse가 호출됨
        p.show(); //동적 바인딩에 의해 MyColorPoint에서 오버라이딩한 메소드 show가 호출됨
    }
}
 
cs


반응형

'컴퓨터 & 프로그래밍 & 전자공학 > JAVA' 카테고리의 다른 글

대문자 개수 세기  (0) 2017.06.19
메소드 오버라이딩  (0) 2017.06.18
상속 클래스  (0) 2017.06.18
ArrayUtility class  (0) 2017.06.18
직사각형 클래스  (0) 2017.06.18

+ Recent posts