The header file
# pragma once
The class HZ
{
Public:
HZ (double lengthValue, double widthValue, double heightValue);
HZ (const HZ& Obj);
Friend HZ operator - (HZ& C1, HZ& C2);
Void print_1 ();
HZ& Operator + (HZ& C1);
Protected:
Private:
Public:
Double length;
Double width;
double height;
};
HZ operator - (HZ& C1, HZ& C2);
Class implements. CPP
# include "HZ. H"
# include & lt; iostream>
using namespace std;
Using STD: : cout;
Using STD: : endl;
HZ: : HZ (double lengthValue, double widthValue, double heightValue)
{
This - & gt; Length=lengthValue;
This - & gt; Width=widthValue;
This - & gt; Height=heightValue;
}
HZ: : HZ (const HZ& Obj)
{
This - & gt; Length=obj. Length;
This - & gt; Width=obj. Width;
This - & gt; Height=obj. Height;//operator overloading plus obj. Height output is stil
}
Void HZ: : print_1 ()
{
Cout & lt; <"Length:" & lt;}
HZ operator - (HZ& C1, HZ& C2)
{
HZ TMP (c1) length - c2) length, c1. The width - c2. Width, c1. The height - c2. Height);
Return the TMP;
}
HZ& HZ: : operator + (HZ& C1)
{
HZ TMP (this - & gt; Length + c1. Length, this - & gt; Width + c1. Width, this - & gt; Height + c1. Height);
Return the TMP;
}
The main function. The CPP
int main ()
{
HZ t1 (1.0, 3.1, 4.0);
HZ t3 (9.0, 2.0, 5.0);
HZ t4=t1 + t3;
T4. Print_1 ();
HZ t5=t1 - t3;
T5. Print_1 ();
return 0;
}
CodePudding user response:
Many wrongHZ& HZ: : operator + (HZ& C1)
{
HZ TMP (this - & gt; Length + c1. Length, this - & gt; Width + c1. Width, this - & gt; Height + c1. Height);
Return the TMP;//returns the local variable reference here, changed the function to HZ.
}
CodePudding user response:
VS2015 pro measurement seems to be no problem:class HZ
{
Public:
HZ (double lengthValue, double widthValue, double heightValue);
HZ (const HZ& Obj);
Friend HZ operator - (HZ& C1, HZ& C2);
Void print_1 ();
HZ& Operator + (HZ& C1);
Protected:
Private:
Public:
Double length;
Double width;
double height;
};
HZ operator - (HZ& C1, HZ& C2);
# include & lt; iostream>
using namespace std;
Using STD: : cout;
Using STD: : endl;
HZ: : HZ (double lengthValue, double widthValue, double heightValue)
{
This - & gt; Length=lengthValue;
This - & gt; Width=widthValue;
This - & gt; Height=heightValue;
}
HZ: : HZ (const HZ& Obj)
{
This - & gt; Length=obj. Length;
This - & gt; Width=obj. Width;
This - & gt; Height=obj. Height;//operator overloading plus obj. Height output is stil
}
Void HZ: : print_1 ()
{
Cout & lt; <"Length:" & lt;}
HZ operator - (HZ& C1, HZ& C2)
{
HZ TMP (c1) length - c2) length, c1. The width - c2. Width, c1. The height - c2. Height);
Return the TMP;
}
HZ& HZ: : operator + (HZ& C1)
{
HZ TMP (this - & gt; Length + c1. Length, this - & gt; Width + c1. Width, this - & gt; Height + c1. Height);
Return the TMP;
}
Int main ()
{
HZ t1 (1.0, 3.1, 4.0);
HZ t3 (9.0, 2.0, 5.0);
HZ t4=t1 + t3;
T4. Print_1 ();
HZ t5=t1 - t3;
T5. Print_1 ();
return 0;
}
//length, width: 5.1 height: 9
//length: - 8 width: height: 1.1-1
//
CodePudding user response:
HZ& Operator + (HZ& C1); Don't return references to come back to reference +=CodePudding user response:
Reference:# include & lt; iostream>
//# pragma once
The class HZ
{
Public:
HZ (double lengthValue, double widthValue, double heightValue);
HZ (const HZ& Obj);
Friend HZ operator - (HZ& C1, HZ& C2);
Void print_1 ();
HZ operator + (HZ& C1);//HZ& Operator + (HZ& C1);
Protected:
Private:
Public:
Double length;
Double width;
double height;
};
HZ operator - (HZ& C1, HZ& C2);
//# include "HZ. H"
//# include & lt; iostream>
using namespace std;
Using STD: : cout;
Using STD: : endl;
HZ: : HZ (double lengthValue, double widthValue, double heightValue)
{
This - & gt; Length=lengthValue;
This - & gt; Width=widthValue;
This - & gt; nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull