パソコン活用研究シリコンバレー(C、C++、の活用研究)

クラス

=========================================================================================
C++の真髄とか言われているクラスというものを眺めてみることにします。
==========================================================================================

1 まずは構造体

(1) 構造体の復習
よく「構造体もクラスだ。」という話を聞きます。何かよくわかんない話ですが、構造体というのはクラスの一番シンプルなものらしいのです。
というわけで、まずは構造体というやつの復習からしてみましょうか。

構造体の題材としては、C言語の構造体で作成した星の構造体を使うことにします。

構造体の定義は以下の書式で行うのでしたね。
struct 構造体タグ {
  メンバーの宣言;
  メンバーの宣言;
 } 変数名;


構造体starの中のメンバーは、それぞれ
x  「星」のX座標
y  「星」のY座標
c 「星」の色
をあらわすことにします。

構造体starの定義は以下の通りです。
struct star {
int x;
int y;
int c;
} st;

では、まず、星を1個だけ表示するプログラム。コンパイルにはBorland C++5.5はを使用しました。

#include <stdlib.h>
#include <dos.h>
#include <conio.h>


main(){
struct star { /*starの構造体定義*/
int x;
int y;
int c;
} st;


clrscr(); /*画面クリア*/
_setcursortype(0);
st.x=20; st.y=10;
st.c=2;
gotoxy(st.x,st.y);textcolor(st.c);
putch('*');
_setcursortype(2);
}

以下はBorland C++に用意されている便利な関数です。
clrscr()  画面をクリアする
_setcursortype(int i)
gotoxy(int x, int y) カーソル位置を(x,y)にセットする
textcolor(int c) 表示色をセットする。

実行すると、座標(20,10)に緑色の星を1個表示します。(画像1)

 (画像1)


(2) (おまけ)もうちょっとプログラムらしく

さて、星を1個表示して終わりでは、悲しいので、、C言語の構造体でやったように、色とりどりの星が降ってきては消え、降ってきては消え、というプログラムにしてみます。(画像2)

 画像2

コンパイラはBoralnd C++ 5.5を使います。C言語の時はLSC-86でコンパイルしましたので、移植のためにコードは多少変更しました。

#include <stdlib.h>
#include <dos.h>
#include <time.h>
#include <conio.h>
#define num 30


main(){
struct star { /*starの構造体定義*/
int x;
int y;
int c;
} st[num];

int i,dx,dc;
long nowt,t;

clrscr(); /*画面クリア*/
_setcursortype(0);
t= time(&nowt);
srand(nowt%3600); /*乱数初期化*/

/*変数の初期化*/
for (i=1;i<num; i++){
st[i].x=0; st[i].y=0; st[i].c=0;}

while(!kbhit()){
for( i=1 ;i<num;i++){
if (st[i].y==0 && rand()>30000){
dx= 32767/78; dc=32767/7;
st[i].x=rand()/dx+1; st[i].y=1; st[i].c=rand()/dc;
}
if (st[i].y == 0) continue;
gotoxy(st[i].x,st[i].y);
putch(' ');
st[i].y=st[i].y+1;
if (st[i].y>19){st[i].y=0; continue;}
textcolor(st[i].c); gotoxy(st[i].x,st[i].y);
putch('*');
}
sleep(1);}
_setcursortype(2);
}



2 クラス

さて、では構造体の定義の部分をクラスに変更してみます。
ほとんど何も変更していませんが、これでコンパイルOK.問題なく実行可です。

変更点といえば、struct → class にした程度です。
こうしてみると、なるほど構造体はクラスの特殊な形(一番シンプルな形)なのね、ということが実感できます。

#include <stdlib.h>
#include <dos.h>
#include <conio.h>

class star /*クラスの定義*/
{
public:
int x;
int y;
int c;
};


main(){
star st;

clrscr(); /*画面クリア*/
_setcursortype(0);
st.x=20; st.y=10;
st.c=2;
gotoxy(st.x,st.y);textcolor(st.c);
putch('*');
_setcursortype(2);
}






ちなみに、色とりどりの星が降ってきては消え、降ってきては消え、というプログラムは以下のようになります。


#include <stdlib.h>
#include <dos.h>
#include <time.h>
#include <conio.h>
#define num 30

class star
{
public:
int x;
int y;
int c;
};


main(){
star st[num];
int i,dx,dc;
long nowt,t;

clrscr(); /*画面クリア*/
_setcursortype(0);
t= time(&nowt);
srand(nowt%3600); /*乱数初期化*/

/*変数の初期化*/
for (i=1;i<num; i++){
st[i].x=0; st[i].y=0; st[i].c=0;}

while(!kbhit()){
for( i=1 ;i<num;i++){
if (st[i].y==0 && rand()>30000){
dx= 32767/78; dc=32767/7;
st[i].x=rand()/dx+1; st[i].y=1; st[i].c=rand()/dc;
}
if (st[i].y == 0) continue;
gotoxy(st[i].x,st[i].y);
putch(' ');
st[i].y=st[i].y+1;
if (st[i].y>19){st[i].y=0; continue;}
textcolor(st[i].c); gotoxy(st[i].x,st[i].y);
putch('*');
}
sleep(1);}
_setcursortype(2);
}



TopPage