#include < iostream >
using namespace std;
class coord {
int x, y;
public:
coord() { x=0; y=0; }
coord(int i, int j){ x=i; y=j; }
void get_xy(int &i, int &j){ i=x; j=y; }
coord operator+(coord ob2);
coord operator+(int i);
};
coord coord::operator+(coord ob2)
{
coord temp;
temp.x = x + ob2.x;
temp.y = y + ob2.y;
return temp;
}
coord coord::operator+(int i)
{
coord temp;
temp.x = x + i;
temp.y = y + i;
return temp;
}
int main()
{
coord o1(10, 10), o2(5, 3), o3;
int x, y;
o3 = o1 + o2;
o3.get_xy(x, y);
cout << "(o1+o2) x: " << x << ", Y: " << y << "\n";
o3 = o1 + 100;
o3.get_xy(x, y);
cout << "(o1+100) x: " << x << ", Y: " << y << "\n";
return 0;
}
コンストラクタ関数にデフォルト引数
#include < iostream >
using namespace std;
class myclass {
int x;
public:
myclass(int n = 0){ x = n; }
int getx() { return x; }
};
int main()
{
myclass o1(10);
myclass o2;
cout << "o1: " << o1.getx() << '\n';
cout << "o2: " << o2.getx() << '\n';
return 0;
}
参照 swap
#include < iostream >
using namespace std;
void swapargs(int &x, int &y);
int main()
{
int i, j;
i = 10;
j = 19;
cout << "i: " << i << ", ";
cout << "j: " << j << "\n";
swapargs(i, j);
cout << "after exchange: ";
cout << "i: " << i << ", ";
cout << "j: " << j << "\n";
return 0;
}
void swapargs(int &x, int &y)
{
int t;
t = x;
x = y;
y = t;
}
整数の配列の割り当て
#include < iostream >
using namespace std;
int main()
{
int *p;
p = new int [5];
if(!p) {
cout << "memory set error\n";
return 1;
}
int i;
for(i=0; i<5; i++) p[i] = i;
for(i=0; i<5; i++){
cout << "integer type p[" << i << "]is: ";
cout << p[i] << "\n";
}
delete [] p;
return 0;
}
整数に動的にメモリ割り当て
#include < iostream >
using namespace std;
int main()
{
int *p;
p = new int(9);
if(!p) {
cout << "memory set error\n";
return 1;
}
cout << "p set integer is: " << *p << "\n";
delete p;
return 0;
}
オブジェクトを動的に割り当て
#includeusing namespace std; class samp { int i, j; public: void set_ij(int a, int b){ i=a; j=b; } int get_product() { return i*j; } }; int main() { samp *p; p = new samp; if(!p){ cout << "memory set error\n"; return 1; } p->set_ij(4, 5); cout << "multiply is :" << p->get_product() << "\n"; delete p; return 0; }
newとdelete
#include < iostream >
using namespace std;
int main()
{
int *p;
p = new int;
if(!p) {
cout << "memory set error\n";
return 1;
}
*p = 1000;
cout << "p point integer is: " << *p << "\n";
delete p;
return 0;
}
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;
}
コンストラクタに複数の引数
#include < iostream >
using namespace std;
class samp {
int a, b;
public:
samp(int n, int m){ a = n; b = m; }
int get_a() { return a; }
int get_b() { return b; }
};
int main()
{
samp ob[4][2] = {
samp(1, 2), samp(3, 4),
samp(5, 6), samp(7, 8),
samp(9, 10), samp(11, 12),
samp(13, 14), samp(15, 16)
};
int i;
for(i = 0; i<4; i++){
cout << ob[i][0].get_a() << ' ';
cout << ob[i][0].get_b() << "\n";
cout << ob[i][1].get_a() << ' ';
cout << ob[i][1].get_b() << "\n";
}
cout << "\n";
return 0;
}
オブジェクトの多次元配列
#include < iostream >
using namespace std;
class samp {
int a;
public:
samp(int n){ a = n; }
int get_a() { return a; }
};
int main()
{
samp ob[4][2] = {
1, 2,
3, 4,
5, 6,
7, 8
};
int i;
for (i=0; i<4; i++){
cout << ob[i][0].get_a() << ' ';
cout << ob[i][1].get_a() << "\n";
}
cout << "\n";
return 0;
}