Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

STEP 1: Create a Multifile Project Back to top Create a new multifile project us

ID: 3560355 • Letter: S

Question

STEP 1: Create a Multifile Project Back to top Create a new multifile project using the Resistor class developed in Week 1, and include a new main() test function.

STEP 2: Modify the Resistor-Class Definition Back to top Add a static data member of the class Resistor to keep track of the number of Resistor objects that are dynamically created. This will also be used to limit the number of objects that are dynamically created to the maximum array size of the pointer array in the test function main(). In the Resistor constructor, use a random-number generator to randomly assign a nominal resistance value to the object between 1,000 and 10,000 ohms. The resistance values are not required to be standard resistance values.

STEP 3: Create the Test Function Main() and the Support Function Back to top Create an array of pointers of type Resistor. Use elements of the pointer array to allow the user to dynamically allocate memory and to instantiate objects of the Resistor class. Use the indirect member-selection operator (pointer) in the test routine to access function members of the Resistor class. Write a new, nonclass function called in function main() to sort the pointers to the Resistor objects in order from lowest nominal resistance value to highest, passing a pointer to the Resistor-object pointer as the only passed data argument. Display the sorted Resistor objects according to the nominal resistance value, from lowest to highest. Within the sorting function, use pointer arithmetic to access the individual Resistor objects. Function main() should also ensure that there are no memory leaks when the program ends.

__________________________________________________________________________

Source App

#include<iostream>
#include<string>
#include<conio.h>
#include"Resistor.h"
using namespace std;
const int SIZE = 5;
void sortResistors(Resistor **resArray)
{
   Resistor * temp;
   for (int i = 0; i < SIZE; i++)
   {
       for (int j = 0; j < SIZE - 1 - i ; j++)
       {
           if( resArray[j]->getResistance() > resArray[j+1]->getResistance())
           {
               temp = resArray[j];
               resArray[j] = resArray[j+1];
               resArray[j+1] = temp;              
           }

       }
   }
}


int main(void)
{
   Resistor *myArray[SIZE];
  
   for (int i = 0; i < SIZE; i++)
   {
       myArray[i] = new Resistor;
   }
   cout << "Displaying unsorted resistor objects ";
   for (int i = 0; i < SIZE; i++)
   {
       myArray[i] -> displayResistor();
   }

   sortResistors(myArray);


   cout << "Displaying sorted resistor objects ";


   for (int i = 0; i < SIZE; i++)
   {
       delete myArray[i];
   }


}

___________________________________________________

Resistor.cpp

#include "Resistor.h"
#include <ctime>
int Resistor::numberObjects = 0;

Resistor::Resistor(void)
{
  
   srand((int)time(0));
  

   rand() % 10000 + 1000;
   rand() % + 1000;

   resistance = rand() % + 1000;
   tolerance = 0.1;
   numberObjects++;
}

Resistor::Resistor(int r, double t)
{
   setResistance (r);
   setTolerance (t);
   numberObjects++;
}


int Resistor::getResistance(void)
{
   return resistance;
}

double Resistor::getTolerance(void)
{
   return tolerance;
}


void Resistor::setResistance(int r)
{
   if (r < 0 || r > 10000)
       resistance = 0;
   else
       resistance = r;
}

void Resistor::setTolerance(double t)
{
   if(t == 0.1 || t == 0.2 || t == 0.05 || t == 0.01)
       tolerance = 0;
   else
       tolerance = t;
}

double Resistor::findMaxResistance(void)
{
   double maxResistance;
   maxResistance = resistance * (1.0 + tolerance);
   return maxResistance;
}


void Resistor::displayResistor (void)
{
   cout << "***Resistor information*** ";
   cout << "Nominal Resistance: " << resistance << endl;
   cout << "Tolerance: " << tolerance << endl;
   cout << "Maximum Resistance: " << findMaxResistance() << endl;
   cout << "Minimum Resistance: " << findMinResistance() << endl;
}

___________________________________________________________

Resistor.h

#pragma once
#include <iostream>
using namespace std;
class Resistor
{
private:
   int resistance;
   double tolerance;
   static int numberObjects;

public:
   Resistor(void);
   Resistor(int r, double t);

  
   int getResistance(void);
   double getTolerance(void);
   void setResistance(int);
   void setTolerance(double);
   static int getNumberObjects(void);

  
   double findMinResistance(void);
   double findMaxResistance(void);
   void displayResistor(void);

  
   ~Resistor();
};

Explanation / Answer

