c++类 输出小程序例子

#include <iostream>
using namespace std; 

class Obj
{
    public:
    void operator << (int s)
    {
        cout <<"int: " << s << flush <<'\n';
    }

    void operator << (double s)
    {
        cout <<"double: " << s << flush <<'\n';
    }

    void operator << (string s)
    {
        cout <<"string: " << s << flush <<'\n'; 
    }

    void A(int s)
    {
        operator << (s);
    }

    void A(double s)
    {
        operator << (s);
    }

    void A(string s)
    {
        operator << (s);
    }

};

int main(void)
{
    Obj K;
    Obj *P = new (Obj);
    int x=12345;
    K << x;
    K << x*0.01;
    K << "115 = 114+1";

    K.A(556);
    K.A(556.01);
    K.A("I am a doctor.");

    P->A("GameOver");
    *P << "GameOver2";
    return 0;
}

输出结果:
int: 12345
double: 123.45
string: 115 = 114+1
int: 556
double: 556.01
string: I am a doctor.
string: GameOver
string: GameOver2


Process exited with return value 0
Press any key to continue …

发布了23 篇原创文章 · 获赞 37 · 访问量 9073

猜你喜欢

转载自blog.csdn.net/weixin_41221124/article/details/79637681