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

Add a static data member of the class Resistor to keep track of the number of Re

ID: 3644684 • Letter: A

Question

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.

Here is what i have

#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
#ifndef RESISTOR_H
#define RESISTOR_H
enum resistorValues{NOMINAL,TOLERANCE,MAX,MIN};
class Resistor {
private:
static int ResCounter;
double* ptrRes;
char* ResName;
public:
Resistor();//default constructor
//argument constructor
Resistor(const string&, double nominalResistance=1000, double Tolerance =.10);
//fuction getName
void getName();
//to display
void DisplayResistor(void);
//
void EnterResistance(void);
~Resistor();
};
int Resistor::ResCounter = 0;
Resistor::Resistor()
{
ptrRes=new double[4];
//default values
ptrRes[NOMINAL]=1000;
ptrRes[TOLERANCE]=.10;
//calculate min and max values
ptrRes[MIN]=ptrRes[NOMINAL]-(ptrRes[NOMINAL] * ptrRes[TOLERANCE]);
ptrRes[MAX]=ptrRes[NOMINAL]+(ptrRes[NOMINAL] * ptrRes[TOLERANCE]);
ResName = NULL;
Resistor::ResCounter++;
}
Resistor::Resistor(const string& name,double nominalResistance,double Tolerance)
{
ptrRes=new double[4];
//default values
ptrRes[NOMINAL]=nominalResistance;
ptrRes[TOLERANCE]=Tolerance;
//calculate min and max values
ptrRes[MIN]=ptrRes[NOMINAL]-(ptrRes[NOMINAL] * ptrRes[TOLERANCE]);
ptrRes[MAX]=ptrRes[NOMINAL]+(ptrRes[NOMINAL] * ptrRes[TOLERANCE]);
//Resistors Name
int length=(int)name.length();
ResName=new char[length+ 1];
strcpy_s(ResName,length+1,name.c_str());
//Increments number of resistor objects that exist
Resistor::ResCounter++;
}
Resistor::~Resistor()
{
delete[]ptrRes;
delete[]ResName;
ptrRes=NULL;
ResName=NULL;
Resistor::ResCounter--;
}
void Resistor::getName()
{
string Name;
cout<<"Enter resistors name: ";
cin >> Name;
int length=(int)Name.length();
if(ResName)
{
delete[] ResName;
ResName=NULL;
}

ResName=new char[length+ 1];
strcpy_s(ResName,length+1,Name.c_str());
}
void Resistor::DisplayResistor ()
{
//Displays all Resistor object data members
//sets the output parameters
cout << setprecision(2);
cout << fixed << showpoint;
//displays the output
cout << " ";
cout << setprecision(5) << fixed << showpoint << setfill(' ');
cout << "Values for " << *ResName << " are: ";
cout << left << setw(25) << "Resistor Nominal Value = "
<< right << setw(10) << ptrRes[NOMINAL] << " ";
cout << left << setw(25) << "ohms Resistor Tolerance = "
<< right << setw(10) << (100*ptrRes[TOLERANCE]) <<"%"<< " ";
cout << left << setw(25) << "Minimum Resistance = "
<< right << setw(10) << ptrRes[MIN] <<" ohms"<< " ";
cout << left << setw(25) << "Maximum Resistance = "
<< right << setw(10) << ptrRes[MAX] <<" ohms" <<" ";
}
#endif


//main .cpp
#include <iostream>
#include <iomanip>
#include <string>
#include "Resistor.h"
using namespace std;
int main()
{
//creating object for resistor
Resistor resistor;
//calling getname function
resistor.getName();
//displying resistor
resistor.DisplayResistor();
system("pause");
return 0;
}

Explanation / Answer

class circuitelement 02 { 03 public: 04 circuitelement(){complex impedance(0.0,0.0); connectiontype='s';frequency=0;}; 05 circuitelement(char pconnect){connectiontype=pconnect;}; 06 virtual ~circuitelement(); 07 virtual void setfrequency()=0; 08 virtual double getfrequency()=0; 09 virtual complex getimpedance()=0; 10 virtual double magnitudeimpedance()=0; 11 virtual double getphase()=0; 12 protected: 13 double frequency;// angular frequency 14 complex impedance ; // Impedance 15 char connectiontype;//Either p or s 16 17 friend class circuit; 18 }; 19 20 21 22 class resistor: public circuitelement 23 { 24 public: 25 resistor():circuitelement(){}; 26 resistor(double pimpedance, char pconnect):circuitelement(pconnect){compleximpedance(pimpedance,0);}; 27 ~resistor(); 28 void setfrequency(); 29 double getfrequency(); 30 complex getimpedance(); 31 double magnitudeimpedance(); 32 double getphase(); 33 34 35 }
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote