Up

「第7章 クラス型」Win32コンソールアプリケーション版

 

 <岡本安晴「大学生のための心理学VC++プログラミング入門」>の第7章のサンプルプログラムのWin32版をVisual C++2008で作成した。プログラムファイルは圧縮ファイルとしてまとめた。圧縮ファイルはクリックしてダウンロードしたものをマウスの右ボタンでクリックして表示されるメニュから「解凍」あるいは「展開」を選んで解凍することができる。解凍したプログラムファイルはVisual C++2010でも開くことができる。


リスト7.1-1Win32

圧縮ファイルsample7_1_1win32.zip

=====================================================

#include "stdafx.h"

#include <iostream>

 

using namespace std;

 

class base {

                double mean, sd;

 

        public:

                void setMeanSD( double m, double s ){

                        mean = m;

                        sd = s;

                        return;

                }

                double getStdScore( double x ){

                        return (x - mean) / sd;

                }

};

 

int _tmain(int argc, _TCHAR* argv[])

{

        base Scale;

        double vMean, vSD;

        cout << "mean = ";

        cin >> vMean;

        cout << "sd = ";

        cin >> vSD;

        cout << "Score = ";

        double score;

        cin >> score;

        Scale.setMeanSD( vMean, vSD );

        double stdScore = Scale.getStdScore( score);

        cout << "StdScore = " << stdScore << endl;

 

        cout << endl << "何か文字を入力して終了。" << endl;

        char ck;

        cin >> ck;

        return 0;

}

 


リスト7.1-2Win32

圧縮ファイルsample7_1_2win32.zip

=====================================================

#include "stdafx.h"

#include <iostream>

 

using namespace std;

 

class base {

                double mean, sd;

 

        public:

                void setMeanSD( double m, double s ){

                        mean = m;

                        sd = s;

                        return;

                }

                double getStdScore( double x ){

                        return (x - mean) / sd;

                }

};

 

int _tmain(int argc, _TCHAR* argv[])

{

        base * pScale;

        pScale = new base;

        double vMean, vSD;

        cout << "mean = ";

        cin >> vMean;

        cout << "sd = ";

        cin >> vSD;

        cout << "Score = ";

        double score;

        cin >> score;

        pScale->setMeanSD( vMean, vSD );

        double stdScore = (*pScale).getStdScore( score);

        cout << "StdScore = " << stdScore << endl;

 

        cout << endl << "何か文字を入力して終了。" << endl;

        char ck;

        cin >> ck;

        return 0;

}

 


リスト7.2-1Win32

圧縮ファイルsample7_2_1win32.zip

=============================================

#include "stdafx.h"

#include <iostream>

 

using namespace std;

 

class base {

                double mean, sd;

 

        public:

                void setMeanSD( double m, double s ){

                        mean = m;

                        sd = s;

                        return;

                }

                double getStdScore( double x ){

                        return (x - mean) / sd;

                }

};

 

class child: public base {

        public:

                double getDevScore( double x ){

                        double z = getStdScore( x );

                        return 10.0 * z + 50.0;

                }

};

 

int _tmain(int argc, _TCHAR* argv[])

{

        child Scale;

        double vMean, vSD;

        cout << "mean = ";

        cin >> vMean;

        cout << "sd = ";

        cin >> vSD;

        cout << "Score = ";

        double score;

        cin >> score;

        Scale.setMeanSD( vMean, vSD );

        double stdScore = Scale.getStdScore( score );

        double devScore = Scale.getDevScore( score );

        cout << "StdScore = " << stdScore << endl;

        cout << "DevScore = " << devScore << endl;

 

        cout << endl << "何か文字を入力して終了。" << endl;

        char ck;

        cin >> ck;

        return 0;

}

 


リスト7.3-1Win32

圧縮ファイルsample7_3_1win32.zip

=============================================

#include "stdafx.h"

#include <iostream>

 

using namespace std;

 

class p_class {

        public:

                virtual void vdisp_message(){

                        cout << "vdisp_message/p_class です。" << endl;

                        return;

                }

                void disp_message(){

                        cout << "disp_message/p_class です。" << endl;

                        return;

                }

};

 

class c_class: public p_class {

        public:

                c_class(){}

                virtual void vdisp_message(){

                        cout << "vdisp_message/c_class です。" << endl;

                        return;

                }

                void disp_message(){

                        cout << "disp_message/c_class です。" << endl;

                        return;

                }

};

 

void CheckClass( p_class * x ){

        x->vdisp_message();

        x->disp_message();

        return;

}

 

void CheckClassRef( p_class & c ){

        c.vdisp_message();

        c.disp_message();

        return;

}

 

int _tmain(int argc, _TCHAR* argv[])

{

        p_class * p;

        p = new p_class;

        p->vdisp_message();

        p->disp_message();

        CheckClass( p );

        cout << endl;

 

        p = new c_class;

        p->vdisp_message();

        p->disp_message();

        CheckClass( p );

        cout << endl;

       

        c_class * cc;

        cc = new c_class;

        cc->vdisp_message();

        cc->disp_message();

        CheckClass( cc );

        cout << endl;

 

        CheckClassRef( *cc );

 

        cout << endl << "何か文字を入力して終了。" << endl;

        char ck;

        cin >> ck;

        return 0;

}

 


リスト7.4-1Win32

圧縮ファイルsample7_4_1win32.zip

=============================================

#include "stdafx.h"

#include <iostream>

 

using namespace std;

 

class sample1 {

        public:

                int a, b;

};

 

class sample11 {

        public:

                int a, b;

                sample11(){

                        a = 1;

                        b = 2;

                }

};

 

