C++:类与对象1(类测试程序)

C++:类与对象1(类测试程序)

题目描述:

Description

下面是一个类的测试程序,编写能使用如下测试程序的类Test:

 int  main()

 {      int a,b;

       Test  x;

       while(cin>>a>>b)

       {       x.intx(a,b);

               x.printx();

       }

       return 0;

 }

 输入两个整数a和b,计算a-b。

Input

包含多组,每组测试有2个整数a和b。

Output

输出算式a-b。

Sample Input

300 100
100 300

Sample Output

300-100=200
100-300=-200

代码区:

#include<iostream>
using namespace std;
class Test
{  
   int a,b;
   public:
  void intx(int a,int b)
  { 
     this->a=a;
     this->b=b;

  }
  void printx()
  {
      cout<<a<<"-"<<b<<"="<<a-b<<endl;
  }
};
int  main()

{
    int a,b;

    Test  x;

    while (cin>>a>>b)

    {
        x.intx(a,b);

        x.printx();

    }

    return 0;

}

若有更好的方法,请留下的你的建议。欢迎评论!

你可能感兴趣的:(C++,类与对象)