I am writing a beam analysis program, and I need to use a constructor method defining the + sign as increasing the length L of the beam by the amount passed as the parameter to the method.
For instance, if the object variable is named THE_BEAM, then the statement THE_BEAM + 5; should add 5 inches to the length L of the beam object and recalculate all of the calculated values.
I am having some trouble figuring out how to do this...can anyone give me some pointers? I have included sections of my code below:
main program:
//parameter for original length (also a user-input):
double Ll;
if (menu == 5)
{
BEAM P1,P2;
double input;
cout << "Please enter the length of the section to weld: " << endl;
cin >> input;
P1.L=input;
cout << "The length to weld is: " << P1.L;
P2=P1 + Ll;
cout << P2;
}
"I need to use a constructor method defining the + sign"
The constructor and operator (+) are two different types, though, the commonality between the two is that they are both functions.
In the code you posted, there're no constructor definitions, only invocations. In BEAM::operator +(), why not modify BEAM::L directly? It's not easy to provide an acceptable solution without knowing the access privileges of BEAM::L, and what constructors BEAM has.
1) Your constructor fails to initialise the data members (initialise is one of the constructor's jobs).
2) Your destructor is redundant. The trivial destructor would suffice in this case.
3) (Splitting hairs here) An unnecessary local object, namely, input.
4) You fail to validate the input. What if the input was a negative number?
Also, I recommend that you flush the stream when you've finished writing to it. Flush is expensive in terms of performance.
"true, but not really important in this example.."
Displaying good practices in all of your examples means the reader will be more inclined to adhere to them.
Jikax wrote:
"So, a declaration in the class is enough? did not know this..."
No, there doesn't need to be one at all; there's no need to override it. Note that declaring a constructor and/or destructor overrides the implicit trivial constructor and/or destructor provided by the compiler. You only need to override the implicit trivial destructor if DMA (Dynamically Allocated Memory) is involved.
Besides, a declaration requires a definition if it's used.
Jikax wrote:
"true, but not really important in this example.."
No, there doesn't need to be one at all; there's no need to override it. Note that declaring a constructor and/or destructor overrides the implicit trivial constructor and/or destructor provided by the compiler. You only need to override the implicit trivial destructor if DMA (Dynamically Allocated Memory) is involved.
hmm.. always thought the destructor just needed to be there. Otherwise the memory won't be freed...
"always thought the destructor just needed to be there. Otherwise the memory won't be freed..."
It happens, so don't fret over it :) My rule of thumb is this: If I use new in a class, override the default destructor. Otherwise, there's no need to override the compiler's destructor.