#include <iostream>
usingnamespace 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.
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..
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: