フレンド関数

関数からそのクラスの非公開メンバにアクセスしたいとき。

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

関数へのオブジェクトの引き渡し

オブジェクトがpass by valueによって関数に渡されます。

#include < iostream >
using namespace std;

class samp{
    int i;
public:
    samp(int n){ i = n; }
    int get_i() { return i; }
};

int sqr_it(samp o)
{
    return o.get_i() * o.get_i();
}

int main()
{
    samp a(10), b(2);
    cout << sqr_it(a) << "\n";
    cout << sqr_it(b) << "\n";
    
    return 0;
}

stackプログラム

1つのオブジェクトのすべてのデータメンバがもう1つのオブジェクトに代入されます。

#include < iostream >
using namespace std;

#define SIZE 10

class stack {
char stck[SIZE];
int tos;
public:
stack();
void push(char ch);
char pop();
};

stack::stack()
{
cout << "creat stack\n"; tos = 0; } void stack::push(char ch) { if(tos==SIZE){ cout << "stack is full\n"; return; } stck[tos] = ch; tos++; } char stack::pop() { if(tos==0){ cout << "stack is empty\n"; return 0; } tos--; return stck[tos]; } int main() { stack s1, s2; int i; s1.push('a'); s1.push('b'); s1.push('c'); s2 = s1; for(i=0; i < 3; i++) cout << "pop s1: " << s1.pop() << "\n"; for(i=0; i < 3; i++) cout << "pop s2: " << s2.pop() << "\n"; return 0; }

オブジェクトの代入

型が同じであれば、1つのオブジェクトをもう1つのオブジェクトに代入することができます。

#include 
using namespace std;

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

int main()
{
    myclass o1, o2;
    
    o1.set(10, 4);
    
    o2 = o1;
    
    o1.show();
    o2.show();
    
    return 0;
}

オーバーロード関数のインライン化

各関数はinlineを使用して宣言。

#include < iostream >
using namespace std;

inline int min(int a, int b)
{
    return a < b ? a: b;
}

inline long min(long a, long b)
{
    return a < b ? a : b;
}

inline double min(double a, double b)
{
    return a < b ? a : b;
}

int main()
{
    cout << min(-10, 10) << "\n";
    cout << min(-10.01, 100.002) << "\n";
    cout << min(-10L, 12L) << "\n";
    
    return 0;
}

インライン関数

関数はどのようなものでもインライン化することができます。

#include 
using namespace std;

class samp {
    int i, j;
public:
    samp(int a, int b);
    int divisible();
};

samp::samp(int a, int b)
{
    i = a;
    j = b;
}

inline int samp::divisible()
{
    return !(i%j);
}

int main()
{
    samp ob1(10, 2), ob2(10, 3);
    
    if(ob1.divisible()) cout << "10 can be divided 2.\n";
    
    if(ob2.divisible()) cout << "10 can be divided 3.\n";
    
    return 0;
}

無名共用体

#include 
using namespace std;

int main()
{
    union {
        unsigned char byte[8];
        double value;
    };
    int i;
    value = 859345.324;
    for(i=0; i<8; i++)
        cout << (int) bytes[i] << " ";
    
    return 0;
}

コンストラクタとデストラクタ

#include 
#include 
#include 
using namespace std;

struct strtype {
    strtype(char *ptr);
    ~strtype();
    void show();
private:
    char *p;
    int len;
};

strtype::strtype(char *ptr)
{
    len = strlen(ptr);
    p = (char *) malloc(len+1);
    if(!p){
        cout << "error memory\n";
        exit(1);
    }
    strcpy(p, ptr);
}

strtype::~strtype()
{
    cout << "release p\n";
    free(p);
}

void strtype::show()
{
    cout << p << " - long: " << len;
    cout << "\n";
}

int main()
{
    strtype s1("this is a test."), s2("I like c++.");
    
    s1.show();
    s2.show();
    
    return 0;
}