I am new to C++ programming. I am using Visual Studio Code, my code:
#include <iostream>
using namespace std;
class Calculator;
class Complex
{
float a, b;
string Operation;
string name;
friend class Calculator;
public:
Complex(float, float, string, string);
Complex(float);
Complex();
Complex(Complex &, string);
void PrintComp(string op = "None")
{
if (op != "None" && name != "None")
{
cout << "\nBy " << op << " of z1 and z2:\n"
<< name << " = " << a << " + " << b << "i\n";
}
else if (name != "None")
{
cout << name << " = " << a << " + " << b << "i\n";
}
else
{
cout << a << " + " << b << "i\n";
}
}
};
Complex ::Complex(float x, float y, string givnname = "None", string operation = "None")
{
a = x;
b = y;
name = givnname;
PrintComp(operation);
}
Complex ::Complex(float x)
{
a = x;
b = 0;
}
Complex ::Complex()
{
a = 0;
b = 0;
}
Complex::Complex(Complex &obj, string givnname="None")
{
a = obj.a;
b = obj.b;
name = givnname;
cout << "Copy Cons called!"<< endl;
PrintComp();
}
class Calculator
{
public:
float SumRealComp(Complex const&, Complex const&);
float SumImgComp(Complex const&, Complex const&);
};
float Calculator ::SumRealComp(Complex const &obj1, Complex const & obj2)
{
return (obj1.a + obj2.a);
}
float Calculator ::SumImgComp(Complex const & obj1, Complex const & obj2)
{
return (obj1.b + obj2.b);
}
int main()
{
Complex z1(3, 5, "z1"), z2(4, 4, "z2");
Calculator calc;
Complex z3(calc.SumRealComp(z1, z2), calc.SumImgComp(z1, z2), "z3", "Sum");
Complex z4(z1, "z4");
return 0;
}
According to above code, I am using copy Constructor at the last, but
Copy Constructor is called at formation of every object. Even when args of both are different. Why it is so?
I am learning from here.
Every suggestions in my code are appreciated.
Thanks!