オブジェクト指向言語 オブジェクト指向言語演習

38
オオオオオオオオオオ オオオオオオオオオオオオ オ 25 オ C++ オオ オオ オオオオオオ

Upload: adrina

Post on 18-Jan-2016

45 views

Category:

Documents


0 download

DESCRIPTION

オブジェクト指向言語 オブジェクト指向言語演習. 第 25 回  C++ 言語 参照、テンプレート. スケジュール (C++ 言語 ). オブジェクト指向技術 (2004/12/13) C++ 言語 コンソール入出力とクラス (2004/12/20) C++ 言語 クラスの継承 (2004/12/20) C++ 言語 クラスの詳細 (2005/01/17) C++ 言語 オーバーロード (2005/01/17) C++ 言語 クラスの継承 (2005/01/24) - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: オブジェクト指向言語 オブジェクト指向言語演習

オブジェクト指向言語オブジェクト指向言語演習

第 25 回  C++ 言語 参照、テンプレート

Page 2: オブジェクト指向言語 オブジェクト指向言語演習

13.Dec.2004 Object Oriented Language - Y.Nagasaka: [email protected] 2

スケジュール (C++ 言語 )20. オブジェクト指向技術 (2004/12/13)21. C++ 言語 コンソール入出力とクラス (2004/12/20)22. C++ 言語 クラスの継承 (2004/12/20)23. C++ 言語 クラスの詳細 (2005/01/17)24. C++ 言語 オーバーロード (2005/01/17)25. C++ 言語 クラスの継承 (2005/01/24)26. C++ 言語 マニピュレータ , ファイル入出力 (2005/01/24)27. C++ 言語 参照、テンプレート (2005/01/31)28. C++ 言語 試験 (2005/02/01)

最終試験は期末試験は 2 月 1 日 ( 火 ) 7,8 時限 に実施します。教室はいつもと同じ場所(新四号館 211 と 411 )です。

Page 3: オブジェクト指向言語 オブジェクト指向言語演習

C++ 言語 参照

C++ Language Reference

Page 4: オブジェクト指向言語 オブジェクト指向言語演習

13.Dec.2004 Object Oriented Language - Y.Nagasaka: [email protected] 4

参照 ( 教科書 P130~)

参照 (reference) C 言語の場合、関数に変数のアドレスを渡すためにはポイ

ンタを使用した。 C++ 言語ではポインタによるアドレス渡しに加えて、通常

の変数の形式 (* を使わない形式 ) で受け取る「参照」がある。

参照の使い方 関数呼び出し

void func(& int); //プロトタイプ宣言

func( x );

関数本体void func(int &x){ x = 10;}

呼び出し側では&が要らない

関数本体の仮引数で&を書くと自動的に判断して、「参照渡し」となる

内部では普通の変数と同じ扱いになる

Page 5: オブジェクト指向言語 オブジェクト指向言語演習

13.Dec.2004 Object Oriented Language - Y.Nagasaka: [email protected] 5

参照の例 ( 教科書 P131)

#include <iostream> using namespace std;

void f(int *n); //ポインタ仮引数を使用する

int main(void) { int i = 0;

f(&i);

cout << "iの新しい値 : " << i << '\n'; return 0;}

void f(int *n) { *n = 100; //nが指す引数に 100を格納する}

#include <iostream>using namespace std;

void f(int &n); //参照仮引数を宣言する

int main(void) { int i = 0;

f(i);

cout << "iの新しい値 : " << i << '\n'; return 0;}

// f()関数は参照仮引数を使用するvoid f(int &n) {// 次の文では *が必要ない n = 100; // 関数を呼び出すのに使用した引数に 100を格納}

引数に&n関数本体では変数の前に何もつかない

引数に *n関数本体では内容を示すためには *nを使用する。

参照の例 ポインタの例

Page 6: オブジェクト指向言語 オブジェクト指向言語演習

C++ 言語 テンプレート

C++ Language Template

Page 7: オブジェクト指向言語 オブジェクト指向言語演習

13.Dec.2004 Object Oriented Language - Y.Nagasaka: [email protected] 7

テンプレート関数 ( 教科書 P345~)

テンプレート関数 (Template Function) 汎用関数 (Generic Function) とも呼ばれ、複数の型に対

応する関数を作成する仕組み テンプレート関数は、呼び出された時、適切な型に適合す

る関数が生成される。 注)これまでの関数は、「引数の型」「戻り値の型」は予

