a_8_3.cpp
来自「C++应用教程原码,里面包含该书中有十三章内容的代码,详细具体」· C++ 代码 · 共 34 行
CPP
34 行
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
class Student {
public:
Student(string i,int j)
: name(i), age(j)
{ }
friend int operator -(const Student &,const Student &);
string getName()
{ return name;}
int getAge()
{ return age;}
private:
int age;
string name;
};
int operator - (const Student &a,const Student &b)
{ return (a.age-b.age);}
void main()
{ Student a("Huangshan",22),b("Liuyu",25);
cout << a.getName()<<","<< a.getAge()<<endl;
cout << b.getName()<<","<< b.getAge()<<endl;
if ((a-b)>0)
cout << a.getName() << "比" << b.getName() << "大"<< a-b<<"岁";
else
cout << a.getName() << "比" << b.getName() << "小"<< b-a<<"岁";
cin.get(); //等待结束,以便调测程序,可以删除
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?