#include <iostream> #include <string> using namespace std; class Date { friend class person; public: int year,month,day; friend ostream & operator <<( ostream&os, const Date&date ) { os<<date.year<<""<<date.month<<""<<date.day; return os; } Date(int year=2000,int month=1,int day=1):year(year),month(month),day(day) {} void print() { cout<<year<<","<<month<<","<<day; } }; class person { public: string Name; string Num; Date brith; person(string Name="张三", string Num="123456789", int year=2000,int month=1,int day=1): Name(Name),Num(Num),brith(year,month,day) {} string GetName() { return Name; } string GetNum() { return Num; } Date Getbrith() { return brith; } }; bool compare(person&p1,person&p2) { if(person&p1.year>person&p2.year) { return true; } else { return false; } } if(compare(person&p1.year,person&p2.year)) { cout<<"p1的年龄大"<<endl; } else { cout<<"p2的年龄大"<<end; } class student: private person { private: int StuNum; }; class teacher:public person { private: Date Time; }; class CTA :public student,public teacher { private: int No; }; int main() { person p1("张三","123456789",2000,1,1); person p2("李四","123456789",2001,1,1); cout<<"序号"<<endl; cout<<" 1 "<<endl; cout<<" "<<p1.GetName()<<endl; cout<<" "<<p1.GetNum()<<endl; cout<<" "<<p1.Getbrith()<<endl; cout<<" 2 "<<endl; cout<<" "<<p2.GetName()<<endl; cout<<" "<<p2.GetNum()<<endl; cout<<" "<<p2.Getbrith()<<endl; }改进后的程序
时间: 2023-10-30 18:26:24 浏览: 125
#include <iostream>
#include <iostream>
#include <string>
using namespace std;
class Date {
friend class Person;
public:
int year,month,day;
friend ostream & operator <<( ostream&os, const Date&date ) {
os<<date.year<<"年"<<date.month<<"月"<<date.day<<"日";
return os;
}
Date(int year=2000,int month=1,int day=1):year(year),month(month),day(day) {}
};
class Person {
public:
string Name;
string Num;
Date brith;
Person(string Name="张三", string Num="123456789", int year=2000,int month=1,int day=1):
Name(Name),Num(Num),brith(year,month,day) {}
string GetName() const { return Name; }
string GetNum() const { return Num; }
Date Getbrith() const { return brith; }
};
bool compare(const Person &p1, const Person &p2) {
if(p1.brith.year > p2.brith.year) {
return true;
} else {
return false;
}
}
int main() {
Person p1("张三","123456789",2000,1,1);
Person p2("李四","123456789",2001,1,1);
cout<<"序号"<<endl;
cout<<" 1 "<<endl;
cout<<" "<<p1.GetName()<<endl;
cout<<" "<<p1.GetNum()<<endl;
cout<<" "<<p1.Getbrith()<<endl;
cout<<" 2 "<<endl;
cout<<" "<<p2.GetName()<<endl;
cout<<" "<<p2.GetNum()<<endl;
cout<<" "<<p2.Getbrith()<<endl;
if(compare(p1, p2)) {
cout<<"p1的年龄大"<<endl;
} else {
cout<<"p2的年龄大"<<endl;
}
}
```
改进的地方:
1. 将类名改为首字母大写的驼峰命名法,以符合常规的命名规范。
2. 将友元函数的返回值类型改为 `ostream&`,并将运算符 `<<` 的左操作数改为 `ostream&`,以使友元函数能够支持链式输出。
3. 将 `compare` 函数的参数改为 `const Person&` 类型的引用,以避免不必要的拷贝和修改。
4. 将 `GetName()`、`GetNum()` 和 `Getbrith()` 函数的返回值类型改为 `const string&` 和 `const Date&`,以避免不必要的拷贝。同时,将这些函数声明为 `const` 成员函数,以表示这些函数不会修改成员变量。
5. 将 `compare` 函数中的比较操作符改为成员变量的比较,以遵循面向对象的设计原则。
6. 修改了输出格式,使其更易读。
阅读全文