C言語で書いているのですが、
行き詰ってしまいました。
#include <stdio.h>
#define OK 0
#define UNDERFLOW -1
#define OVERFLOW -2
#define N 3
//==========================================
class stack {
// メンバ変数の定義
int top;
int sdata[N];
public:
stack() {this->top = 0;}
void init() {
this->top = 0;
} // end of init
//-----------------------------------------
int push(int d) {
if (this->top >= N) goto ERR; // オーバフローのチェック
sdata[this->top] = d;// データの格納
this->top += 1;// データ格納位置の更新
//printf("%d %d\n", this->top, sdata[this->top]); // ??
printf("%d: pushed\n", d);
return OK;
ERR:
printf("Error: overflow\n");
return OVERFLOW;
} // end of push
//-----------------------------------------
int pop(int *d) {
if (this->top <=0 ) goto ERR; // アンダーフローのチェック
this->top -= 1; //データ格納位置の更新
*d = this->sdata[this->top]; //データの取り出し
printf("%d: popped\n", *d); // 動作確認用
return OK;
ERR:
printf("Error: underflow\n");
return UNDERFLOW;
} // end of pop
//-----------------------------------------
void show() {
int i;
printf("Stack: ");
for(i = 0; i< this->top; i++)
printf("%d ", this->sdata[i]); // データの表示
printf("\n");
} // end of show
};// end stack
//========================================
class stack2: public stack {
public:
// 関数topdataの定義
void topdata(int* d){
}
};
//========================================
int main() {
int cc;
int d;
stack2 st;
st.init();
cc = st.pop(&d);
cc = st.push(10);
cc = st.push(20);
cc = st.push(30);
st.show();
cc = st.push(40);
st.topdata(&d);
printf("top = %d\n", d);
cc = st.pop(&d);
st.show();
return 0;
} //end of main
Error: underflow
10: pushed
20: pushed
30: pushed
Stack: 10 20 30
Error: overflow
top = -858993460
30: popped
Stack: 10 20
続行するには何かキーを押してください . . .
top = 30にしたいのですが
クラスを継承するclass stack2の
topdeta関数の使い方がわかりません。
class stackのほうは大丈夫な感じではいるのですが
継承するにはどうすればよろしいんでしょうか。
よろしくお願い致します。