Home > Back-end >  There is something wrong with the operator overloading (system no error, but the results is wrong)
There is something wrong with the operator overloading (system no error, but the results is wrong)

Time:09-24

//for people class reloading the==operator and "=" operator
//"==" operator to judge two people class objects the size of the id attribute;"="operator to realize people class object assignment operation
# include
using namespace std;
The class date//the date class
{
Public:
Date () {};
The date (int a, b int, int) c
{
Cout<" Date the constructor is called "& lt; Year=a;
The month=b;
Day=c;
}
The date (date & amp; B)
{
Cout<" The date copy constructor is called "& lt; Year=b.y ear;
The month=b.m onth.
Day=b.d ay;
}
Int getyear () {return year; }
Int getmonth () {return the month; }
Int getday () {return day; }
To date () {cout<" The date the destructor is called "& lt; Friend class people;
Private:
Int year, month, day.
};
Class people//people
{
Public:
People () {};
People (char [11], a char b [7], [3] char c, char d [16], int, e int f, int g) : birthday (e, f, g)
{
Cout<" People the constructor is called "& lt; The name [11]=a, [11].
The number [7] [7]=b;
Sex [3] [3]=c;
Id [16] [16]=d;
}
Void setpeople ();
Void showpeople ();
People (people & amp; P)
{
Cout<" People copy constructor is called "& lt; The name [11]=p.n ame [11].
The number [7]=p.n umber [7];
Sex [3]=p. ex [3].
Birthday=p.b irthday;
Id [16]=[16] p.i d;
}
~ people () {cout<" People destructor is called "& lt; People operator=(const people & amp; M1)
{
The 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 Boolean operator==(const people & amp; P1, const people & amp; P2)
{
If (p1) id==p2) id)
return true;
The else
return false;
}

Private:
The date birthday;
Char name [11], number [7], sex [3], [16] id;
};
Void people: : setpeople ()//people in the class member function outside the class the implementation of the
{
Cout<" Please enter the number ";
Cin> Number;
Cout<" Please enter the name ";
Cin> name;
Cout<" Please enter the gender ";
Cin> Sex;
Cout<" Please enter the birth year ";
Cin> Birthday. Year;
Cout<" Please enter the month of birth ";
Cin> Birthday. The month;
Cout<" Please enter the birth ";
Cin> Birthday. Day;
Cout<" Please enter the id number ";
Cin> Id;
}
Void people: : showpeople ()
{
Cout<" Number: "& lt; Cout<" Name: "& lt; Cout<" Gender: "& lt; Cout<" Date of birth: "& lt; Cout<" Id number: "& lt; }
Int main ()//main function
{
People (p1, p2, p3;
P1. Setpeople ();
P2. Setpeople ();
Cout<" After inputting "& lt; P1. Showpeople ();
P2. Showpeople ();
Cout<" Interspersing "& lt; If (p1==p2)
Cout<" Same id "& lt; The else
Cout<" Different "id & lt; P3=p;
P3. Showpeople ();
return 0;
}

CodePudding user response:

Grammar is not right!
Strcpy (name, m.n ame)

CodePudding user response:

As friends upstairs said, people kind of overloaded operator=to
 people operator=(const people & amp; 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;
}

C + + environment debugging VS2015
  • Related