抽出子の作成

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

書式フラグの設定

#include < iostream >
using namespace std;

int main()
{
    cout  << 123.23 << " hello " << 100 << '\n';
    cout << 10 << ' ' << -10 << '\n';
    cout << 100.0 << "\n\n";
    
    cout.unsetf(ios::dec);
    cout.setf(ios::hex | ios::scientific);
    cout << 123.23 << " hello " << 100 << '\n';
    
    cout.setf(ios::dec);
    cout.setf(ios::hex | ios::scientific);
    cout << 123.23 << " hello " << 100 << '\n';
    
    cout.setf(ios::showpos);
    cout << 10 << ' ' << -10 << '\n';
    cout. setf(ios::showpoint | ios::fixed);
    cout << 100.0;
    
    return 0;
}

仮想基本クラス

#include < iostream >
using namespace std;

class base{
public:
    int i;
};

class derived1 : virtual public base {
public:
    int j;
};

class derived2 : virtual public base{
public:
    int k;
};

class derived3 : public derived1, public derived2{
public:
    int product() { return i * j * k; }
};

int main()
{
    derived3 ob;
    
    ob.i = 10;
    ob.j = 3;
    ob.k = 5;
    
    cout << "multiply is " << ob.product() << '\n';
    
    return 0;
}

派生クラスが複数の基本クラスを直接継承

#include < iostream >
using namespace std;

class B1 {
public:
    B1() { cout << "B1 constructor call \n"; }
    ~B1() { cout << "B1 destructor call \n"; }
};

class B2 {
    int b;
public:
    B2() { cout << "B2 contructor call \n"; }
    ~B2() { cout << "B2 destructor call \n"; }
};


class D : public B1, public B2{
public:
    D() { cout << "call D constructor\n"; }
    ~D() { cout << "call D destructor\n"; }
};

int main()
{
    D ob;
    
    return 0;
}

多重継承

#include < iostream >
using namespace std;

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

class D1 : public B1 {
    int b;
public:
    D1(int x, int y): B1(y)
    {
        b = x;
    }
    int getb(){ return b; }
};

class D2 : public D1 {
    int c;
public:
    D2(int x, int y, int z): D1(y, z)
    {
        c = x;
    }
    void show() {
        cout << geta() << ' ' << getb() << ' ';
        cout << c << '\n';
    }
};

int main()
{
    D2 ob(1, 2, 3);
    ob.show();
    
    cout << ob.geta() << ' ' << ob.getb() << '\n';
    return 0;
}

派生クラスと基本クラス

#include < iostream >
using namespace std;

class base {
    int i;
public:
    base(int n){
        cout << "call base class constructor\n";
        i = n;
    }
    ~base() { cout << "call base class destructor\n"; }
    void showi() { cout << i << '\n'; }
};

class derived : public base {
    int j;
public:
    derived(int n, int m): base(m){
        cout << "call derived class constructor \n";
        j = n;
    }
    ~derived() { cout << "call derived class destructor\n"; }
    void showj() {cout << j << '\n'; }
};

int main()
{
    derived o(10, 20);
    
    o.showi();
    o.showj();
    
    return 0;
}