class sample2 {

        public:

                int a, b;

               

                sample2(): a(1), b(2){ }

                sample2( int x ): a(x) { }

                sample2( int x, int y ){ a = x; b = y; }

};

 

int _tmain(int argc, _TCHAR* argv[])

{

//      sample1 obj1;

//              このまま次行を実行するとWin32の場合はエラーになる。

//      cout << "obj1.a = " << obj1.a << "   obj1.b = " << obj1.b << endl;

 

        sample11 obj11; // = sample11();

        cout << "obj11.a = " << obj11.a << "   obj11.b = " << obj11.b << endl;

 

        sample2 obj2;

        cout << "obj2.a = " << obj2.a << "   obj2.b = " << obj2.b << endl;

 

        sample2 obj22( 3 );

        cout << "obj22.a = " << obj22.a << "   obj22.b = " << obj22.b << endl;

 

        sample2 obj23( 4, 5 );

        cout << "obj23.a = " << obj23.a << "   obj23.b = " << obj23.b << endl;

        cout << endl;

 

        sample2 obj3 = obj23;

        cout << "obj3.a = " << obj3.a << "   obj3.b = " << obj3.b << endl;

        sample2 obj32 = sample2( 10, 20 );

        cout << "obj32.a = " << obj32.a << "   obj32.b = " << obj32.b << endl;

        cout << endl;

 

        sample1 * p1 = new sample1;

        cout << "p1->a = " << p1->a << "   p1->b = " << p1->b << endl;

        sample2 * p2 = new sample2( 11, 22 );

        cout << "p2->a = " << p2->a << "   p2->b = " << p2->b << endl;

        *p2 = obj32;

        cout << "p2->a = " << p2->a << "   p2->b = " << p2->b << endl;

        p2 = &obj23;

        cout << "p2->a = " << p2->a << "   p2->b = " << p2->b << endl;

        cout << endl;

 

        cout << endl << "何か文字を入力して終了。" << endl;

        char ck;

        cin >> ck;

        return 0;

}

 


リスト7.4-2Win32

圧縮ファイルsample7_4_2win32.zip

========================================

#include "stdafx.h"

#include <iostream>

 

using namespace std;

 

class sample {

        public:

                sample(){

                        cout << "Hello (sample)." << endl;

                }

                virtual ~sample(){

                                cout << "Good-by (sample)." << endl;

                }

};

 

class sample_c: public sample {

        public:

                char c;

                sample_c(): c('a'){

                        cout << "Hello (sample_c)/"<< c << endl;

                }

                sample_c( char vc ): c(vc){

                        cout << "Hello (sample_c)/"<< c << endl;

                }

                virtual ~sample_c(){

                        cout << "Good-by (sample_c)/" << c << endl;

                }

};

 

int _tmain(int argc, _TCHAR* argv[])

{

        sample * p;

        p = new sample();

        delete p;

        cout << endl;

        p = new sample_c();

        delete p;

        cout << endl;

        {

                sample_c obj_c;

                sample_c * obj_cc;

                obj_cc = new sample_c('b');

        //      delete obj_cc;

        }

 

        cout << endl << "何か文字を入力して終了。" << endl;

        char ck;

        cin >> ck;

        return 0;

}

 


リスト7.5-1Win32

圧縮ファイルsample7_5_1win32.zip

=============================================

#include "stdafx.h"

#include <iostream>

 

using namespace std;

 

void check( int i){

        if (i == 0 ){

                throw "exception...i == 0 !";

        }else{

                cout << "OK...i = " << i << endl;

        }

}

 

int _tmain(int argc, _TCHAR* argv[])

{

        cout << "i = ";

        int i;

        cin >> i;

        check( i );

 

        cout << endl << "何か文字を入力して終了。" << endl;

        char ck;

        cin >> ck;

        return 0;

}

 


リスト7.5-2Win32

圧縮ファイルsample7_5_2win32.zip

============================================

#include "stdafx.h"

#include <iostream>

 

using namespace std;

 

void check( int i ){

        if (i == 0){

                throw "exception...i == 0 !";

        }else{

                cout << "OK...i = " << i << endl;

        }

}

 

int _tmain(int argc, _TCHAR* argv[])

{

        cout << "i = ";

        int i;

        cin >> i;

        try{

                check( i );

        }

        catch( char * s ){

                cout << "例外の生成--> " << s << endl;

        }

 

        cout << endl << "何か文字を入力して終了。" << endl;

        char ck;

        cin >> ck;

        return 0;

}

 


リスト7.5-3Win32

Linux上でコンパイルするときは、runtime_errorを使用するためにstdexceptヘッダーファイルを

#include <stdexcept>

とインクルードする必要がある。

圧縮ファイルsample7_5_3win32.zip

==============================================

#include "stdafx.h"

#include <iostream>

 

using namespace std;

 

void check( int i ){

        if (i == 0){

                throw "exception...i = 0 !";

        }else if(i < 0){

                throw runtime_error("runtime_error...i < 0");

        }else{

                cout << "OK...i = " << i << endl;

        }

}

 

int _tmain(int argc, _TCHAR* argv[])

{

        cout << "i = ";

        int i;

        cin >> i;

        try{

                check( i );

        }

        catch( char * s ){

                cout << "例外の生成--> " << s << endl;

        }

        catch( exception & e){

                cout << "exception->" << e.what() << endl;

        }

 

        cout << endl << "何か文字を入力して終了。" << endl;

        char ck;

        cin >> ck;

        return 0;

}

 


 

第6章用のWin32版サンプルプログラム

第8章用のWin32版サンプルプログラム

 

Up