C++:类与对象1(类测试程序)
2016-01-08 14:08
357 查看
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语言栈帧
- C语言学习之关键字第十讲
- C语言:从文本文件中读取输入数据(升级版)
- netbeans c/c++ (or gcc) & nasm co-work
- C语言学习之关键字第九讲
- C语言tips不完全汇总
- c++ new
- C++处理一个动态规划的问题
- c++实现gray code(格雷码)
- C++ STL之stack
- c语言位域
- c++ 函数调用在进入下一个循环的时候会再次初始化参数,将函数体直接写进去就正常
- c++中string类型用下标初始化后str.size()为0 输出string值为空
- C++中尖括号和引号的区别---转载
- C++中使用array报错 requires compiler and library surpport for the ISO c++ 2011 standard
- C++中getline被跳过
- c++11move语意的便捷理解
- 指向函数的指针数组
- C++11中的右值引用及move语义编程
- #if defined和#if !defined(c语言的宏定义)