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

#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;
}

write()

#include < iostream >
#include < fstream >
#include < cstring >
using namespace std;

int main()
{
    ofstream out("test", ios::out||ios::binary);
    
    if(!out) {
        cout << "can not open output file\n";
        return 1;
    }
    
    double num = 100.45;
    char str[] = "this is a test";
    
    out.write((char *) &num, sizeof(double));
    out.write(str, strlen(str));
    
    out.close();
    
    return 0;
}

ディスク入出力

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

int main(int argc, char *argv[])
{
    if(argc!=2){
        cout << "use: WRITE\n";
        
        return 1;
    }
    ofstream out(argv[1]);
    
    if(!out){
        cout << "cannot open input file\n";
        return 1;
    }
    
    char str[80];
    cout << "write char in disk and stop $\n";
    
    do {
        cout << ": ";
        cin >> str;
        out << str << endl;
    } while (*str != '$');
    
    out.close();
    return 0;
}

独自マニュプレーター

setup()というマニュプレータ

#include < iostream >
using namespace std;

ostream &setup(ostream &stream)
{
    stream.width(10);
    stream.precision(4);
    stream.fill('*');
    
    return stream;
}

int main()
{
    cout << setup << 123.123456;
    
    return 0;
}

抽出子の作成

#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; }
    friend ostream &operator<<(ostream &stream, coord ob);
    friend istream &operator>>(istream &stream, coord &ob);
};

ostream &operator<<(ostream &stream, coord ob)
{
    stream << ob.x << ", " << ob.y << '\n';
    return stream;
}

istream &operator>>(istream &stream, coord &ob)
{
    cout << "input polar: ";
    stream >> ob.x >> ob.y;
    return stream;
}

int main()
{
    coord a(1, 1), b(10, 23);
    
    cout << a << b;
    cin >> a;
    cout << a;
    
    return 0;
}

独自挿入子の作成

#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; }
    friend ostream &operator<<(ostream &stream, coord ob);
};

ostream &operator<<(ostream &stream, coord ob)
{
    stream << ob.x << ", " << ob.y << '\n';
    return stream;
}
int main()
{
    coord a(1, 1), b(10, 23);
    cout << a << b;
    
    return 0;
}

入出力マニュピレーター

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

int main()
{
    cout << hex << 100 << endl;
    cout << oct << 10 << endl;
    
    cout << setfill('x') << setw(10);
    cout << 100 << " hi " << endl;
    
    return 0;
}

C++入出力

#include < iostream >
#include 
using namespace std;

int main()
{
    double x;
    
    cout.precision(4);
    cout << "         x sqrt(x)    x^2\n\n";
    
    for(x = 2.0; x <= 20.0; x++){
        cout.width(7);
        cout << x << "  ";
        cout.width(7);
        cout << sqrt(x) << " ";
        cout.width(7);
        cout << x*x << '\n';
        
    }
    
    return 0;
}

width(),precision(),fill()

#include < iostream >
using namespace std;

int main()
{
    cout.width(10);
    cout << "hello" << '\n';
    cout.fill('%');
    cout.width(10);
    cout << "hello" << '\n';
    cout.setf(ios::left);
    cout.width(10);
    cout << "hello" << '\n';
    
    
    cout.width(10);
    cout.precision(10);
    cout << 123.234567 << '\n';
    cout.width(10);
    cout.precision(6);
    cout << 123.234567 << '\n';
    return 0;
}