Could some please look at my code? Constructor help:

Hello,

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:

BEAM operator + (BEAM);

BEAM BEAM::operator + (BEAM beamlength)
{
double newL;
BEAM newlength;
newL=L+beamlength.L;
newlength.L=newL;
return newlength;}


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;
}
closed account (zb0S216C)
Cirabou wrote:
"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.

I suggest something like:

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
class Beam
{
    public:
        Beam(const unsigned int &InitialLen = 0U);
        Beam(const Beam &Inst);

    private:
        unsigned int Length__;

    public:
        Beam operator + (const Beam &Inst);
        Beam operator + (const unsigned int &NewLength);
};

Beam::Beam(const unsigned int &InitialLen)
    : Length__(InitialLen)
{ }

Beam::Beam(const Beam &Inst)
    : Length__(Beam.Length__)
{ }

Beam Beam::operator + (const Beam &Inst)
{
    return(this->Length__ + Inst.Length__);
}

Beam Beam::operator + (const unsigned int &NewLength)
{
    return(this->Length__ + NewLength);
}

Edit: Jikax: Your code has multiple flaws, as wells as redundant code.

Wazzak
Last edited on
your almost there..

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
using namespace std;

class BEAM
{
public:
  BEAM();
  ~BEAM();

  BEAM operator+ (const double& b2);

  double L; 
};

BEAM::BEAM()
{}

BEAM::~BEAM()
{}

BEAM BEAM::operator+ (const double& b2)
{
  BEAM newBeam;
  newBeam.L = L + b2;
  return newBeam;
}

int main()
{
  BEAM P1,P2;
  double input;
  double Ll = 5;
  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.L;
  return 0;
}


not tested though;)
Last edited on
Jikax: Your code has multiple flaws, as wells as redundant code.


show me...
closed account (zb0S216C)
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.

Wazzak
1) Your constructor fails to initialise the data members (initialise is one of the constructor's jobs).


true, but not really important in this example..

2) Your destructor is redundant. The trivial destructor would suffice in this case


So, a declaration in the class is enough? did not know this...

3) (Splitting hairs here) An unnecessary local object, namely, input.


absolutely true... did copy cirbou's code ;)... not nice...

4) You fail to validate the input. What if the input was a negative number?


true, but not really important in this example..
Last edited on
closed account (zb0S216C)
Jikax wrote:
"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.."

Same as above.

Wazzak
Last edited on
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...
closed account (zb0S216C)
Jikax wrote:
"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.

Wazzak
Last edited on
Topic archived. No new replies allowed.