Basically, here is what the program is supposed to do:
1. Create a ResistorClass overloaded assignment (=) operator function which shall do the following:
* Overload the assignment operator ( = ) to assign all m_dptrRes data values of an object of class ResistorClass to another object of class ResistorClass. Remember to use deep copying.
* Be sure to copy the max and min resistance values.
* Copy m_sptrResName using the strcpy_s function (see lab 6 for details on the strcopy_s function).
* Print the message "Overloaded Resistor Assignment = Operator Called"
2. Create a ResistorClass overloaded addition operator function which shall do the following:
* Overload the addition operator ( + ) to perform series resistance addition of the nominal values ( m_dptrRes[0]) of two Resistor Class objects. In other words, the nominal values of the two resistor objects will be added together.
* Compare the tolerance values ( m_dptrRes[1]) of two Resistor Class objects and set the tolerance equal to the larger of the two tolerance values.
* Print the message "Overloaded Resistor Series Resistance Operator Called"
3. Create a ResistorClass overloaded logical OR operator function which shall do the following:
* Overload the logical OR operator ( || ) to calculate the equivalent parallel resistance of the nominal values ( m_dptrRes[0]) of two Resistor Class objects.
The formula for calculating parallel resistance is:
NominalValue1 * NominalValue2 / (NominalValue1 + NominalValue2)
* Compare the tolerance values of the two objects and set the tolerance value ( m_dptrRes[1]) equal to the larger of the two tolerance values.
* Print the message "Overloaded Resistor Parallel Resistance Operator Called"
Test the ResistorClass overloaded operators by performing the following tasks:
1. Instantiate four ResistorClass objects: Res1, Res2, ResSeries and ResParallel
2. Instantiate all ResistorClass objects using the parameterized constructor.
3. Calculate ResSeries = Res1 + Res2
4. Calculate ResParallel = Res1 || Res2
5. Display the values of the ResSeries and ResParallel objects
6. End program
So I have it all in however I know that I have done something wrong. The program just crashes every time.
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
class ResistorClass {
public:
void DisplayResistor(void);
void EnterResistance(void);
ResistorClass();
ResistorClass(char Name[], double nominalResistance, double Tolerance);
~ResistorClass( );
static int m_istResCounter;
const ResistorClass& ResistorClass::operator =(const ResistorClass&);
ResistorClass ResistorClass::operator +(const ResistorClass&) const ;
ResistorClass ResistorClass::operator ||(const ResistorClass&) const;
enum resistorValues {NOMINAL, TOLERANCE, MAX, MIN};
protected:
double *m_dptrRes;
char *m_sptrResName;
}; //end resistor class
int ResistorClass::m_istResCounter = 0; //intialize static counter
int main(){
//Create a resistor using the parameterized constructor
ResistorClass oResOne("Res1", 1000, .10);
ResistorClass oResTwo("Res2", 2000, .20);
ResistorClass oResSeries("", 0, 0);
ResistorClass oResParall("", 0, 0);
//perform the calculations
oResSeries = oResOne + oResTwo;
oResParall = oResOne || oResTwo;
//display the calculations
oResSeries.DisplayResistor();
oResParall.DisplayResistor();
} //end main
/***********************************************************
Resistor Class Implementation
************************************************************/
const ResistorClass& ResistorClass::operator =(const ResistorClass& ResistorObj){
if (this !=&ResistorObj){
char Name[50];
m_sptrResName = new char [50];
m_dptrRes = new double[4];
this-> m_dptrRes[NOMINAL] = ResistorObj.m_dptrRes[NOMINAL];
this-> m_dptrRes[TOLERANCE] = ResistorObj.m_dptrRes[TOLERANCE];
this-> m_dptrRes[MAX] = ResistorObj.m_dptrRes[MAX];
this-> m_dptrRes[MIN] = ResistorObj.m_dptrRes[MIN];
strcpy_s(m_sptrResName, 50, Name);
}
cout << "Overloaded Resistor Assignment = Operator Called\n";
return *this;
} //end assignment operator
ResistorClass ResistorClass::operator +(const ResistorClass& resistorObj) const{
//overload the addition operator
//calculate Nominal Value
double NominalValue = resistorObj.m_dptrRes[NOMINAL] + m_dptrRes[NOMINAL];
//Calculate tolerance
double Tolerance = 0;
if (resistorObj.m_dptrRes[TOLERANCE] > m_dptrRes[TOLERANCE]){
Tolerance = resistorObj.m_dptrRes[TOLERANCE];
}
else {
Tolerance = m_dptrRes[TOLERANCE];
};
//display a message letting us know the function was executed
cout << "Overloaded Resistor Series Resistance Operator Called\n";
//return the new object
return ResistorClass("SeriesResult", NominalValue, Tolerance);
}//end overload addition
ResistorClass ResistorClass::operator ||(const ResistorClass& resistorObj) const{
//overload the OR operator
//calculate Nominal Value
double NominalValue = (resistorObj.m_dptrRes[NOMINAL] * m_dptrRes[NOMINAL]) / (resistorObj.m_dptrRes[NOMINAL] + m_dptrRes[NOMINAL]);
//Calculate tolerance
double Tolerance = 0;
if (resistorObj.m_dptrRes[TOLERANCE] > m_dptrRes[TOLERANCE]){
Tolerance = resistorObj.m_dptrRes[TOLERANCE];
}
else {
Tolerance = m_dptrRes[TOLERANCE];
};
//display a message letting us know the function was executed
cout << "Overloaded Resistor Parallel Resistance Operator Called\n";
//return the new object
return ResistorClass("ParallelResult", NominalValue, Tolerance);
}//end overload OR operator
ResistorClass::ResistorClass(){
//default constructor
//create array for resistor values
m_dptrRes = new double[4];
//default values
m_dptrRes[NOMINAL] = 1000;
m_dptrRes[TOLERANCE] = .10;
//calculate min and max values
m_dptrRes[MIN] = m_dptrRes[NOMINAL] - (m_dptrRes[NOMINAL] * m_dptrRes[TOLERANCE]);
m_dptrRes[MAX] = m_dptrRes[NOMINAL] + (m_dptrRes[NOMINAL] * m_dptrRes[TOLERANCE]);
//create a new character array
m_sptrResName = new char [50];
//input resistor's name
cout << "\n\n";
cout << "Enter Resistor Name (default): ";
cin.getline(m_sptrResName,50);
//Increment the object count
m_istResCounter++;
cout << "\ndefault constructor called\n";
}//end default constructor
ResistorClass::ResistorClass(char Name[], double nominalResistance, double Tolerance){
//Parameterized constructor
//store the resistor name
m_sptrResName = new char [50];
strcpy_s(m_sptrResName, 50, Name);
//store the resistor values
m_dptrRes = new double[4];
m_dptrRes[NOMINAL] = nominalResistance;
m_dptrRes[TOLERANCE] = Tolerance;
//Calculate min and max
m_dptrRes[MIN] = m_dptrRes[NOMINAL] - (m_dptrRes[NOMINAL] * m_dptrRes[TOLERANCE]);
m_dptrRes[MAX] = m_dptrRes[NOMINAL] + (m_dptrRes[NOMINAL] * m_dptrRes[TOLERANCE]);
//Increment the object count
m_istResCounter++;
cout << "\nparameterized constructor called\n";
}//end parameterized constructor
ResistorClass::~ResistorClass(){
//destructor
//decrement the object count
m_istResCounter--;
cout << "\ndestructor call for " << m_sptrResName << endl;
cout << "\nCurrent Object Count is " << m_istResCounter << endl;
//clean up the memory
delete m_dptrRes;
delete m_sptrResName;
}//end destructor
void ResistorClass::DisplayResistor (){
//Displays all Resistor object data members
//set the output parameters
cout << setprecision(2); //see Ch3 page 136 in text book
cout << fixed << showpoint;
//display the output
cout << "\n\n";
cout << "Values for " << m_sptrResName << " are:\n" ;
cout << "Resistor Nominal Value = " << setw(10) << m_dptrRes[NOMINAL] << "\n";
cout << "ohmsResistorTolerance = " << setw(10) << m_dptrRes[TOLERANCE] * 100 << "% \n";
cout << "Mininimum Resistance = " << setw(10) << m_dptrRes[MIN] << " ohms\n";
cout << "Maximum Resistance = " << setw(10) << m_dptrRes[MAX] << " ohms\n" ;
}//end displayResistor
void ResistorClass::EnterResistance (){
//Displays the current value for m_dResValue and prompt the user to enter a new value.
bool hasError = false; //default error condition
//continue prompting user for input until values are valid
do {
//input data
cout << "\n\n";
cout << "Enter the Nominal Resistor Value :";
cin >> m_dptrRes[NOMINAL];
cout << "Enter the Resistor Tolerance Value :";
cin >> m_dptrRes[TOLERANCE];
//input error checking
hasError = false;
if (m_dptrRes[NOMINAL] < 0 || m_dptrRes[NOMINAL] > 10000) {
cout << "\n\n";
cout << "Error: Resistor Value" << endl << "Entry must be between 0 and 10,000\n";
m_dptrRes[NOMINAL] = -1; //denotes error value
hasError = true;
}
if (m_dptrRes[TOLERANCE] > 1) {
cout << "\n\n";
cout << "Error: Tolerance Value" << endl << "Entry must be between a decimal value <= 1\n";
m_dptrRes[TOLERANCE] = -1; //denotes error value
hasError = true;
}
} while (hasError);
//Calculate new Values
m_dptrRes[MIN] = m_dptrRes[NOMINAL] - (m_dptrRes[NOMINAL] * m_dptrRes[TOLERANCE]);
m_dptrRes[MAX] = m_dptrRes[NOMINAL] + (m_dptrRes[NOMINAL] * m_dptrRes[TOLERANCE]);
//display the new values
DisplayResistor();
}//end EnterResistance