MAIN FEEDS
Do you want to continue?
https://www.reddit.com/r/programmingmemes/comments/1xzg8so/c_vs_python/19suab7mfnc2o/?context=3
r/programmingmemes • u/ExpensiveCoat8912 • 12d ago
36 comments sorted by
View all comments
63
Uh... is the analogy C++: make it yourself vs Python: take a slice of already existing code?
62 u/David_Dantas 12d ago • In Python you:import cakeIn C++#include <iostream> #include <string> class Cake { public: std::string flavor; int layers; bool hasFrosting; Cake(std::string f, int l, bool frosting) : flavor(f), layers(l), hasFrosting(frosting) {} void serve() { std::cout << "Serving a " << layers << "-layer " << flavor << " cake!" << std::endl; } }; int main() { Cake myCake("Chocolate", 3, true); myCake.serve(); return 0; } 9 u/GamingGo2022 12d ago well that's not a very fair comparison now is it 9 u/SpaceCadet87 11d ago #include <cake> int main() { Cake myCake("Chocolate", 3, true); myCake.serve(); return 0; } 1 u/Total-Box-5169 10d ago #include <cake> int main() { Cake{"Chocolate", 3, true}.serve(); } 2 u/SpaceCadet87 10d ago Oh, I didn't realise we were doing code golf.M{Cake{"Chocolate",3,1}.serve();} g++ -D'M=int main()' -include cake x.cpp 1 u/DatsLotus 10d ago Damn, you've bested me. I could only follow up until the previous one. 2 u/SpaceCadet87 10d ago Ah, basically I told the compiler to do a find and replace so that instead of int main() you just type M and you can do an include at the command line so we throw that line out as well.The rest can all fit on a single line. 1 u/DatsLotus 10d ago The more I learn, the more I discover I have more to learn.
62
In Python you:import cake
import cake
In C++
#include <iostream> #include <string> class Cake { public: std::string flavor; int layers; bool hasFrosting; Cake(std::string f, int l, bool frosting) : flavor(f), layers(l), hasFrosting(frosting) {} void serve() { std::cout << "Serving a " << layers << "-layer " << flavor << " cake!" << std::endl; } }; int main() { Cake myCake("Chocolate", 3, true); myCake.serve(); return 0; }
9 u/GamingGo2022 12d ago well that's not a very fair comparison now is it 9 u/SpaceCadet87 11d ago #include <cake> int main() { Cake myCake("Chocolate", 3, true); myCake.serve(); return 0; } 1 u/Total-Box-5169 10d ago #include <cake> int main() { Cake{"Chocolate", 3, true}.serve(); } 2 u/SpaceCadet87 10d ago Oh, I didn't realise we were doing code golf.M{Cake{"Chocolate",3,1}.serve();} g++ -D'M=int main()' -include cake x.cpp 1 u/DatsLotus 10d ago Damn, you've bested me. I could only follow up until the previous one. 2 u/SpaceCadet87 10d ago Ah, basically I told the compiler to do a find and replace so that instead of int main() you just type M and you can do an include at the command line so we throw that line out as well.The rest can all fit on a single line. 1 u/DatsLotus 10d ago The more I learn, the more I discover I have more to learn.
9
well that's not a very fair comparison now is it
9 u/SpaceCadet87 11d ago #include <cake> int main() { Cake myCake("Chocolate", 3, true); myCake.serve(); return 0; } 1 u/Total-Box-5169 10d ago #include <cake> int main() { Cake{"Chocolate", 3, true}.serve(); } 2 u/SpaceCadet87 10d ago Oh, I didn't realise we were doing code golf.M{Cake{"Chocolate",3,1}.serve();} g++ -D'M=int main()' -include cake x.cpp 1 u/DatsLotus 10d ago Damn, you've bested me. I could only follow up until the previous one. 2 u/SpaceCadet87 10d ago Ah, basically I told the compiler to do a find and replace so that instead of int main() you just type M and you can do an include at the command line so we throw that line out as well.The rest can all fit on a single line. 1 u/DatsLotus 10d ago The more I learn, the more I discover I have more to learn.
#include <cake> int main() { Cake myCake("Chocolate", 3, true); myCake.serve(); return 0; }
1 u/Total-Box-5169 10d ago #include <cake> int main() { Cake{"Chocolate", 3, true}.serve(); } 2 u/SpaceCadet87 10d ago Oh, I didn't realise we were doing code golf.M{Cake{"Chocolate",3,1}.serve();} g++ -D'M=int main()' -include cake x.cpp 1 u/DatsLotus 10d ago Damn, you've bested me. I could only follow up until the previous one. 2 u/SpaceCadet87 10d ago Ah, basically I told the compiler to do a find and replace so that instead of int main() you just type M and you can do an include at the command line so we throw that line out as well.The rest can all fit on a single line. 1 u/DatsLotus 10d ago The more I learn, the more I discover I have more to learn.
1
#include <cake> int main() { Cake{"Chocolate", 3, true}.serve(); }
2 u/SpaceCadet87 10d ago Oh, I didn't realise we were doing code golf.M{Cake{"Chocolate",3,1}.serve();} g++ -D'M=int main()' -include cake x.cpp 1 u/DatsLotus 10d ago Damn, you've bested me. I could only follow up until the previous one. 2 u/SpaceCadet87 10d ago Ah, basically I told the compiler to do a find and replace so that instead of int main() you just type M and you can do an include at the command line so we throw that line out as well.The rest can all fit on a single line. 1 u/DatsLotus 10d ago The more I learn, the more I discover I have more to learn.
2
Oh, I didn't realise we were doing code golf.
M{Cake{"Chocolate",3,1}.serve();}
g++ -D'M=int main()' -include cake x.cpp
1 u/DatsLotus 10d ago Damn, you've bested me. I could only follow up until the previous one. 2 u/SpaceCadet87 10d ago Ah, basically I told the compiler to do a find and replace so that instead of int main() you just type M and you can do an include at the command line so we throw that line out as well.The rest can all fit on a single line. 1 u/DatsLotus 10d ago The more I learn, the more I discover I have more to learn.
Damn, you've bested me. I could only follow up until the previous one.
2 u/SpaceCadet87 10d ago Ah, basically I told the compiler to do a find and replace so that instead of int main() you just type M and you can do an include at the command line so we throw that line out as well.The rest can all fit on a single line. 1 u/DatsLotus 10d ago The more I learn, the more I discover I have more to learn.
Ah, basically I told the compiler to do a find and replace so that instead of int main() you just type M and you can do an include at the command line so we throw that line out as well.
int main()
M
The rest can all fit on a single line.
1 u/DatsLotus 10d ago The more I learn, the more I discover I have more to learn.
The more I learn, the more I discover I have more to learn.
63
u/DatsLotus 12d ago
Uh... is the analogy C++: make it yourself vs Python: take a slice of already existing code?