第26講 クラスの学習その1☆☆
第4話 第3話問題解答
第3話問題解答例
#include<iostream>
#include<string>
#include<stdlib.h>
using namespace System;
using namespace std;
struct K{
char a[40],b[40],c[40];
char as[40],bs[40],cs[40];
char kok[40],sya[40],suu[40],rik[40],eig[40];
int ad[5],bd[5],cd[5];
};
void main(){
int i;
K A;
strcpy(A.a,"太郎");
strcpy(A.b,"次郎");
strcpy(A.c,"花子");
strcpy(A.as,"男");
strcpy(A.bs,"男");
strcpy(A.cs,"女");
strcpy(A.kok,"国語");
strcpy(A.sya,"社会");
strcpy(A.suu,"数学");
strcpy(A.rik,"理科");
strcpy(A.eig,"英語");
for(i=0;i<5;i++)A.ad[i]=rand()%100;
for(i=0;i<5;i++)A.bd[i]=rand()%100;
for(i=0;i<5;i++)A.cd[i]=rand()%100;
cout<<"クラスA:" <<endl;
cout<<"名前"<<" "<<"性"<<"
"<<A.kok<<" "<<A.sya<<" "<<A.suu<<"
"<<A.rik<<" "<<A.eig<<" "<<endl;
cout<<A.a<<" "<<A.as;
for(i=0;i<5;i++)if(A.ad[i]<10)cout<<" "<<A.ad[i]<<"
";else cout<<" "<<A.ad[i]<<" ";
cout<<endl;
cout<<A.b<<" "<<A.bs;
for(i=0;i<5;i++)if(A.bd[i]<10)cout<<" "<<A.bd[i]<<"
";else cout<<" "<<A.bd[i]<<" ";
cout<<endl;
cout<<A.c<<" "<<A.cs;
for(i=0;i<5;i++)if(A.cd[i]<10)cout<<" "<<A.cd[i]<<"
";else cout<<" "<<A.cd[i]<<" ";
cout<<endl;
K B;
strcpy(B.a,"梅子");
strcpy(B.b,"凛子");
strcpy(B.c,"一郎");
strcpy(B.as,"女");
strcpy(B.bs,"女");
strcpy(B.cs,"男");
for(i=0;i<5;i++)B.ad[i]=rand()%100;
for(i=0;i<5;i++)B.bd[i]=rand()%100;
for(i=0;i<5;i++)B.cd[i]=rand()%100;
cout<<"クラスB:" <<endl;
cout<<B.a<<" "<<B.as;
for(i=0;i<5;i++)if(B.ad[i]<10)cout<<" "<<B.ad[i]<<"
";else cout<<" "<<B.ad[i]<<" ";
cout<<endl;
cout<<B.b<<" "<<B.bs;
for(i=0;i<5;i++)if(B.bd[i]<10)cout<<" "<<B.bd[i]<<"
";else cout<<" "<<B.bd[i]<<" ";
cout<<endl;
cout<<B.c<<" "<<B.cs;
for(i=0;i<5;i++)if(B.cd[i]<10)cout<<" "<<B.cd[i]<<"
";else cout<<" "<<B.cd[i]<<" ";
cout<<endl;
K C;
strcpy(C.a,"英二");
strcpy(C.b,"尚子");
strcpy(C.c,"良太");
strcpy(C.as,"男");
strcpy(C.bs,"女");
strcpy(C.cs,"男");
for(i=0;i<5;i++)C.ad[i]=rand()%100;
for(i=0;i<5;i++)C.bd[i]=rand()%100;
for(i=0;i<5;i++)C.cd[i]=rand()%100;
cout<<"クラスC:" <<endl;
cout<<C.a<<" "<<C.as;
for(i=0;i<5;i++)if(C.ad[i]<10)cout<<" "<<C.ad[i]<<"
";else cout<<" "<<C.ad[i]<<" ";
cout<<endl;
cout<<C.b<<" "<<C.bs;
for(i=0;i<5;i++)if(C.bd[i]<10)cout<<" "<<C.bd[i]<<"
";else cout<<" "<<C.bd[i]<<" ";
cout<<endl;
cout<<C.c<<" "<<C.cs;
for(i=0;i<5;i++)if(C.cd[i]<10)cout<<" "<<C.cd[i]<<"
";else cout<<" "<<C.cd[i]<<" ";
cout<<endl;
K D;
strcpy(D.a,"淳子");
strcpy(D.b,"純太");
strcpy(D.c,"直美");
strcpy(D.as,"女");
strcpy(D.bs,"男");
strcpy(D.cs,"女");
for(i=0;i<5;i++)D.ad[i]=rand()%100;
for(i=0;i<5;i++)D.bd[i]=rand()%100;
for(i=0;i<5;i++)D.cd[i]=rand()%100;
cout<<"クラスD:" <<endl;
cout<<D.a<<" "<<D.as;
for(i=0;i<5;i++)if(D.ad[i]<10)cout<<" "<<D.ad[i]<<"
";else cout<<" "<<D.ad[i]<<" ";
cout<<endl;
cout<<D.b<<" "<<D.bs;
for(i=0;i<5;i++)if(D.bd[i]<10)cout<<" "<<D.bd[i]<<"
";else cout<<" "<<D.bd[i]<<" ";
cout<<endl;
cout<<D.c<<" "<<D.cs;
for(i=0;i<5;i++)if(D.cd[i]<10)cout<<" "<<D.cd[i]<<"
";else cout<<" "<<D.cd[i]<<" ";
cout<<endl;
}
さて、皆さん
struct K{
char a[40],b[40],c[40];
char as[40],bs[40],cs[40];
char kok[40],sya[40],suu[40],rik[40],eig[40];
int ad[5],bd[5],cd[5];
};
現時点では、クラスにはメンバ変数=フィールドしか入っていません。
これにメンバー関数=メソッド=操作を加えていきましょう。
第3話へ 第5話へ
VC++講義第1部へ
vb講義へ
VB講義基礎へ
初心者のための世界で一番わかりやすいVisual C++入門基礎講座
初心者のための世界で一番わかりやすいVisual
Basic入門基礎講座