Can somebody help me with my C++ Calculator?

Started by
6 comments, last by Ekim_Gram 20 years, 9 months ago
Ok, since I haven''t made anything since my function based calculator, I''m not going to make an OO calculator but I''m having a little bit of trouble. Here''s the class declaration I''m going to be using:

class Calculate
{
public:
	Calculate();
	~Calculate();
	void Add();
	void Subtract();
	void Divide();
	void Multiply();
	void Modulo();

private:
	int firstNum;
	int secondNum;

};
In the conctructor Calculate() I want to declare two pointers: one to firstNum, and another to secondNum to use with the five mathmatical object functions but I don''t remember how to do that correctly. Then in the destructor I want to delete those pointers and null them; I know how to do that. So right now I''m just having trouble foring the constructor with the pointers and such. Thanks in advance for the help! There''s no town drunk here, we all take turns.
Advertisement
I''m not sure I see the reason for having pointers to simple int datatypes but perhaps this is what you seek?

int *pfirstNum, *psecondNum;//probably add these as members to your classCalculate::Calculate(){    pfirstNum=&firstNum    psecondNum=&secondNum    //foo foo! }//your constructor Calculate::~Calculate(){    pfirstNum=NULL;    psecondNum=NULL;    //more cleanup crap goes here} 





You fight like a dairy farmer.

--{You fight like a dairy farmer!}

GreatWolf,
Your example is no good, i think he wants to bable to assign data when he constructs an object of that class. Not add 2 pointers that do nothing.

Ekim_Gram,
It is a bit extreeme to use pointers in your simple example, but you are in the learning phase so I guess practicing using pointers is ok (although you may become confused by them even more by using them when they probably shouldn't be used). Anyways, by you example i assume you want to contrust the class using the constuctor like...

calc = new Calculate(my1stPointer, my2ndPointer);

Therefore you need to add a non-default constructor like below...

(using the code you posted...)
class Calculate{public:   Calculate();   Calculate(int* first, int* second);  //*NEW*   ~Calculate(); private:   int firstNum;   int secondNum;};Calculate::Calculate(){   //no params, probably should set the Nums to 0}Calculate::Calculate(int* first, int* second){   //set the value of the private ints = to the params   firstNum = first;   secondNum = second;}  


Note that the paramaters use in the non-default constructor are pointers, however the private numbers in the class are NOT pointers. I dont know if you ment to do this or not.

[edited by - IllMind on July 8, 2003 4:29:36 PM]
Thanks IllMind that is pretty much what I wanted to do. As for using pointers, I find them more efficient then regular ints because you can delete them to free up space when you don''t need them anymore.


There''s no town drunk here, we all take turns.
But using pointers brings some other difficulty''s with them..

Say you declare one with new but forget to delete it and voila your first memory leek.. it could be a problem in a bigger programm to keep track of all the pointers you''ve got and to delete them..

And you should do some error checking on them.. wat if one of them becomess NULL and you assign some data to it..

I don''t know if it''s ''illigal'' to use pointers for simple opperations but it''s more easy to just use the regular int(or any other type)..

Maybe someone with more experience could give his advice but this is what I think of it
quote:Original post by Ekim_Gram
Thanks IllMind that is pretty much what I wanted to do. As for using pointers, I find them more efficient then regular ints because you can delete them to free up space when you don''t need them anymore.


There''s no town drunk here, we all take turns.


There is a misunderstanding about why you would use pointers to primitives here. The main reason to use a pointer to a primitive is to pass by reference instead of pass by value.

In your example, I assume you would have 2 int*''s as class member variables. This is no different, storage wise, then having 2 ints as class member variables.

assuming 4-byte ints, using:
new Calculator(x, y);
allocates 8 bytes on the heap, whether you use int''s or int*''s.
There is no storage benefit, and plenty of opportunities for screwing up your data...
Ok, that just made me forget using pointers in the Constructor cause for some reason that cleared it all up but that doesn''t mean I shouldn''t use a pointer to an object like so:

int main(){  Calculate *calculater = new Calculate;  // stuff  return 0;}


I guess what I meant was that but I kinda confused myself by making the constructor use pointers and such.


There''s no town drunk here, we all take turns.
Might I suggest another challenge? Instead of just making a simple calculator that works with ints how about you try and make a calculator that will work with numbers having large amounts of digits? In other words, making a calculator that'll work with numbers that isn't limited to the amount of space allocated to that numerical datatype. For example, if the user enters 1,000,000,000,000 (1 trillion)+ 21,051,081,268,512 your calculator can do it.

Edit: btw, this happens to be the case study problem that was used in my AP C++ class all the way back in 1999 so it might still be on the ap board's site for download. After you're done with your program you can compare your implementation with theirs. Or if you're just plain stuck you can look at their implementation and see how they did it.




You fight like a dairy farmer.

[edited by - Greatwolf on July 8, 2003 6:18:57 PM]

--{You fight like a dairy farmer!}

This topic is closed to new replies.

Advertisement