The evolution of chambers in the heart in different classes of vertebrates is because of :
Metabolism
Adaptation with different environment.
Fishes have 2 chambers in their heart, amphibians and reptiles 3 chambers (exception of crocodile which has 4 chambers), Birds and mammals have 4 chambers.
It evolved according to the environment they lived in and according to their metabolism.