Listing 2: Simple C++ example

//
//    X.h
//
class X
    {
public:
    X(int = 10);
    ~X();
    X &operator=(X const &);
    operator int();
    static int count();
private:
    int value_;
    static int count_;
    };

//
//    X.cpp
//
#include "X.h"

X::X(int value) : value_(value)
    {
    ++count_;
    }

X::~X()
    {
    --count_;
    }

X &X::operator=(X const &that)
    {
    value_ = that.value_;
    return *this;
    }

X::operator int()
    {
    return value_;
    }

int X::count()
    {
    return count_;
    }

int X::count_ = 0;

//
//    main.cpp
//
#include <stdio.h>
#include "X.h"

int main()
    {
    printf("count is %d\n\n", X::count());
    // block
        {
        X x1;
        X x2 = 20;
        printf("count is %d\n\n", X::count());
        printf("x1 is %d\n", (int) x1);
        printf("x2 is %d\n\n", (int) x2);
        x1 = x2;
        printf("x1 is %d\n\n", (int) x1);
        }
    printf("count is %d\n", X::count());
    }
— End of Listing —