modify for cpp & change sample code