HiveBrain v1.2.0
Get Started
← Back to all entries
patterncppModerate

C++ calculator using classes

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
calculatorusingclasses

Problem

This is my first time using classes objects and functions. What can I improve on?

#include 
#include 
using namespace std;

class functions{
public:
    void Body(){
        cout > func;
cout > x;
        cout > y;
        cout > x;
        cout > y;
        cout > x;
        cout > y;
        cout > x;
        cout > y;
        cout << x << " / " << y << " = ";
        cout << key.Division(x, y);
        break;
    default:
        cout << "Invalid Input...";
        break;
}

}

Solution

Unless you'll be maintaining an accumulated number for multiple operations, this may not be the best use of classes. For your code, you'd get the same effect by just performing these calculations within the switch statement (something similar to this). Right now, you're just using a class to contain similar functions, including a trivial output function.

Some additional notes:

  • Try not to use using namespace std.



  • Indent everything within main() as well. Not doing so may make it hard to tell that the code is being contained within something.



  • You don't need `` here, so just remove it.



  • It may be useful to prevent the user from dividing by 0, which is undefined behavior.

Context

StackExchange Code Review Q#146616, answer score: 15

Revisions (0)

No revisions yet.