| 情報学部 | 菅沼ホーム | 全体目次 | 演習解答例 | 付録 | 索引 |
class Vector {
double x[2];
} Vector a,b,c; c = a + b;
".",".*","::","?*"
operator+
= : 代入 () : 関数呼び出し [] : 添え字 -> : 間接参照
c = a + b;
c = a.(operator+)(b);
/**********************************/
/* ベクトルの加算(メンバー関数) */
/* coded by Y.Suganuma */
/**********************************/
#include <stdio.h>
/**********************/
/* クラスvectorの定義 */
/**********************/
class Vector {
double x[2];
public:
Vector operator+ (Vector &b); // 演算子+のオーバーロード
void input();
void output();
};
/**********************/
/* +のオーバーロード */
/**********************/
Vector Vector::operator+ (Vector &b)
{
Vector c;
c.x[0] = x[0] + b.x[0];
c.x[1] = x[1] + b.x[1];
return c;
}
/**********************/
/* ベクトル要素の入力 */
/**********************/
void Vector::input()
{
printf(" 2つの値を入力してください ");
scanf("%lf %lf", &(x[0]), &(x[1]));
}
/**********************/
/* ベクトル要素の出力 */
/**********************/
void Vector::output()
{
printf("%f %f\n", x[0], x[1]);
}
/************/
/* main関数 */
/************/
int main()
{
Vector a, b, c;
printf("a\n");
a.input();
printf("b\n");
b.input();
c = a + b;
c.output();
return 0;
}
a 2つの値を入力してください 1 2 b 2つの値を入力してください 3 5 4.000000 7.000000
/**********************************/
/* ベクトルの加算(フレンド関数) */
/* coded by Y.Suganuma */
/**********************************/
#include <stdio.h>
/**********************/
/* クラスvectorの定義 */
/**********************/
class Vector {
double x[2];
public:
void input();
void output();
friend Vector operator+ (Vector &a, Vector &b);
};
/**********************/
/* +のオーバーロード */
/**********************/
Vector operator+ (Vector &a, Vector &b)
{
Vector c;
c.x[0] = a.x[0] + b.x[0];
c.x[1] = a.x[1] + b.x[1];
return c;
}
/**********************/
/* ベクトル要素の入力 */
/**********************/
void Vector::input()
{
printf(" 2つの値を入力してください ");
scanf("%lf %lf", &(x[0]), &(x[1]));
}
/**********************/
/* ベクトル要素の出力 */
/**********************/
void Vector::output()
{
printf("%f %f\n", x[0], x[1]);
}
/************/
/* main関数 */
/************/
int main()
{
Vector a, b, c;
printf("a\n");
a.input();
printf("b\n");
b.input();
c = a + b;
c.output();
return 0;
}
z = x + 5;
z = 5 + x;
/****************************/
/* 複素数(メンバー関数) */
/* coded by Y.Suganuma */
/****************************/
#include <iostream>
using namespace std;
class Complex {
double r;
double i;
public:
Complex (double a = 0.0, double b = 0.0); // constructor
Complex operator +(Complex a); // overload of +
friend ostream& operator << (ostream&, Complex); // overload of <<
};
Complex::Complex(double a, double b)
{
r = a;
i = b;
}
Complex Complex::operator +(Complex a)
{
Complex c;
c.r = a.r + r;
c.i = a.i + i;
return c;
}
ostream& operator << (ostream& stream, Complex a)
{
stream << "(" << a.r << ", " << a.i << ")\n";
return stream;
}
int main()
{
Complex w, x(1.0, 2.0), y(3.0), z;
z = x + y;
w = x + 5.0; // 5.0 + x はエラー
cout << "z " << z ;
cout << "w " << w ;
return 0;
}
z (4, 2) w (6, 2)
/****************************/
/* 複素数(フレンド関数) */
/* coded by Y.Suganuma */
/****************************/
#include <iostream>
using namespace std;
class Complex {
double r;
double i;
public:
Complex (double a = 0.0, double b = 0.0); // constructor
friend Complex operator +(Complex a, Complex b); // overload of +
friend ostream& operator << (ostream&, Complex); // overload of <<
};
Complex::Complex(double a, double b)
{
r = a;
i = b;
}
Complex operator +(Complex a, Complex b)
{
Complex c;
c.r = a.r + b.r;
c.i = a.i + b.i;
return c;
}
ostream& operator << (ostream& stream, Complex a)
{
stream << "(" << a.r << ", " << a.i << ")\n";
return stream;
}
int main()
{
Complex w, x(1.0, 2.0), y(3.0), z;
z = x + y;
w = 5.0 + x;
cout << "z " << z;
cout << "w " << w;
return 0;
}
z (4, 2) w (6, 2)
#include <iostream>
#include <complex>
using namespace std;
int main()
{
complex<double> x(1.0, 2.0), y(3.0);
complex<double> z = x + y;
complex<double> w = 5.0 + x;
cout << "z " << z << endl;
cout << "w " << w << endl;
return 0;
}
/****************************/
/* 変換演算子 */
/* coded by Y.Suganuma */
/****************************/
#include <stdio.h>
/**********************/
/* クラスVectorの定義 */
/**********************/
class Vector {
double x[2];
public:
operator int() // 変換演算子の定義
{
return (x[0] != 0.0 || x[1] != 0.0);
}
friend void input(Vector &);
friend void output(Vector &);
};
/**********************/
/* ベクトル要素の入力 */
/**********************/
void input(Vector &v)
{
scanf("%lf %lf", &(v.x[0]), &(v.x[1]));
}
/**********************/
/* ベクトル要素の出力 */
/**********************/
void output(Vector &v)
{
printf("%f %f\n", v.x[0], v.x[1]);
}
/************/
/* main関数 */
/************/
int main()
{
Vector a;
int sw = 0;
while (sw == 0) {
printf("要素を入力してください ");
input(a);
sw = int(a); // オブジェクトaの型をintに変換
}
output(a);
return 0;
}
要素を入力してください 0 0 要素を入力してください 1 0 1.000000 0.000000
#include <stdio.h>
// () のオーバーロード
class Plus
{
public :
int operator() (int a, int b)
{
return a + b;
}
};
// main
int main()
{
Plus p;
int c = p(10, 20);
printf("c = %d\n", c);
return 0;
}
c = 30
#include <stdio.h>
// 構造体 list
struct list
{
char *name;
int nen;
};
// クラス Vector
class Vector
{
list *val;
int max;
public:
// コンストラクタ
Vector(int n)
{
val = new list [n];
max = n;
}
// デストラクタ
~Vector()
{
delete [] val;
}
friend class IteratorNext;
};
// クラス IteratorNext
class IteratorNext
{
const Vector *vp;
int i;
public:
// コンストラクタ
IteratorNext (const Vector &v)
{
vp = &v;
i = 0;
}
// 次の要素
list *operator() ()
{
return (i < vp->max) ? &(vp->val[i++]) : 0;
}
};
// main
int main()
{
Vector v(2);
list *p;
IteratorNext next1(v);
p = next1();
p->name = "山田太郎";
p->nen = 30;
p = next1();
p->name = "鈴木花子";
p->nen = 25;
IteratorNext next2(v);
while ((p = next2()) != 0)
printf("%s %d\n", p->name, p->nen);
return 0;
}
山田太郎 30 鈴木花子 25
a = b ・ c → a = operator・(b, c)
a = b ・ c → a = b.operator・(c)
a = ・b → a = operator・(b)
a = ・b → a = b.operator・()
a = b[c] → a = b.operator[](c)
a = b(c,d,・・・) → a = b.operator()(c,d,・・・)
| 情報学部 | 菅沼ホーム | 全体目次 | 演習解答例 | 付録 | 索引 |