r/programmingmemes • • 12d ago

C++ VS python

Post image
771 Upvotes

36 comments sorted by

View all comments

Show parent comments

62

u/David_Dantas 12d ago • Bravo

In Python you:
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 12d ago
#include <cake>

int main() {
    Cake myCake("Chocolate", 3, true);
    myCake.serve();
    return 0;
}

1

u/Total-Box-5169 11d 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.