Cosmic ray feedback in hydrodynamical simulations of galaxy and galaxy cluster formation