In: Computer Science
Code in C++ programming language description about lesson Calculator (multiple, sum, dived, sub) example.
#include
#include
using namespace std;
class numbers {
protected:
float x,y;
public:
numbers(float xx=0.0,float yy=0.0)
:x(xx),y(yy) {
}
/*void printxy(){
cout <<"x=" << x<<",y=" << y < }*/ }; class OP : protected numbers { // int x,y; // void printxy() public: OP(float xx=0.0, float yy=0.0) : numbers(xx,yy) {} void setxy(float xx,float yy){ x=xx; y=yy; } void input(){ cout <<"enter x: "; cin>>x; cout <<"enter y: "; cin>>y; } void print() { cout <<"x=" << x<<",y=" << y < } float sum() { return x+y; } float sub() { return x-y; } float mult() { return x*y; } float div() { return x/y; } }; void menu() { cout <<"=====menu================\n"; cout <<" Enetr npute number[i]\n"; cout <<" Enter sum [+]\n"; cout <<" Enter sub[-]\n"; cout <<" Enter multiple [*]\n"; cout <<" Enter exit [q]\n"; cout <<"==========================\n"; cout <<"choice: "; } int main() { OP op(2.5,6.5); char ch; while(1) { menu() ; cin.sync(); cin.get(ch) ; switch(ch){ case 'i': op.input(); break; case 'p': op.print(); break; case '+': op.print(); cout <<"sum=" << op.sum() < break; case '-': op.print(); cout <<"sub=" < break; case '*': op.print(); cout <<"mult=" << op.mult() < break; case '/': op.print(); cout <<"div=" < break; case 'q': exit(0); default : cout <<">>sorry invalid op...\n"; } } }