オーバーロードバージョン

#include < iostream >
#include < vector >
using namespace std;

class Demo {
    double d;
public:
    Demo() { d = 0.0; }
    Demo(double x){ d = x; }
    
    Demo &operator=(double x){
        d = x; return *this;
    }
    double getd() { return d; }
};

bool operator<(Demo a, Demo b)
{
    return a.getd() < b.getd();
}

bool operator==(Demo a, Demo b)
{
    return a.getd() == b.getd();
}

int main()
{
    vector v;
    int i;
    
    for(i=0; i<10; i++)
        v.push_back(Demo(i/3.0));
    
    for(i=0; i
	

コンストラクタ

#include < iostream >
using namespace std;

class myclass {
    int a;
public:
    myclass(int x){ a= x; }
    int geta() { return a; }
};

int main()
{
    myclass ob(4);
    
    cout << ob.geta();
    
    return 0;
}

new(nothrow)オプション

割り当てエラーを強制的に発生させています。

#include 
#include < new >
using namespace std;

int main()
{
    double *p;
    
    do {
        p = new(nothrow) double[100000];
        if(p) cout << "succeed memory set\n";
        else cout << "memory set error\n";
    } while(p);
    
    return 0;
}

try/catchブロック

#include < iostream >
#include 
using namespace std;

int main()
{
    int *p;
    try {
        p = new int;
    } catch (bad_alloc xa){
        cout << "error memory set.\n";
        return 1;
    }
    
    for(*p = 0; *p < 10; (*p)++)
        cout << *p << " ";
    
    delete p;
    
    return 0;
}

catchの使用方法

#include < iostream >
using namespace std;

void Xhandler(int test)
{
    try{
        if(test==0) throw test;
        if(test==1) throw 'a';
        if(test==2) throw 123.23;
    }
    
    catch(...){
        cout << "get!\n";
    }
}

int main()
{
    cout << "start\n";
    Xhandler(0);
    Xhandler(1);
    Xhandler(2);
    cout << "finish";
    
    return 0;
}

catchの例外処理

#include < iostream >
using namespace std;

int main()
{
    cout << "start\n";
    
    try {
        cout << "try block internal\n";
        throw 10;
        cout << "this is not action";
    }
    
    catch (int i){
        cout << "get! number is : ";
        cout << i << "\n";
    }
    
    cout << "finish";
    
    return 0;
}

複数の汎用データ型の定義

#include < iostream >
using namespace std;

template  class myclass
{
    Type1 i;
    Type2 j;
public:
    myclass(Type1 a, Type2 b) { i = a; j = b; }
    void show() { cout << i << ' ' << j << '\n'; }
};

int main()
{
    myclass ob1(10, 0.23);
    myclass ob2('X', "This is a test");
    
    ob1.show();
    ob2.show();
    
    return 0;
}

ランダムなイベントに対する仮想関数

#include < iostream >
#include < cstdlib >
using namespace std;

class base {
public:
    int i;
    base(int x){ i = x; }
    virtual void func()
    {
        cout << "use base func(): ";
        cout << i << '\n';
    }
};

class derived1 : public base {
public:
    derived1(int x) : base(x) {}
    void func()
    {
        cout << "use derived 1 func(): ";
        cout << i*i << '\n';
    }
};

class derived2 : public base {
public:
    derived2(int x) : base(x) {}
    void func()
    {
        cout << "use derived2 func() : ";
        cout << i+i << '\n';
    }
};

int main()
{
    base *p;
    derived1 d_ob1(10);
    derived2 d_ob2(10);
    int i, j;
    
    for(i=0; i<10; i++){
        j = rand();
        if((j%2)) p = &d_ob1;
        else p = &d_ob2;
        p->func();
    }
    return 0;
}

派生クラスへのポインタ

#include < iostream >
using namespace std;

class base {
    int x;
public:
    void setx(int i){ x = i; }
    int getx() { return x; }
};

class derived : public base {
    int y;
public:
    void sety(int i){ y = i; }
    int gety() { return y; }
};

int main()
{
    base *p;
    base b_ob;
    derived d_ob;
    
    p = &b_ob;
    p->setx(10);
    cout << "base class object x: " << p->getx() << '\n';
    
    p = &d_ob;
    p->setx(99);
    
    d_ob.sety(88);
    cout << "derived class object x: " << p->getx() << '\n';
    cout << "derived class object y: " << d_ob.gety() << '\n';
    
    return 0;
}