め決められたもの1つ

int i_add(int a, int b){ return a+b;}/*整数変数 aと bを足して戻す関数 */

double d_add(double a, double b){ return a+b;}/*倍精度実数 aと bを足して戻す関数 */

int a,b,c; double x,y,z;

c = i_add(a, b); z = d_add(x, y);

同じ機能をする関数なのに、型に応じて複数定義しなければならない。

通常の関数の例

Page 8: オブジェクト指向言語 オブジェクト指向言語演習

13.Dec.2004 Object Oriented Language - Y.Nagasaka: [email protected] 8

テンプレート関数の書式 書式

例 前述の加算を行う関数をテンプレート関数で記述する

template <class タイプ > 関数の型 関数名 (引数 ) { // 関数本体}

#include <iostream>using namespace std;//テンプレート関数で定義した加算関数template <class T> T add (T a, T b){ return a + b;}int main(void) { int a = 10, b = 20; double x = 20.3, y = 35.3; cout << a << “+” << b << “=” << add(a,b) << endl; cout << x << “+” << y << “=” << add(x,y) << endl; return 0;}

Tを intに置換int add(int a, int b) { return a + b;}

Tを doubleに置換double add(double a,double b){ return a + b;}

Page 9: オブジェクト指向言語 オブジェクト指向言語演習

C++ 言語 課題

Page 10: オブジェクト指向言語 オブジェクト指向言語演習

13.Dec.2004 Object Oriented Language - Y.Nagasaka: [email protected] 10

第 21 回 課題 1

以下の UML クラス図で表される Circle クラスの宣言と定義を行いなさい。メイン関数は与えられたものを利用すること (oo21-1.cc) 。 r 半径 setR 半径をセットする getR 半径を取得する

Circle

-r: int

+setR(rr: int): void+getR(): int

int main() { int r;

Circle circle1; circle1.setR(7); r = circle1.getR(); cout << “circle1: r=“         << r << “\n”;}

Page 11: オブジェクト指向言語 オブジェクト指向言語演習

13.Dec.2004 Object Oriented Language - Y.Nagasaka: [email protected] 11

第 21 回 課題 1

#include <iostream>using namespace std;

class Circle {private: int r;public: void setR(int rr); int getR(void);};void Circle::setR(int rr){ r = rr; return;}int Circle::getR(void){ return r;}int main(void){省略}

Circle

-r: int

+setR(rr: int): void+getR(): int

メンバ変数 rはクラス外からアクセス禁止なので、関数を使って値をセットする。

メンバ変数 rはクラス外からアクセス禁止なので、関数を使って値を取得する。

Page 12: オブジェクト指向言語 オブジェクト指向言語演習

13.Dec.2004 Object Oriented Language - Y.Nagasaka: [email protected] 12

第 21 回 課題 2

課題 1 を拡張して、円の面積を返すメンバ関数を追加したプログラムを作成しなさい (oo21-2.cc) 。

Circle

-r: int

+setR(rr: int): void+getR(): int+getArea(): double

int main() { int r; double area;

Circle circle1; circle1.setR(7); r = circle1.getR(); cout << “circle1: r=“         << r << “\n”; cout << “The area is” <<circle1.getArea() << “\n”;}

Page 13: オブジェクト指向言語 オブジェクト指向言語演習

13.Dec.2004 Object Oriented Language - Y.Nagasaka: [email protected] 13

第 21 回 課題 2

#include <iostream>using namespace std;

class Circle {private: int r;public: void setR(int rr); int getR(void); double getArea(void);};void Circle::setR(int rr){ r = rr; return;}int Circle::getR(void){ return r;}double Circle::getArea(void){ return (double) (r*r) * 3.141592;}int main() {省略}

Circle

-r: int

+setR(rr: int): void+getR(): int+getArea(): double

getArea 関数で面積を計算して戻す。r は int なので r*r を double に型変換している。

Page 14: オブジェクト指向言語 オブジェクト指向言語演習

13.Dec.2004 Object Oriented Language - Y.Nagasaka: [email protected] 14

第 22 回 課題 資料 P7,8 の継承の例( Point2D/Point3D )を

oo22-1.cc として、完成させなさい ただし、メイン関数は次ページ

のものを利用すること また、実行結果は以下のようになる

$ ./oo22-1(x, y, z) = (10, 20, 30)

Point2D

- x : int - y : int

+ setX ( xx : int ) : void+ setY ( yy : int ) : void+ getX ( ) : int+ getY ( ) : int

Point3D

- z : int

+ setZ ( zz : int ) : void+ getZ ( ) : int

実行結果

Page 15: オブジェクト指向言語 オブジェクト指向言語演習

13.Dec.2004 Object Oriented Language - Y.Nagasaka: [email protected] 15

第 22 回 課題#include <iostream>using namespace std;class Point2D {省略};class Point3D : public Point2D {省略};void Point2D::setX(int xx){ x = xx;}void Point2D::setY(int yy){ y = yy;}int Point2D::getX(void){ return x;}int Point2D::getY(void){ return y;}void Point3D::setZ(int zz){ z = zz;}int Point3D::getZ(void){ return z;}

Point3Dは Point2Dを継承している。

Page 16: オブジェクト指向言語 オブジェクト指向言語演習

13.Dec.2004 Object Oriented Language - Y.Nagasaka: [email protected] 16

第 22 回 課題2 課題1で完成させたプログラム( oo22-1.cc )に変

更を加えて、メンバ関数をすべてインライン関数で指定するようにしたプログラム( oo22-2.cc )を作成しなさい。 実行結果が課題1と同じになることを確認すること

Page 17: オブジェクト指向言語 オブジェクト指向言語演習

13.Dec.2004 Object Oriented Language - Y.Nagasaka: [email protected] 17

第 22 回 課題2class Point2D { int x; int y;public: void setX(int xx) { x = xx; } void setY(int yy) { y = yy; } int getX(void) { return x; } int getY(void) { return y; }};class Point3D : public Point2D { int z;public: void setZ(int zz) { z = zz; } int getZ(void) { return z; }};

#include <iostream>using namespace std;class Point2D {省略};class Point3D : public Point2D {省略};void Point2D::setX(int xx){ x = xx;}void Point2D::setY(int yy){ y = yy;}int Point2D::getX(void){ return x;}int Point2D::getY(void){ return y;}void Point3D::setZ(int zz){ z = zz;}int Point3D::getZ(void){ return z;}

インライン関数にする

Page 18: オブジェクト指向言語 オブジェクト指向言語演習

13.Dec.2004 Object Oriented Language - Y.Nagasaka: [email protected] 18

第 22 回 課題3 課題1で完成させたプログラム( oo22-1.cc )のメ

イン関数部分に変更を加えて、ドット演算子を使わず、オブジェクトポインタとアロー演算子を利用して書き換えたプログラム( oo22-3.cc )を作成しなさい。 実行結果が課題1と同じになることを確認すること

Page 19: オブジェクト指向言語 オブジェクト指向言語演習

13.Dec.2004 Object Oriented Language - Y.Nagasaka: [email protected] 19

第 22 回 課題3int main(void) { Point3D p, *pp;

pp = &p;

pp->setX(10); pp->setY(20); pp->setZ(30);

cout << "(x, y, z) = ("; cout << pp->getX() << ", " << pp->getY() << ", “ << pp->getZ(); cout << ")\n";

return 0;}

