此程序运用c++程序中的运算符重载来做。
text.cpp
#include<iostream>
#include<iomanip>
#include"Score.h"
using namespace std;
int main()
{
Score a,b,c;
cout <<"输入三个分数" <<endl;
cin>>a;
cin>>b;
cin>>c;
a=a+b;
cout<<"a=a+b的值为:";
cout <<a<<endl;
a=a-b;
cout <<"a=a-b的值为:";
cout <<a<<endl;
cout <<"b=b*c的值为:";
b=c*b;
cout <<b<<endl;
cout <<"a=a/c的值为:";
a=a/c;
cout <<a<<endl;
}
Srore.h
#include<iostream>
#ifndef SCORE_H
#define SCORE_H
using namespace std;
class Score
{
friend ostream & operator<<(ostream&, const Score &);
friend istream & operator>>(istream&, Score &);
friend Score operator+(Score x,Score y);
friend Score operator-(Score x,Score y);
friend Score operator*(Score x,Score y);
friend Score operator/(Score x,Score y);
public:
Score(int m=0,int d=0);
~Score();
private:
int mole;
int de