patterncppModerate
Simple calculator with classes in C++
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):
Second class (it makes the strings):
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
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:
Also, please do not return a
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
aheader does not make use of theiostream,string, orstringstreamheaders. They need not be included in that header.
- Your
textheader does not make use of theiostreamorstringstreamheaders. They need not be included in that header.
- Header includes are transitive. Because your main function includes your
textheader which in turn includes thestringheader, the main.cpp need not also includestring.
- Your
mainimplementation file does not make use of thesstreamheaders. They need not be included in that file.
- Your
aclass methods made no use of the class instance. That said, these methods could be declared as static.
- Your
textclass 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
\nto the output or by calling tostd::endlin theiostreamheader. 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.