It terms of logistics probably doing something like Prague-Vienna-Budapest-Prague (or reverse order) would be the simplest. Krakow not only geographically more distant (=travel time), but also journey there would increase the sum you'll spend on train tickets. However if you do decide to include Krakow, then still it's not a big problem to incorporate it into an itinerary (e.g. Prague-Budapest-Krakow-Prague)
Tickets (let's stick to plan without Krakow for now):
Prague-Vienna: multiple daytime trains (taking 4:45), and via
Czech Railways eShop it's possible to get tickets for as low as 19 euro per passenger. Also available overnight option
Vienna-Budapest: again multiple trains (taking +/- 3 hours), discounted tickets (start from 19 euro) can be bought via Austrian Railways website (we have a step by step guide on how to purchase austrian train tickets -
http://europetrainsguide.com/Advice/How-to-Buy-Train-Tickets/Austria-OeBB.html)
Budapest-Prague: less train than in previous cases, but still quite a few per day (+ overnight one), discounted tickets (start from 19 euro) can be bought via Hungarian Railways website (step by step guide -
http://europetrainsguide.com/Advice/How-to-Buy-Train-Tickets/Hungary-MAV.html )
But remember that discounted tickets in all the cases are limited in number per departure, thus require in advance purchase, plus there are some limitations in terms of exchangeability & refund, thus make sure you are ok with such tickets purchase conditions
Regarding First vs. Second class... hmm... in the case of you potential trips the difference isn't essential (however there are even discounted 1st class tickets available), so, imho, no need to overpay. While in case you decide to use overnight train, you'll probably will want to be all in the same compartment and it's 2nd class anyways (either 2 bed sleeper or 4 berth couchette)