亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

您好,以下代碼是關于C++問題#include <iostream>的,請問該怎么辦?

您好,以下代碼是關于C++問題#include <iostream>的,請問該怎么辦?

C++ C
元芳怎么了 2021-12-07 15:11:01
#include <string> using namespace std; class date{private:int year ;int month;int day; public :date(){cout<<"構造函數";};void setDate(int y ,int m ,int d ){year=y;month=m;day=d;};  date (date&bir){year =bir.year ;month=bir.month;day=bir.day;};int getYear(){return year;};int getMonth(){return month;};int getDay(){return day;};};class people{private:date birthday;int num;char name[20];char sex[4];char id[20];public :people(){};void input(){int y,m,d;cout<<"錄入數據";cout<<"編號"<<endl;cin>>num;cout<<"姓名"<<endl;cin>>name;cout<<"性別"<<endl;cin>>sex; //40cout<<"身份證編號"<<endl;cin>>id;cout<<"出生日期(年月日)"<<endl;cin>>y>>m>>d;birthday.setDate(y,m,d);};void output(){cout<<"編號"<<num<<endl;cout<<"姓名"<<name<<endl;cout<<"性別"<<sex<<endl ; cout<<"身份證編號"<<id<<endl;cout<<"生日"<<birthday;}; };int main() {people p1;p1.input();p1.output();return 0;}最后在output()方法里面輸出birthday報錯,該怎樣修改?本人C++小白,問下復制函數到底有什么用?以上面為例,把那個的復制函數去掉可否?
查看完整描述

2 回答

?
智慧大石

TA貢獻1946條經驗 獲得超3個贊

#include <iostream>//要加上這個頭文件#include <string> using namespace std; class date{private:    int year ;    int month;    int day;   public :    date()    {        cout<<"構造函數";    }; void  setDate(int y ,int m ,int d )    {    year=y;    month=m;    day=d;    };     date (date&bir) {     year =bir.year ;     month=bir.month;     day=bir.day; }; int getYear() { return year; }; int getMonth() { return month;}; int getDay() { return day; };  void out()  {     cout<<"年:"<<getYear()<<" 月"<<getMonth()<<"  日"<<getDay()<<endl;  }//加個輸出函數};class people{private:    date birthday;    int  num;    char name[20];    char sex[4];    char id[20];      public :    people()    {    };    void input()    {        int y,m,d;        cout<<"錄入數據";        cout<<"編號"<<endl;        cin>>num;        cout<<"姓名"<<endl;        cin>>name;        cout<<"性別"<<endl;        cin>>sex;            cout<<"身份證編號"<<endl;        cin>>id;        cout<<"出生日期(年月日)"<<endl;        cin>>y>>m>>d;        birthday.setDate(y,m,d);     };    void output()    {        cout<<"編號"<<num<<endl;        cout<<"姓名"<<name<<endl;        cout<<"性別"<<sex<<endl  ;         cout<<"身份證編號"<<id<<endl;        //cout<<"生日"<<birthday;        birthday.out();//調用輸出函數}; }; int main()  {    people p1;    p1.input();    p1.output();    return 0;}//樓下的已經說了為什么錯了,如果真要輸出cout<<birthday就要用重載輸入輸出流



查看完整回答
反對 回復 2021-12-12
?
嚕嚕噠

TA貢獻1784條經驗 獲得超7個贊

1給cout輸出流添加類。2.《深度探索C++對象模型》《C++沉思錄》兩本書精解。

3,函數體后可不加分號,但類體大括號外必須加分號。


#include <string> #include<iostream>using namespace std;class date {private:    int year;    int month;    int day;public:    date() {        cout << "構造函數";    }    void setDate(int y, int m, int d) {        year = y;        month = m;        day = d;    }        date(date&bir) {        year = bir.year;        month = bir.month;        day = bir.day;    }        int getYear() {        return year;    }    int getMonth() {        return month;    }    int getDay() {        return day;    }friend ostream& operator<<(ostream&,date& d);};ostream& operator<<(ostream& os,date& d){    return os<<d.year<<'/'<<d.month<<'/'<<d.day<<endl;     }class people {private:    date birthday;    int num;    char name[20];    char sex[4];    char id[20]; public:    people() {    }    void input() {        int y, m, d;        cout << "錄入數據";        cout << "編號" << endl;        cin >> num;        cout << "姓名" << endl;        cin >> name;        cout << "性別" << endl;        cin >> sex;    //40        cout << "身份證編號" << endl;        cin >> id;        cout << "出生日期(年月日)" << endl;        cin >> y >> m >> d;        birthday.setDate(y, m, d);     }    void output() {        cout << "編號" << num << endl;        cout << "姓名" << name << endl;        cout << "性別" << sex << endl;        cout << "身份證編號" << id << endl;        cout << "生日" <<birthday <<endl;//;     }}; int main() {    people p1;    p1.input();    p1.output();    return 0;}



查看完整回答
反對 回復 2021-12-12
  • 2 回答
  • 0 關注
  • 605 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號