在構造函數中從鍵盤輸入值

方法一:利用istream

      

#include<iostream>
using namespace std;

class Date
{
private:
    int d,m,y;
public:
    Date(istream &in);
};

int main()
{
    Date my_date(cin);
    my_date.show();
}

Date::Date(istream &in)
{
    in >> y >> m >> d;
}


方法二:直接利用cin輸入

#include<iostream>
using namespace std;

class Date
{
private:
    int d,m,y;
public:
    Date();
    void show()const;
};

int main()
{
    Date my_date;
    my_date.show();
}

Date::Date()
{
    int a,b,c;
    cin >> a >> b >> c;
    y = a;
    m = b;
    d = c;
}

void Date::show()const
{
    cout << Date::d << endl;
}




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章