#include<iostream>
#include<iomanip>
#include<string>
using namespace std;
class ResistorClass
{
private:
double m_dResValue;
double m_dTolerance;
double m_dMinResistance;
double m_dMaxResistance;
public:
string m_cResistorName;
void DisplayResistor( void );
void EnterResistance (void );
void AddSeries ( ResistorClass Resistor1, ResistorClass Resistor2 );
ResistorClass( );
ResistorClass( string Name, double nominalResistance, double Tolerance );
ResistorClass( const ResistorClass &ResistorObject );
~ResistorClass( );
};
int main( void )
{
ResistorClass oResOne;
Resistor tolerance = 20%
ResistorClass oResTwo("Resistor 2",4700,20);
ResistorClass oResThree(oResTwo);
oResOne.DisplayResistor();
oResTwo.DisplayResistor();
oResThree.DisplayResistor();
system("pause");
return 0;
}
void ResistorClass:: DisplayResistor ( void )
{
cout << fixed << setprecision (2);
cout<<"Values for "<<m_cResistorName<<" are: "<<endl;
cout<<"Resistor Nominal Value = "<<m_dResValue<<endl;
cout<<"ohmsResistorTolerance = "<<m_dTolerance*100<<" %"<<endl;
cout<<"Mininimum Resistance = "<<m_dMinResistance<<" ohms"<<endl;
cout<<"Maximum Resistance = "<<m_dMaxResistance<<" ohms"<<endl;
cout<<endl;
}
void ResistorClass::EnterResistance (void)
{
m_dResValue = -1;
while (m_dResValue < 1)
{
cout << "Enter Resistor Value: " <<endl;
cin >> m_dResValue;
if (m_dResValue < 1 || m_dResValue > 10000000)
{
cout << "Input Error Resistor Value must be > 1" << endl;
cout << "Please Enter a value that is > 1: " << endl;
cin >> m_dResValue;
}
else break;
}
m_dTolerance = -1;
while (m_dTolerance < 1)
{
cout << "Enter Resistor Tolerance: " << endl;
cin >> m_dTolerance;
if (m_dTolerance < 1 || m_dTolerance > 10000000)
{
cout << "Input Error Resistor Tolerance Value must be > 1: " << endl;
cout << "Please Enter a value that is > 1: " << endl;
cin >> m_dTolerance;
}
else break;
}
m_dTolerance = m_dTolerance/100;
m_dMinResistance = m_dResValue - (m_dResValue * m_dTolerance);
m_dMaxResistance = m_dResValue + (m_dResValue * m_dTolerance);
cout<<endl;
}
void ResistorClass:: AddSeries ( ResistorClass Resistor1, ResistorClass Resistor2)
{
if( Resistor1.m_dTolerance > Resistor2.m_dTolerance )
m_dTolerance = Resistor1.m_dTolerance;
else
m_dTolerance = Resistor2.m_dTolerance;
m_dResValue = Resistor1.m_dResValue + Resistor2.m_dResValue;
m_dMinResistance = m_dResValue - (m_dResValue * m_dTolerance);
m_dMaxResistance = m_dResValue + (m_dResValue * m_dTolerance);
cout<<endl;
}
ResistorClass::ResistorClass( )
{
cout << "Enter Resistor Name <default>: " ;
getline(cin, m_cResistorName, ' ');
m_dResValue = 1000.0;
m_dTolerance = 0.10;
m_dMinResistance = m_dResValue - (m_dResValue * m_dTolerance);
m_dMaxResistance = m_dResValue + (m_dResValue * m_dTolerance);
cout <<endl<< "Default Constructor Called:" <<endl;
}
ResistorClass::ResistorClass( string Name, double nominalResistance, double Tolerance )
{
cout <<endl<<"Parameterized Constructor Called:" <<endl;
m_dResValue = nominalResistance;
m_dTolerance = double(Tolerance/100);
m_cResistorName = Name;
m_dMinResistance = m_dResValue - (m_dResValue * m_dTolerance);
m_dMaxResistance = m_dResValue + (m_dResValue * m_dTolerance);
}
ResistorClass::ResistorClass(const ResistorClass &ResistorObject)
{
cout<<endl <<"Enter Resister Name <copy>: ";
getline(cin, m_cResistorName, ' ');
m_dResValue=ResistorObject.m_dResValue;
m_dTolerance=ResistorObject.m_dTolerance;
m_dMaxResistance=ResistorObject.m_dMaxResistance;
m_dMinResistance=ResistorObject.m_dMinResistance;
cout <<endl<<"Copy Constructor Called:"<<endl;
}
ResistorClass:: ~ResistorClass( )
{
cout << "Destructor Called "<<endl;
}