I am new here and I am new to C++... been slowly picking things up. I created a Basic Calc program and I was wonder what you guys think on how its writen. I know its a little extreme for a basic calc but I wanted to make it transferable and upgradable as much as possible.
Code:#include <iostream> class Math //Math object that has all the basic math functions { public: int Add() const { return numx + numy; } int Sub() const { return numx - numy; } int Div() const { return numx / numy; } int Mult() const { return numx * numy; } void SetXY(); private: int numx; int numy; }; void Math::SetXY() //Prompts for X and Y { cout << "\nx:"; cin >> numx; cout << "y:"; cin >> numy; }; int main() { using namespace std; int menu(); int answer(int ans); int select(unsigned short int sel); unsigned short int sel = 0; for (;sel != 5;) //Main Loop of Program { select(menu()); //Calling Menu function and passes it to the Select Function } return 0; //Exits Program } //Printing a menu on the screen and asking for a selection. int menu() { unsigned short int select = 0; for (;;) { cout << "1. Add\n"; cout << "2. Subtract\n"; cout << "3. Divide\n"; cout << "4. Mutiple\n"; cout << "5. Exit.\n"; cout << "\nSelect one:"; cin >> select; if (select > 5) cout << "Invalid Selection, try again.\n"; else break; } return select; } //Prints the Answer int answer(int ans) { cout << "\nAnswer: " << ans << "\n"; } //Checking select to excute the correct function of object NewMath int select(unsigned short int sel) { Math *NewMath = new Math; //Creates math object on the heap switch (sel) { case 5: cout << "Exiting...\n"; //Exiting Program break; case 4: cout << "Mutilping...\n"; //Calling Mutilpe Function NewMath->SetXY(); answer(NewMath->Mult()); break; case 3: cout << "Dividing...\n"; //Calling Divide Function NewMath->SetXY(); answer(NewMath->Div()); break; case 2: cout << "Subtracting...\n"; //Calling Subtract Function NewMath->SetXY(); answer(NewMath->Sub()); break; case 1: cout << "Adding...\n"; //Calling Add Function NewMath->SetXY(); answer(NewMath->Add()); } delete NewMath; //Delete Math object }



LinkBack URL
About LinkBacks


