I need to 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"

the expected output is:

parameterized constructor called

parameterized constructor called

parameterized constructor called

parameterized constructor called

Overloaded Resistor + Series Resistance Operator Called

parameterized constructor called

Overloaded Resistor Assignment = Operator Called

destructor call for SeriesResult

Current Object Count is 4

Overloaded Resistor || Parallel Resistance Operator Called

parameterized constructor called

Overloaded Resistor Assignment = Operator Called

destructor call for ParallelResult

Current Object Count is 4


Values for SeriesResult are:
Resistor Nominal Value = 3000.00
ohmsResistorTolerance = 20.00%
Mininimum Resistance = 2400.00 ohms
Maximum Resistance = 3600.00 ohms


Values for ParallelResult are:
Resistor Nominal Value = 666.67
ohmsResistorTolerance = 20.00%
Mininimum Resistance = 533.33 ohms
Maximum Resistance = 800.00 ohms

destructor call for ParallelResult

Current Object Count is 3

destructor call for SeriesResult

Current Object Count is 2

destructor call for Res2

Current Object Count is 1

destructor call for Res1

Current Object Count is 0

Attachments
#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;	
}; 
int ResistorClass::m_istResCounter = 0; 



const ResistorClass& ResistorClass::operator =(const ResistorClass& ResistorObj){
	if (this !=&ResistorObj){

				
                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];
                m_sptrResName = new char [50];
                strcpy_s(m_sptrResName, 50, ResistorObj.m_sptrResName);
        }
	cout << "\nOverloaded Resistor Assignment = Operator Called\n";
    return *this;	
} 

ResistorClass ResistorClass::operator +(const ResistorClass& ResistorObj) const{ 
	
	double NominalValue = ResistorObj.m_dptrRes[NOMINAL] + m_dptrRes[NOMINAL];
	
	double Tolerance = 0;
	if (ResistorObj.m_dptrRes[TOLERANCE] > m_dptrRes[TOLERANCE]){
		Tolerance = ResistorObj.m_dptrRes[TOLERANCE];
	}
	else {
		Tolerance = m_dptrRes[TOLERANCE];
	};
	
	cout << "\nOverloaded Resistor + Series Resistance Operator Called\n";
 	
 	return ResistorClass("SeriesResult", NominalValue, Tolerance); 
}

ResistorClass ResistorClass::operator ||(const ResistorClass& ResistorObj) const{ 
	
	double NominalValue = (ResistorObj.m_dptrRes[NOMINAL] * m_dptrRes[NOMINAL]) / (ResistorObj.m_dptrRes[NOMINAL] + m_dptrRes[NOMINAL]);
	
	double Tolerance = 0;
	if (ResistorObj.m_dptrRes[TOLERANCE] > m_dptrRes[TOLERANCE]){
		Tolerance = ResistorObj.m_dptrRes[TOLERANCE];
	}
	else {
		Tolerance = m_dptrRes[TOLERANCE];
	};
	
	cout << "\nOverloaded Resistor || Parallel Resistance Operator Called\n";
 	
 	return ResistorClass("ParallelResult", NominalValue, Tolerance); 
		 
}



ResistorClass::ResistorClass(char Name[], double nominalResistance, double Tolerance){
   
	m_sptrResName = new char [50];
	strcpy_s(m_sptrResName, 50, Name);
	m_dptrRes = new double[4];
	m_dptrRes[NOMINAL] = nominalResistance;
	m_dptrRes[TOLERANCE] = Tolerance;

	
    m_dptrRes[MIN] = m_dptrRes[NOMINAL] - (m_dptrRes[NOMINAL] * m_dptrRes[TOLERANCE]);
	m_dptrRes[MAX] = m_dptrRes[NOMINAL] + (m_dptrRes[NOMINAL] * m_dptrRes[TOLERANCE]);
	
	m_istResCounter++;
	cout << "\nparameterized constructor called\n";
}



void ResistorClass::DisplayResistor (){

	cout << setprecision(2); 
	cout << fixed << showpoint;
	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" ;

}

ResistorClass::~ResistorClass(){
	
	m_istResCounter--;
	cout << "\ndestructor call for " << m_sptrResName << endl;
	cout << "\nCurrent Object Count is " << m_istResCounter << endl;
	
	delete m_dptrRes;
	delete m_sptrResName;
}

