A
August1
Below are the declaration for an overloaded assignment operator in an
interface file in addition to the implementation file definition of
the function. What would be an appropriate if condition to ensure
that a client of the program cannot assign an object of the class to
itself. The if() condition would be placed within the function
definition to compare the operand in the function parameter is not the
same as the temporary object being created.
//MutualFund.h - interface file
//overloaded assignment operator declaration
MutualFund& operator = (const MutualFund&);
//MutualFund.cpp - implementation file
//definition
MutualFund& MutualFund:perator = (const MutualFund& operand)
{
szFundName = new char[25];
strcpy(szFundName,operand.szFundName);
iFundShares = operand.iFundShares;
dShareValue = operand.dShareValue;
dPortfolioValue = operand.dPortfolioValue;
return *this;
}
//Fund.cpp - client file
MutualFund fund1;
/*Rather than declaring object of the class and assigning the values
of all data members to the object's data members as desired with a
foloowing statement,
MutualFund fund2;
fund2 = fund1;
the user does this,
fund1 = fund1;
this is why there should be a condition to test that an object is not
assigned to itself*/
Thanks,
Anthony
interface file in addition to the implementation file definition of
the function. What would be an appropriate if condition to ensure
that a client of the program cannot assign an object of the class to
itself. The if() condition would be placed within the function
definition to compare the operand in the function parameter is not the
same as the temporary object being created.
//MutualFund.h - interface file
//overloaded assignment operator declaration
MutualFund& operator = (const MutualFund&);
//MutualFund.cpp - implementation file
//definition
MutualFund& MutualFund:perator = (const MutualFund& operand)
{
szFundName = new char[25];
strcpy(szFundName,operand.szFundName);
iFundShares = operand.iFundShares;
dShareValue = operand.dShareValue;
dPortfolioValue = operand.dPortfolioValue;
return *this;
}
//Fund.cpp - client file
MutualFund fund1;
/*Rather than declaring object of the class and assigning the values
of all data members to the object's data members as desired with a
foloowing statement,
MutualFund fund2;
fund2 = fund1;
the user does this,
fund1 = fund1;
this is why there should be a condition to test that an object is not
assigned to itself*/
Thanks,
Anthony