C++自学:运算符重载 - insertion operator

以下代码重载了insertion operator “<<”。

#include 
#include 

using namespace std;

struct YouTubeChannel{
    string Name;
    int SubscribersCount;

    YouTubeChannel(string name, int subscribersCount){
        Name = name;
        SubscribersCount = subscribersCount;
    }
};

ostream& operator<<(ostream& COUT, YouTubeChannel& ytChannel){
        COUT << "Name: " << ytChannel.Name << endl;
        COUT << "SubscribersCount: " << ytChannel.SubscribersCount << endl;
        return COUT;
}

int main(){

    YouTubeChannel yt1 = YouTubeChannel("dell", 5000);
    YouTubeChannel yt2 = YouTubeChannel("asus", 7000);

    cout << yt1 << yt2;

    cout << 7;

    return 0;
}

你可能感兴趣的:(c++学习,c++,算法)