constructor

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 <iostream>
using namespace std;
class Calculator // practice constructor
{
    int number1;
    int number2;
    int number3;
public:
    Calculator(int x,int y)
    {
        number1=x;
        number2=y;
    }
    Calculator(int x,int y,int z)
    {
        number1=x;
        number2=y;
        number3=z;
    }
    int multiply(){return number1*number2;}
    int addition(){return number1+number2;}
    int division(){return number1/number2;}
    int subtraction(){return number1-number2;}
    int multiply3(){return number1*number2*number3;}
    int addition3(){return number1+number2+number3;}
    int division3(){return number1/number2/number3;}
    int subtraction3(){return number1-number2-number3;}
};
int main()
{
    Calculator newCalculator(1,2);
    Calculator Calculators(1,2,3);
    cout<<newCalculator.addition()<<endl;
    cout<<newCalculator.division()<<endl;
    cout<<newCalculator.subtraction()<<endl;
    cout<<newCalculator.multiply()<<endl;
    cout<<newCalculator.addition3()<<endl;
    cout<<newCalculator.subtraction3()<<endl;
    cout<<newCalculator.division3()<<endl;
    cout<<newCalculator.multiply3()<<endl;
}

this is the output

3
0
-1
2
199994349
-199930304095
0
-3140539484

i dont know what problem i will ask to you in my code.
i dont understand "when constructor is created it will be called immediately,after the allocation"
and can you fix my code and explain it to me. im just practicing constructor and
it says you can create constructor as many as you want.

your zero result is due to integer division. http://mathworld.wolfram.com/IntegerDivision.html

your garbage results (e.g. 199994349 & -199930304095) is because you are using number3 in your class without it being initialised.

note, you never use the object you create on line 32.
Last edited on
your zero result is due to integer division. http://mathworld.wolfram.com/IntegerDivision.html

i can deal with math. i was just kinda too lazy to write control statement for it


your garbage results (e.g. 199994349 & -199930304095) is because you are using number3 in your class without it being initialised.

what do you mean? i declared number3 as same as number2 and number1

note, you never use the object you create on line 32.

can you explain it to me? i think this is my unknown problem

what do you mean? i declared number3 as same as number2 and number1
1
2
3
4
5
6
7
8
9
    int number1;
    int number2;
    int number3;
public:
    Calculator(int x,int y)
    {
        number1=x;
        number2=y;
    }//What would number3 equals to? 


can you explain it to me? i think this is my unknown problem
1
2
3
    Calculator newCalculator(1,2); //Create newCalculator object
    Calculator Calculators(1,2,3);  //Create Calculators objecct
    cout<<newCalculator.addition()<<endl; //Make some operations with newCalculator 
Calculators are never used after creation (object name is never rencountered again)
@MiiniPaa
//What would number3 equals to?

you dont understand my code, do you?
i didn't initialize it becuase that constructor will be used in these
1
2
3
4
  cout<<newCalculator.addition()<<endl;
    cout<<newCalculator.division()<<endl;
    cout<<newCalculator.subtraction()<<endl;
    cout<<newCalculator.multiply()<<endl;

and the second constructor which i initialized the number 3 and will be used on
1
2
3
4
   cout<<newCalculator.addition3()<<endl;
    cout<<newCalculator.subtraction3()<<endl;
    cout<<newCalculator.division3()<<endl;
    cout<<newCalculator.multiply3()<<endl;

Calculators are never used after creation (object name is never rencountered again)


i see. no one reencountered my calculators object that's why i encountered such ugly output. and because in my code number3 which is still a garbage even i initialized it..

how should i fix it?
Last edited on
you dont understand my code, do you?
i didn't initialize it becuase that constructor will be used in these
1
2
3
4
cout<<newCalculator.addition()<<endl;
cout<<newCalculator.division()<<endl;
cout<<newCalculator.subtraction()<<endl;
cout<<newCalculator.multiply()<<endl;


The constructor has already been called here:
Calculator newCalculator(1,2);
It will not be called again and number3 is still not initialised so you'll still get garbage.

When you create this object:
Calculator newCalculator(1,2);
It calls this constructor that takes 2 parameters:
1
2
3
4
5
Calculator(int x,int y)
    {
        number1=x;
        number2=y;
    }


Which doesn't initialise number3

You then create another object here:
Calculator Calculators(1,2,3);
Which calls your other constructor which takes 3 parameters:
1
2
3
4
5
6
Calculator(int x,int y,int z)
    {
        number1=x;
        number2=y;
        number3=z;
    }

This does initialise number3 but you never use this object in the following code:
1
2
3
4
5
6
7
8
cout<<newCalculator.addition()<<endl;
    cout<<newCalculator.division()<<endl;
    cout<<newCalculator.subtraction()<<endl;
    cout<<newCalculator.multiply()<<endl;
    cout<<newCalculator.addition3()<<endl;
    cout<<newCalculator.subtraction3()<<endl;
    cout<<newCalculator.division3()<<endl;
    cout<<newCalculator.multiply3()<<endl;


you dont understand my code, do you?

It seems you don't understand your own code.

I suggest you read this: http://www.cplusplus.com/doc/tutorial/classes/
and the second constructor which i initialized the number 3 and will be used on
1
2
3
4
5
 cout<<newCalculator.addition3()<<endl;
    cout<<newCalculator.subtraction3()<<endl;
    cout<<newCalculator.division3()<<endl;
    cout<<newCalculator.multiply3()<<endl;


Nope.
Topic archived. No new replies allowed.