Menu

C++ Operator Overloading
📰

C++ Operator Overloading

DEV Community: cpp·Yunus Emre Ak·13 days ago
#dDNCmHBI
#devtocpp#devto#dev#include#amount
Long article (3064 words) — no AI summary yet.
Reading 0:00
15s threshold

(Note this is not the final post, there will be more implementation later.)

Think following scenario: We are trying to track our money. So we create a class for it.

#include <string> #include <iostream>

class Money { public : std :: string currency ; int amount ; } ;

int main ( ) { Money inBank ; inBank . currency = "dollar" ; inBank . amount = 10 ;

std :: cout << "Total money I have: " << inBank . amount << " " << inBank . currency << std :: endl ;

return 0 ; }

// Total money I have: 10 dollar

Enter fullscreen mode

Exit fullscreen mode

It looks a bit hard to print total money. It could be so great if we could just write std::cout << inBank << std::endl . Let's try that first.

#include <string> #include <iostream>

class Money { public : std :: string currency ; int amount ; } ;

int main ( ) { Money inBank ; inBank . currency = "dollar" ; inBank .…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.