int main(void) { Point3D p;

p.setX(10); p.setY(20); p.setZ(30);

cout << "(x, y, z) = ("; cout << p.getX() << ", " << p.getY() << ", “ << p.getZ(); cout << ")\n";

return 0;}

ポインタ変数 ppにオブジェクト pのアドレスを入れる

ポインタ変数 ppの宣言

Page 20: オブジェクト指向言語 オブジェクト指向言語演習

13.Dec.2004 Object Oriented Language - Y.Nagasaka: [email protected] 20

第 24 回 課題 1(1)

例を参考にして、次のクラス図で示されるような Ball クラスを作成しなさい。 Ball クラスのコンストラクタとしては1. 引数が無い場合

r=10, (x, y)=(10, 10)

2. 引数が一つの場合 r= 引数で与えられた値 , (x, y)=(20, 20)

3. 引数が二つの場合 (x, y)= 引数で与えられた値 , r=30

4. 引数が三つの場合 r, x, y の順番で与えられた引数の値を利用

となるようにコンストラクタをオーバーロードしなさい。 なお、 Main 部分は次のものを使うこと

Page 21: オブジェクト指向言語 オブジェクト指向言語演習

13.Dec.2004 Object Oriented Language - Y.Nagasaka: [email protected] 21

第 24 回 課題 1(2)

