Skip to content

Instantly share code, notes, and snippets.

@AnthoniG
Created February 26, 2022 11:43
Show Gist options
  • Save AnthoniG/1e8d266e57380ed97873e15af3b1ead9 to your computer and use it in GitHub Desktop.
Save AnthoniG/1e8d266e57380ed97873e15af3b1ead9 to your computer and use it in GitHub Desktop.
C++ Operator Overloading
#include <iostream>
#include<string>
#include<list>
using namespace std;
struct YouTubeChannel {
string Name;
int SubscribersCount;
YouTubeChannel(string name, int subscribersCount) {
Name = name;
SubscribersCount = subscribersCount;
}
bool operator==(const YouTubeChannel&channel) const{
return this->Name == channel.Name;
}
};
ostream& operator<<(ostream& COUT, YouTubeChannel& ytChannel) {
COUT << "Name: " << ytChannel.Name << endl;
COUT << "Subscribers: " << ytChannel.SubscribersCount << endl;
return COUT;
}
struct MyCollection {
list<YouTubeChannel>myChannels;
void operator+=(YouTubeChannel& channel) {
this->myChannels.push_back(channel);
}
void operator-=(YouTubeChannel& channel) {
this->myChannels.remove(channel);
}
};
ostream& operator<<(ostream& COUT, MyCollection& myCollection) {
for (YouTubeChannel ytChannel : myCollection.myChannels)
COUT << ytChannel << endl;
return COUT;
}
int main()
{
YouTubeChannel yt1 = YouTubeChannel("CodeBeauty", 75000);
YouTubeChannel yt2 = YouTubeChannel("My second channel", 80000);
MyCollection myCollection;
myCollection += yt1;
myCollection += yt2;
myCollection -= yt2;
cout << myCollection;
cin.get();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment