Class constructor help please.

Hey guys, I need help with my hw. My teacher wants us to create constructors to a class he already created for us. Here is the header file, and the two cpp files. I'm having trouble with the square root constructor.

Here is the cpp file.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include <iostream>
#include <string>
#include <cmath>
#include "calculator.h"

using namespace std;

Calculator::Calculator()
{
	//Display and Memory is set at 0.0
	dv = 0.0;
	mv = 0.0;
}
void Calculator::clear()
{
	//Display is set to 0
	this -> dv == 0;
}
void Calculator::add(float addend)
{
	this -> add;
}
void Calculator::subtract(float subtrahend)
{
	this -> subtract;
}
void Calculator::multiply(float multiplier)
{
	this -> multiply;
}
void Calculator::divide(float divisor)
{
	this -> divide;
}
void Calculator::plusminus()
{
	//Switching dv to its inverse
	this -> dv = -dv;
}
void Calculator::squareroot()
{
	if (dv < 0)
	{
		this -> error == true;
	}
	else
	{
		this -> squareroot(dv);
	}

}
void Calculator::m_plus()
{
	this -> mv += dv;
}
void Calculator::m_minus()
{
	this -> mv -= dv;
}
void Calculator::m_recall()
{
	this -> dv == mv;
}
void Calculator::m_clear()
{
	this -> mv == 0;
}


Here is the header file he provided for us that we shouldn't modify.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <string>

using namespace std;

class Calculator
{
public:
	Calculator();

	// Modifies the current "display" value of the calculator
	// in common ways (see assertions in homework3.cpp for examples)
	void clear(); // Resets the display value to 0.0
	void add(float addend);
	void subtract(float subtrahend);
	void multiply(float multiplier);
	void divide(float divisor);
	void plusminus();

	// If display value is negative and squareroot() is called,
	// all other methods do nothing and display() returns "ERROR" until clear() is called. 
	void squareroot();

	// Modifies the calculator's "memory" value.
	// The memory value is a second value, in addition to the display value.
	void m_plus(); // Adds the display value to the memory value 
	void m_minus(); // Subtracts the display value from the memory value
	void m_recall(); // Sets the display value to the memory value
	void m_clear(); // Sets the memory value to 0.

					// Returns the calculator's display value as a string
					// All trailing 0s are removed, i.e. 173.45000 is displayed as 173.45.
					// Hint: use ostringstream in the <sstream> library, see:
					// http://www.cplusplus.com/reference/sstream/ostringstream/str/
	string display();

private:
	float dv; // Stores the current value of the calculator
	float mv; // Stores the current memory value of the calculator
	bool error; // Stores whether an error (caused by squareroot()) has occurred
};


and lastly, the last cpp file.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include <fstream>
#include <iostream>
#include <cassert>
#include "calculator.h"

int main()
{
	Calculator calc;

	assert(calc.display() == "0");
	calc.add(1.0);
	assert(calc.display() == "1");
	calc.add(-5.5);
	assert(calc.display() == "-4.5");
	calc.clear();
	assert(calc.display() == "0");

	calc.clear();
	calc.subtract(7.5);
	assert(calc.display() == "-7.5");
	calc.subtract(10.0);
	assert(calc.display() == "-17.5");

	calc.clear();
	calc.add(1.0);
	calc.multiply(6.0);
	assert(calc.display() == "6");
	calc.multiply(2.5);
	assert(calc.display() == "15");

	calc.clear();
	calc.add(10.0);
	calc.divide(2.0);
	assert(calc.display() == "5");
	calc.divide(0.5);
	assert(calc.display() == "10");

	calc.clear();
	calc.add(-9.0);
	calc.plusminus();
	assert(calc.display() == "9");
	calc.plusminus();
	assert(calc.display() == "-9");

	calc.clear();
	calc.squareroot();
	assert(calc.display() == "0");

	calc.clear();
	calc.add(9.0);
	calc.squareroot();
	assert(calc.display() == "3");

	calc.clear();
	calc.add(-1.0);
	calc.squareroot();
	assert(calc.display() == "ERROR");
	calc.clear();
	assert(calc.display() == "0");

	calc.clear();
	calc.m_clear();
	calc.add(3.0);
	calc.m_plus();
	calc.clear();
	calc.m_recall();
	assert(calc.display() == "3");
	// Memory should have value 3.0 now
	calc.clear();
	calc.add(3.0);
	calc.m_plus();
	calc.clear();
	calc.m_recall();
	assert(calc.display() == "6");
	// Memory should have value 6.0 now
	calc.clear();
	calc.add(2.0);
	calc.m_minus();
	calc.clear();
	calc.m_recall();
	assert(calc.display() == "4");
	// Memory should have value 4.0 now
	calc.clear();
	calc.add(1.0);
	calc.m_minus();
	calc.clear();
	calc.m_recall();
	assert(calc.display() == "3");

	cout << "All assertions passed!" << endl;

	return 0;
}
Last edited on
I'm also wondering if I created all constructors correctly... I'm not too sure if I did the constructors below the square root one the right way.
This class has exactly one explicit constructor (ctor). A constructor is a special procedure which returns nothing (not even void!) and runs when a new instance of an object is created. There's also the concept of a destructor, which is bound to similar rules, except that it runs exactly when an existing instance of an object is destroyed.

A constructor is always named exactly the same as the class. In this case, Calculator::Calculator() is Calculator's only explicit constructor; the other functions you have written are simply "member functions".

The constructor's code is fine, but many of the member functions aren't (they do nothing.)
Consider
1
2
3
4
void Calculator::add(float addend)
{
	this -> add;
}

It's reasonable to assume that a calculator's add button adds its operand to whatever value is on the display. this->add; doesn't do that. Add what? Add how?

Add the argument to the calculator's display value, and set that to the display value. Which calculator? this calculator.
1
2
3
4
void Calculator::add(float addend) 
{
        this->dv += addend;
}


In Calculator::squareroot(), there's the same problem, but it also indicates that if there is an error, no buttons except "clear" respond, and the calculator will display "ERROR" until the error state is cleared.

You can do that by checking the member variable "error" in every place it matters with an if statement, only doing the calculation if the calculator's not error'd out.

Unfortunately that means that you have to copy the same error-checking code 9 times, which is terrible because it's slow and also extremely error-prone (what if you have to change it later, but miss a spot?). If you're not free to modify the class, however, you're stuck doing that.
Topic archived. No new replies allowed.