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

Simple calculator with classes in C++

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

Problem

I came to C++ from Java. This is my attempt at making a calculator on about 2 days worth of knowledge. I don't really know what is redundant or unnecessary. Nor do I know of the better ways to achieve this effect.

First class (it does the math):

#pragma once
#include 
#include 
#include 
using namespace std;
class a {
public:
double add(double a, double b) {
    return a + b;
}
double sub(double a, double b) {
    return a - b;
}
double mult(double a, double b) {
    return a * b;
}
double div(double a, double b) {
    return a / b;
}
};


Second class (it makes the strings):

#pragma once
#include 
#include 
#include 
using namespace std;
class text {
public:
string add(double a, double b, double ans) {
    return to_string(a) + " + " + to_string(b) + " = " + to_string(ans);
}
string sub(double a, double b, double ans) {
    return to_string(a) + " - " + to_string(b) + " = " + to_string(ans);
}
string div(double a, double b, double ans) {
    return to_string(a) + " / " + to_string(b) + " = " + to_string(ans);
}
string mult(double a, double b, double ans) {
    return to_string(a) + " * " + to_string(b) + " = " + to_string(ans);
}
string first() {
    return "First Number: ";
}
string second() {
    return "Second Number: ";
}
string op() {
    return "Operator: ";
}
string wrong() {
    return "Invalid Operator!";
}
};


Main method:

```
#include "a.h"
#include "text.h"
#include
#include
#include
using namespace std;
int main() {
text ask;
a mather;
string temp;
char op;
double a, b, ans;

cout > a;
cout > b;

switch (op) {
case '+':
ans = mather.add(a, b);
temp = ask.add(a, b, ans);
break;
case '-':
ans = mather.sub(a, b);
temp = ask.sub(a, b, ans);
break;
case '/':
ans = mather.div(a, b);
temp = ask.div(a, b, ans);
break;
case '*':
ans = mather.mult(a, b);
temp = ask.mult(a, b, ans);
break;
default:
cout << "Invalid Operator!";
}

cout << temp;
cin.get();
cin.get(); //I don't know how to

Solution

Welcome to C++.

A few notes:

  • When working outside of a toy project, avoid using namespace std. For why, see here.



  • In addition, when working outside of a toy project, I encourage you to define your classes within the scope of a namespace. Only you can fight namespace pollution.



  • Your a header does not make use of the iostream, string, or stringstream headers. They need not be included in that header.



  • Your text header does not make use of the iostream or stringstream headers. They need not be included in that header.



  • Header includes are transitive. Because your main function includes your text header which in turn includes the string header, the main.cpp need not also include string.



  • Your main implementation file does not make use of the sstream headers. They need not be included in that file.



  • Your a class methods made no use of the class instance. That said, these methods could be declared as static.



  • Your text class methods made no use of the class instance. That said, these methods could also be declared as static.



  • When you write to the terminal, you probably want a next line character. You can do this either by appending a \n to the output or by calling to std::endl in the iostream header. For more on the distinction, please see this post.



  • return 0; is actually unnecessary to indicate successful execution of the main function as of C99. reference



Also, please do not return a constexpr char *. Assuming you haven't specified otherwise, your compiler is very likely compiling to the C++ 11 specification. In which case, your string methods will be subject to return value optimization outside of debug mode and no temporary variables or heap allocations will be made.

Context

StackExchange Code Review Q#119840, answer score: 16

Revisions (0)

No revisions yet.