#include < iostream > #includeusing namespace std; int main() { vector v; int i; for(i=0; i<10; i++) v.push_back(i); for(i=0; i<10; i++) cout << v[i] << " "; cout << endl; vector ::iterator p = v.begin(); while(p != v.end()){ cout << *p << " "; p++; } return 0; }
Category: uncategorized
thisポインタ
メンバ関数から同じクラスのほかのメンバを参照する際には、直接参照することができる。
#include < iostream > #include < cstring > using namespace std; class inventory { char item[20]; double cost; int on_hand; public: inventory(char *i, double c, int o) { strcpy(item, i); cost = c; on_hand = o; } void show(); }; void inventory::show() { cout << item; cout << ": $" << cost; cout << " stock: " << on_hand << "\n"; } int main() { inventory ob("rench", 4.95, 4); ob.show(); return 0; }
オブジェクトの代入
型が同じであれば、1つのオブジェクトをもう1つのオブジェクトに代入することができます。
#includeusing namespace std; class myclass { int a, b; public: void set(int i, int j){ a = i; b = j; } void show() { cout << a << ' ' << b << '\n'; } }; int main() { myclass o1, o2; o1.set(10, 4); o2 = o1; o1.show(); o2.show(); return 0; }