//對people類多載“= =”運算子和“=”運算子
//“= =”運算子判斷兩個people類物件id屬性的大小;“=”運算子實作people類物件的賦值操作
#include<iostream>
using namespace std;
class date//date類
{
public:
date(){};
date(int a,int b,int c)
{
cout<<"date建構式被呼叫"<<endl;
year=a;
month=b;
day=c;
}
date(date &b)
{
cout<<"date復制建構式被呼叫"<<endl;
year=b.year;
month=b.month;
day=b.day;
}
int getyear(){return year;}
int getmonth(){return month;}
int getday(){return day;}
~date(){cout<<"date解構式被呼叫"<<endl;}
friend class people;
private:
int year,month,day;
};
class people//people類
{
public:
people(){};
people(char a[11],char b[7],char c[3],char d[16],int e,int f,int g):birthday(e,f,g)
{
cout<<"people建構式被呼叫"<<endl;
name[11]=a[11];
number[7]=b[7];
sex[3]=c[3];
id[16]=d[16];
}
void setpeople();
void showpeople();
people(people &p)
{
cout<<"people復制建構式被呼叫"<<endl;
name[11]=p.name[11];
number[7]=p.number[7];
sex[3]=p.sex[3];
birthday=p.birthday;
id[16]=p.id[16];
}
~people(){cout<<"people解構式被呼叫"<<endl;}
people operator=(const people &m1)
{
name[11]=m1.name[11];
number[7]=m1.number[7];
sex[3]=m1.sex[3];
birthday=m1.birthday;
id[16]=m1.id[16];
return *this;
}
friend bool operator== (const people &p1,const people &p2)
{
if(p1.id==p2.id)
return true;
else
return false;
}
private:
date birthday;
char name[11],number[7],sex[3],id[16];
};
void people::setpeople()//people類內成員函式在類外的實作
{
cout<<"請輸入編號";
cin>>number;
cout<<"請輸入姓名";
cin>>name;
cout<<"請輸入性別";
cin>>sex;
cout<<"請輸入出生年";
cin>>birthday.year;
cout<<"請輸入出生月";
cin>>birthday.month;
cout<<"請輸入出生日";
cin>>birthday.day;
cout<<"請輸入身份證號";
cin>>id;
}
void people::showpeople()
{
cout<<"編號:"<<number<<endl;
cout<<"姓名:"<<name<<endl;
cout<<"性別:"<<sex<<endl;
cout<<"出生日期:"<<birthday.getyear()<<"."<<birthday.getmonth()<<"."<<birthday.getday()<<endl;
cout<<"身份證號:"<<id<<endl;
}
int main()//主函式
{
people p1,p2,p3;
p1.setpeople();
p2.setpeople();
cout<<"錄入完畢"<<endl;
p1.showpeople();
p2.showpeople();
cout<<"顯示完畢"<<endl;
if(p1==p2)
cout<<"id相同"<<endl;
else
cout<<"id不同"<<endl;
p3=p1;
p3.showpeople();
return 0;
}
uj5u.com熱心網友回復:
語法就不對呀strcpy(name, m.name)才對
uj5u.com熱心網友回復:
正如樓上朋友說的,people類的多載運算子=改為people operator=(const people &m1)
{
strncpy(name,m1.name, 11);
strncpy(number,m1.number, 7);
strncpy(sex, m1.sex, 3);
birthday = m1.birthday;
strncpy(id, m1.id, 16);
return *this;
}
VS2015 C++環境除錯
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/71314.html
標籤:新手樂園
