修改以下代码使其正确
#include<iostream>#include<string>
using namespace std;
class person
{
public:
person(int a, string b)
{
name = b;
age = a;
}
void print(int a,string b);
string name;
int age;
friend person operator+(person p1, person p2);
};
void person::print(int a, string b)
{
cout << b << "" << a << endl;
}
person operator+(person p1, person p2)
{
return person(p1.name + p2.name, p1.age + p2.age);
}
int main()
{
person p1(17,"黄灿灿");
person p2(18, "马瑞瑞");
person p3 = p2 + p1;
p3.print();
} #include<iostream>
#include<string>
using namespace std;
class person
{
public:
person(int a, string b)
{
name = b;
age = a;
}
void print()
{
cout << "姓名:" << name << endl;
cout << "年龄:" << age << endl;
}
string name;
int age;
friend person operator+(person p1, person p2);
};
person operator+(person p1, person p2)
{
int newAge = p1.age + p2.age;
string newName = p1.name + "和" + p2.name;
person newPerson(newAge, newName);
return newPerson;
}
int main()
{
person p1(18, "小明");
person p2(20, "小红");
person p3 = p1 + p2;
p3.print();
return 0;
}
以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
页:
[1]