int main(){
 	
	ResistorClass oResOne("Res1", 1000, .10);
	ResistorClass oResTwo("Res2", 2000, .20);
	ResistorClass oResSeries("", 0, 0);
	ResistorClass oResParall("", 0, 0);
	
	
	oResSeries = oResOne + oResTwo;
	oResParall = oResOne || oResTwo;

	
	oResSeries.DisplayResistor();
	oResParall.DisplayResistor(); 
	
system ("pause");
}

Is there a question in there somewhere? What are you having trouble with?

I'm afraid I just can't figure out how to provide a valuable response to your query.

Edited 5 Years Ago by Fbody: n/a

Objective: Create a C++ Class that includes overloaded operators.

Recall from last week's lab that we designed and implemented two resistor classes. We will use our lab work from last week and enhance the base resistor class by overloading several operators. You can use the lab solution from last week as your starting point. There is no need for the fancyResistorClass so you can eliminate all of the code related to this class.

Here is what you need to do:

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"

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"

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"

this is what i have

#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;	
}; 
int ResistorClass::m_istResCounter = 0; 



const ResistorClass& ResistorClass::operator =(const ResistorClass& ResistorObj){
	if (this !=&ResistorObj){

				
                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];
                m_sptrResName = new char [50];
                strcpy_s(m_sptrResName, 50, ResistorObj.m_sptrResName);
        }
	cout << "\nOverloaded Resistor Assignment = Operator Called\n";
    return *this;	
} 

ResistorClass ResistorClass::operator +(const ResistorClass& ResistorObj) const{ 
	
	double NominalValue = ResistorObj.m_dptrRes[NOMINAL] + m_dptrRes[NOMINAL];
	
	double Tolerance = 0;
	if (ResistorObj.m_dptrRes[TOLERANCE] > m_dptrRes[TOLERANCE]){
		Tolerance = ResistorObj.m_dptrRes[TOLERANCE];
	}
	else {
		Tolerance = m_dptrRes[TOLERANCE];
	};
	
	cout << "\nOverloaded Resistor + Series Resistance Operator Called\n";
 	
 	return ResistorClass("SeriesResult", NominalValue, Tolerance); 
}

ResistorClass ResistorClass::operator ||(const ResistorClass& ResistorObj) const{ 
	
	double NominalValue = (ResistorObj.m_dptrRes[NOMINAL] * m_dptrRes[NOMINAL]) / (ResistorObj.m_dptrRes[NOMINAL] + m_dptrRes[NOMINAL]);
	
	double Tolerance = 0;
	if (ResistorObj.m_dptrRes[TOLERANCE] > m_dptrRes[TOLERANCE]){
		Tolerance = ResistorObj.m_dptrRes[TOLERANCE];
	}
	else {
		Tolerance = m_dptrRes[TOLERANCE];
	};
	
	cout << "\nOverloaded Resistor || Parallel Resistance Operator Called\n";
 	
 	return ResistorClass("ParallelResult", NominalValue, Tolerance); 
		 
}



ResistorClass::ResistorClass(char Name[], double nominalResistance, double Tolerance){
   
	m_sptrResName = new char [50];
	strcpy_s(m_sptrResName, 50, Name);
	m_dptrRes = new double[4];
	m_dptrRes[NOMINAL] = nominalResistance;
	m_dptrRes[TOLERANCE] = Tolerance;

	
    m_dptrRes[MIN] = m_dptrRes[NOMINAL] - (m_dptrRes[NOMINAL] * m_dptrRes[TOLERANCE]);
	m_dptrRes[MAX] = m_dptrRes[NOMINAL] + (m_dptrRes[NOMINAL] * m_dptrRes[TOLERANCE]);
	
	m_istResCounter++;
	cout << "\nparameterized constructor called\n";
}



void ResistorClass::DisplayResistor (){

	cout << setprecision(2); 
	cout << fixed << showpoint;
	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" ;

}

ResistorClass::~ResistorClass(){
	
	m_istResCounter--;
	cout << "\ndestructor call for " << m_sptrResName << endl;
	cout << "\nCurrent Object Count is " << m_istResCounter << endl;
	
	delete m_dptrRes;
	delete m_sptrResName;
}

int main(){
 	
	ResistorClass oResOne("Res1", 1000, .10);
	ResistorClass oResTwo("Res2", 2000, .20);
	ResistorClass oResSeries("", 0, 0);
	ResistorClass oResParall("", 0, 0);
	
	
	oResSeries = oResOne + oResTwo;
	oResParall = oResOne || oResTwo;

	
	oResSeries.DisplayResistor();
	oResParall.DisplayResistor(); 
	
system ("pause");
}

i am missing

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"

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"

This article has been dead for over six months. Start a new discussion instead.