#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就要用重載輸入輸出流 |

嚕嚕噠
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; } |
- 2 回答
- 0 關注
- 605 瀏覽
添加回答
舉報
0/150
提交
取消