クラス図は右のとおり

実行結果は以下のとおり$ ./oo24-1b1: r=10 x=10 y=10b2: r=100 x=20 y=20b3: r=30 x=210 y=220b4: r=310 x=320 y=330

Ball

-x: int-y: int-r: int

+Ball( )+Ball(rr: int)+Ball(xx: int, yy: int)+Ball(rr: int, xx: int, yy: int)+~Ball( )+setR(rr: int): void+getR( ): int+getX( ): int+getY( ): int

Page 22: オブジェクト指向言語 オブジェクト指向言語演習

13.Dec.2004 Object Oriented Language - Y.Nagasaka: [email protected] 22

第 24 回 課題 1

#include <iostream>using namespace std;

class Ball{private: int r, x, y;public: Ball(void); Ball(int rr); Ball(int xx, int yy); Ball(int rr, int xx, int yy); ~Ball(void);//デストラクタ void setR(int rr); int getR(void); int getX(void); int getY(void);};

Ball::Ball(void){ r = 10; x = 10; y = 10;}Ball::Ball(int rr){ r = rr; x = 20; y = 20;}Ball::Ball(int xx, int yy){ x = xx; y = yy; r = 30;}Ball::Ball(int rr, int xx, int yy){ r = rr; x = xx; y = yy;}Ball::~Ball(void){//デストラクタ}void Ball::setR(int rr){ r = rr;}int Ball::getR(void){ return r;}int Ball::getX(void){ return x;}int Ball::getY(void){ return x;}

コンストラクタ

Ball b1; Ball b2(100); Ball b3(210, 220); Ball b4(310, 320, 330);

メイン関数のオブジェクト宣言部

Page 23: オブジェクト指向言語 オブジェクト指向言語演習

13.Dec.2004 Object Oriented Language - Y.Nagasaka: [email protected] 23

第 24 回 課題2 「コピーコンストラクタの例」のプログラムにおい

て、 Data クラスの扱うデータを int から double と変更しなさい。

なお、 Main 文についても例を参考にして、同じような内容になるように変更を加えなさい。

Page 24: オブジェクト指向言語 オブジェクト指向言語演習

13.Dec.2004 Object Oriented Language - Y.Nagasaka: [email protected] 24

第 24 回 課題2#include <iostream>using namespace std;class Data { private: double *p; int size; public: Data(int ssize); Data(const Data &obj); ~Data(); void setData(int i, double j); double getData(int i);};Data::Data(int ssize) { p = new double[ssize]; if(!p) exit(1); size = ssize;}Data::Data(const Data &obj) { int i; size = obj.size; p = new double[size]; if(!p) exit(1); for(i=0;i<size;i++)p[i]= obj.p[i];}Data::~Data() { delete [] p; }

void Data::setData(int i, double j){ if ( i>=0 && i<size ) p[i] = j;}double Data::getData(int i) { if ( i>=0 && i<size ) return p[i]; else return -1;}int main(void) { Data d(10); int i; for(i=0;i<10;i++) d.setData(i,(double)i); for ( i=0; i<10; i++ ) cout << showpoint << d.getData(i) << " "; cout << endl;

Data d1 = d;

for ( i=0; i<10; i++ ) cout << showpoint <<d1.getData(i) << " "; cout << endl; return 0;}

Page 25: オブジェクト指向言語 オブジェクト指向言語演習

13.Dec.2004 Object Oriented Language - Y.Nagasaka: [email protected] 25

第 25 回 課題 1(1)

以下のクラス図を表すプログラムを、例を参考にして作成しなさい。 Ball クラスは前回課題に x,y を設定する関数を付加。 なお、新しい関数の内容は

例のとおり。 また、 main 部分は次ペー

ジのものをそのまま利用しなさい。 +Ball( )

+Ball(rr: int)+Ball(xx: int, yy: int)+Ball(xx: int, yy: int, rr: int)+~Ball( )+setX(xx: int = 0): void+setY(yy: int = 0): void+setR(rr: int = 100): void+getX( ): int+getY( ): int+getR( ): int

Ball

-x: int-y: int-r: int

Ball3D

- z: int

+ Ball3D( )+ ~Ball3D( )+ setZ(zz: int = 0): void+ getZ( ): int+ show( ): void

Page 26: オブジェクト指向言語 オブジェクト指向言語演習

13.Dec.2004 Object Oriented Language - Y.Nagasaka: [email protected] 26

第 25 回 課題 1(2)

int main(int argc, char **argv) { int x, y, z, r;  cout << "x ? “ ; cin >> x; cout << "y ? “ ; cin >> y; cout << "z ? “    ; cin >> z; cout << "radius ? "; cin >> r; Ball3D bb; bb.show();

bb.setX(x); bb.setY(y); bb.setZ(z); bb.setR(r);

bb.show(); return (0);}

Ball3D クラスは Ball クラスを継承しているため、 Ball3D クラスでは定義していない関数が利用できる

実行結果x ? 12y ? 15z ? 12radius ? 50x = 10, y = 10, z = 10, radius = 10 x = 12, y = 15, z = 12, radius = 50

Page 27: オブジェクト指向言語 オブジェクト指向言語演習

13.Dec.2004 Object Oriented Language - Y.Nagasaka: [email protected] 27

第 25 回 課題 1

Ball3D は Ball クラスを継承 Ball クラスの x,y,r は private なので、 getX,getY,getR の public

の関数を使ってアクセスする必要がある。

class Ball{省略};

class Ball3D: public Ball {private: int z;public: Ball3D(void); ~Ball3D(void); void setZ(int zz = 0); int getZ(void); void show(void);};

Ball3D::Ball3D(void){ z = 10;}Ball3D::~Ball3D(void){}void Ball3D::setZ(int zz){ z = zz;}int Ball3D::getZ(void){ return z;}void Ball3D::show(void){ cout << "x = " << getX() << ", y = " << getY() << ", z = " << z << ", radius = " << getR()     << endl;}

Page 28: オブジェクト指向言語 オブジェクト指向言語演習

13.Dec.2004 Object Oriented Language - Y.Nagasaka: [email protected] 28

第 25 回 課題 2(1)

下記のクラス図で示すプログラムを作成せよ。 ただし、それぞれの内容は次のページに示すとおり。

Main 部分では以下のオブジェクトを作成し、それぞれのオブジェクの半径、位置、色を表示させなさい。 ColoredBall3D b1; ColoredBall3D b2(“black”); ColoredBall3D b3(3,5,7,10,“red”);

Color- color: string

+ Color( )+ Color(ccolor:string)+ ~Color( )+ setColor(ccolor:string)+ getColor( ): string

ColoredBall3D

+ ColoredBall3D( )+ ColoredBall3D(ccolor: string)+ ColoredBall3D(xx:int,yy:int,zz:int,rr:int, ,ccolor:string)+ ~ColoredBall3D( )

Ball

Ball3D

Page 29: オブジェクト指向言語 オブジェクト指向言語演習

13.Dec.2004 Object Oriented Language - Y.Nagasaka: [email protected] 29

第 25 回 課題 2(2)

Color Class メンバ変数

色 : color (string) 色を文字列として保存しておく変数

white, black, red, blue, green, yellow, … メンバ関数

Color( ) – コンストラクタ 初期値は color = “white”

Color( string ccolor ) – コンストラクタ 色の指定 color = ccolor とするコンストラクタ

~Color( ) – デストラクタ 何もしない

setColor( string ccolor ) 色の設定 color = ccolor を行う関数

getColor( ) オブジェクトの持つ色を返す関数

戻り値は string 型

Color- color: string

+ Color( )+ Color(ccolor: string)+ ~Color( )+ setColor(ccolor: string)+ getColor( ): string

Page 30: オブジェクト指向言語 オブジェクト指向言語演習

13.Dec.2004 Object Oriented Language - Y.Nagasaka: [email protected] 30

第 25 回 課題 2(3)

ColoredBall3D Class メンバ変数

なし メンバ関数

ColoredBall3D( ) – コンストラクタ ColoredBall3D( string ccolor ) – コンストラクタ ColoredBall3D( int xx, int yy, int zz,

int rr, string ccolor ) – コンストラクタ

~ColoredBall3D( ) – デストラクタ 何もしない ColoredBall3D

+ ColoredBall3D( )+ ColoredBall3D(ccolor: string)+ ColoredBall3D(xx:int, yy:int, zz:int, rr: int, ccolor: string)+ ~ColoredBall3D( )

Page 31: オブジェクト指向言語 オブジェクト指向言語演習

13.Dec.2004 Object Oriented Language - Y.Nagasaka: [email protected] 31

第 25 回 課題 2

class Color{private: string color;public: Color(void); Color(string ccolor); ~Color(void); void setColor(string ccolor); string getColor(void);};

class ColoredBall3D: public Ball3D, public Color {private:public: ColoredBall3D(void); ColoredBall3D(string ccolor); ColoredBall3D(int xx, int yy, int zz, int rr, string ccolor); ~ColoredBall3D(void);};

Color- color: string

+ Color( )+ Color(ccolor: string)+ ~Color( )+ setColor(ccolor: string)+ getColor( ): string

ColoredBall3D

+ ColoredBall3D( )+ ColoredBall3D(ccolor: string)+ ColoredBall3D(xx:int,yy:int, zz:int, rr: int, ccolor:string)+ ~ColoredBall3D( )

Ball Ball3D

Page 32: オブジェクト指向言語 オブジェクト指向言語演習

13.Dec.2004 Object Oriented Language - Y.Nagasaka: [email protected] 32

第 25 回 課題 2

class Color{private: string color;public: Color(void); Color(string ccolor); ~Color(void); void setColor(string ccolor); string getColor(void);};

class ColoredBall3D: public Ball3D, public Color {private:public: ColoredBall3D(void); ColoredBall3D(string ccolor); ColoredBall3D(int xx, int yy, int zz, int rr, string ccolor); ~ColoredBall3D(void);};

Color::Color(void){ color = "white";}Color::Color(string ccolor){ color = ccolor;}Color::~Color(void){ }void Color::setColor(string ccolor){ color = ccolor;}string Color::getColor(void){ return color;}ColoredBall3D::ColoredBall3D(void){}ColoredBall3D::ColoredBall3D(string ccolor){ setColor(ccolor);}ColoredBall3D::ColoredBall3D(int xx, int yy, int zz, int rr, string ccolor){ setX(xx); setY(yy); setZ(zz); setR(rr); setColor(ccolor);}ColoredBall3D::~ColoredBall3D(void){}

Page 33: オブジェクト指向言語 オブジェクト指向言語演習

13.Dec.2004 Object Oriented Language - Y.Nagasaka: [email protected] 33

第 25 回 課題 2

int main(void){

ColoredBall3D b1; ColoredBall3D b2("black"); ColoredBall3D b3(3,5,7,10,"red");

cout << "b1 : " << b1.getX() << " , " << b1.getY() << " , " << b1.getZ() << " , " << b1.getR() << " , " << b1.getColor() << endl; cout << "b2 : " << b2.getX() << " , " << b2.getY() << " , " << b2.getZ() << " , " << b2.getR() << " , " << b2.getColor() << endl; cout << "b3 : " << b3.getX() << " , " << b3.getY() << " , " << b3.getZ() << " , " << b3.getR() << " , " << b3.getColor() << endl; return 0;}

Page 34: オブジェクト指向言語 オブジェクト指向言語演習

13.Dec.2004 Object Oriented Language - Y.Nagasaka: [email protected] 34

第 26 回 課題1 以下の実行結果に示すような九九の表を作成しなさ

い。ただし、数字は実数表示とし、数字の幅は 7 とすること。

$ ./oo26-1 1.00 2.00 3.00 4.00 5.00 6.00 7.00 8.00 9.00 2.00 4.00 6.00 8.00 10.0 12.0 14.0 16.0 18.0 3.00 6.00 9.00 12.0 15.0 18.0 21.0 24.0 27.0 4.00 8.00 12.0 16.0 20.0 24.0 28.0 32.0 36.0 5.00 10.0 15.0 20.0 25.0 30.0 35.0 40.0 45.0 6.00 12.0 18.0 24.0 30.0 36.0 42.0 48.0 54.0 7.00 14.0 21.0 28.0 35.0 42.0 49.0 56.0 63.0 8.00 16.0 24.0 32.0 40.0 48.0 56.0 64.0 72.0 9.00 18.0 27.0 36.0 45.0 54.0 63.0 72.0 81.0

Page 35: オブジェクト指向言語 オブジェクト指向言語演習

13.Dec.2004 Object Oriented Language - Y.Nagasaka: [email protected] 35

第 26 回 課題1#include <iostream>using namespace std;int main(void){ int i,j; for(i = 1; i <= 9; i++){ for(j = 1; j <= 9; j++){ cout.width(7);cout.precision(3); cout.setf(ios::showpoint); cout << (double) (i*j); } cout << endl; }}

#include <iostream>#include <iomanip>using namespace std;int main(void){ int i,j; for(i = 1; i <= 9; i++){ for(j = 1; j <= 9; j++){ cout << setw(7) << setprecision(3) << showpoint  << (double) (i*j); } cout << endl; }}

Page 36: オブジェクト指向言語 オブジェクト指向言語演習

13.Dec.2004 Object Oriented Language - Y.Nagasaka: [email protected] 36

第 26 回 課題2 入力したコピー元ファイルを開き、そのファイルの

中身をすべて大文字に変換して、コピー先ファイルに書き込むプログラムを作成しなさい。 ただし、コピー元ファイル名、コピー先ファイル名はコマ

ンド行から与えるようにすること。

$ ./oo26-2 oo26-2.cc oo26-2.cc.copy$$ cat oo26-2.cc.copy#INCLUDE <IOSTREAM>#INCLUDE <FSTREAM>USING NAMESPACE STD;

INT MAIN(INT ARGC, CHAR **ARGV) { CHAR CH;

::

作成されたファイルの中身が正しいかを確認するために、cat コマンドを用いてそのファイルの中身を表示させる

Page 37: オブジェクト指向言語 オブジェクト指向言語演習

13.Dec.2004 Object Oriented Language - Y.Nagasaka: [email protected] 37

第 26 回 課題2#include <iostream>#include <fstream>using namespace std;int main(int argc, char *argv[]){ // *argv[]と **argvは同じ意味 char ch;

ifstream fin(argv[1]); ofstream fout(argv[2]); if (!fout) { cout << “Output file open error." << endl; exit(1); } if (!fin) { cout << “Input file open error." << endl; exit(1); }

fin.unsetf(ios::skipws); while ( !fin.eof() ) { fin >> ch; if (ch >= 'a' && ch <= 'z') ch = ch -'a' + 'A'; if ( !fin.eof() ) fout << ch; } fin.close(); fout.close(); return 0;}

Page 38: オブジェクト指向言語 オブジェクト指向言語演習

13.Dec.2004 Object Oriented Language - Y.Nagasaka: [email protected] 38

コンパイルと実行 まずはじめに

作業ディレクトリの作成 $ mkdir ~/wrk/

$ mkdir ~/wrk/oo/

次に 作業ディレクトリへの移動 $ cd ~/wrk/oo プログラミング $ vi oo20-1.cc コンパイル・リンク $ g++ -o oo20-1 oo20-1.cc 実行 $ ./oo20-1

* C++言語プログラムのファイル名は、 .cc でなければならない

一度実行すればよい

* プログラムが終了しなくなったら Ctrl Ctrl キーを押しながら キーを押しながら cc

を押して強制終了すること注意