#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;
}
オブジェクトの配列の初期化
#includeusing namespace std; class samp { int a; public: samp(int n){ a = n; } int get_a() { return a; } }; int main() { samp ob[4] = { -1, -2, -3, -4 }; int i; for(i=0; i<4; i++) cout << ob[i].get_a() << ' '; cout << "\n"; return 0; }
オブジェクトの配列
オブジェクトは変数であり、他の型の変数と同じ機能と属性を持っています。
#include < iostream >
using namespace std;
class samp {
int a;
public:
void set_a(int n) { a = n; }
int get_a() { return a; }
};
int main()
{
samp ob[4];
int i;
for(i=0; i<4; i++) ob[i].set_a(i);
for(i=0; i<4; i++) cout << ob[i].get_a();
cout << "\n";
return 0;
}
フレンド関数
二つの異なる型のクラスが共通の特性を持っているとき、それを比較する。
#include < iostream >
using namespace std;
class truck;
class car {
int passengers;
int speed;
public:
car(int p, int s){ passengers = p; speed = s; }
friend int sp_greater(car c, truck t);
};
class truck {
int weight;
int speed;
public:
truck(int w, int s){ weight = w, speed = s; }
friend int sp_greater(car c, truck t);
};
int sp_greater(car c, truck t)
{
return c.speed - t.speed;
}
int main()
{
int t;
car c1(6, 55), c2(2, 120);
truck t1(10000, 55), t2(20000, 72);
cout << "c1 and t1 compare:\n";
t = sp_greater(c1, t1);
if(t<0) cout << "truck is faster\n";
else if(t==0) cout << "car and truck speed is same\n";
else cout << "car is faster\n";
cout << "\nc2 and t2 compare:\n";
t = sp_greater(c2, t2);
if(t<0) cout << "truck is faster\n";
else if(t==0) cout << "car and truck speed is same\n";
else cout << "car is faster\n";
return 0;
}
フレンド関数
関数からそのクラスの非公開メンバにアクセスしたいとき。
#include < iostream >
using namespace std;
class myclass {
int n, d;
public:
myclass(int i, int j){ n = i; d = j; }
friend int isfactor(myclass ob);
};
int isfactor(myclass ob)
{
if(!(ob.n % ob.d)) return 1;
else return 0;
}
int main()
{
myclass ob1(10, 2), ob2(13, 3);
if(isfactor(ob1)) cout << "2 is 10 factor\n";
else cout << "2 is not 10 factor \n";
if(isfactor(ob2)) cout << "3 is 13 factor \n";
else cout << "3 is not 13 factor \n";
return 0;
}
オブジェクト返し
#include < iostream >
#include < cstring >
using namespace std;
class samp{
char s[80];
public:
void show() { cout << s << "\n"; }
void set(char *str) { strcpy(s, str); }
};
samp input()
{
char s[80];
samp str;
cout << "type character: ";
cin >> s;
str.set(s);
return str;
}
int main()
{
samp ob;
ob = input();
ob.show();
return 0;
}
オブジェクトのコピー
オブジェクトのコピーを作成すると、コンストラクタ関数は呼び出されません。
#include < iostream >
using namespace std;
class samp {
int i;
public:
samp(int n ){
i = n;
cout << "call contractor \n";
}
~samp() { cout << "call destroctor\n"; }
int get_i() { return i; }
};
int sqr_it(samp o)
{
return o.get_i() * o.get_i();
}
int main()
{
samp a(10);
cout << sqr_it(a) << "\n";
return 0;
}