#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 关注
- 679 浏览
添加回答
举报
0/150
提交
取消
