STEP 1: Create a Multifile Project Objective: Create a C++ console application t
ID: 3540583 • Letter: S
Question
STEP 1: Create a Multifile Project
Objective: Create a C++ console application that will model the characteristics of a resistor.
Create a multifile project. Create and add to the project an h file containing the resistor-class definition.
Create and add to the project a cpp file containing the implementation of the class-member functions.
Create and add to the project a ccp file containing the main() function, which will instantiate a resistor object and test its member functions.
STEP 2: Required Class Members
The resistor class will, at minimum, have members that do the following.
store the nominal resistance value of a resistor
store the tolerance of a resistor
initialize any and all nominal-resistance values to correct, EIA, nonzero values that are greater than 0 and less than 1,000,000 ohms
initialize any and all resistance-tolerance values to correct, E12, E24, E48, or E96 resistance-tolerance values
allow the nominal-resistance and tolerance values of a resistor object to be changed by the user
All member functions should have a test message stating the name of the function. All the test messages should be displayed or not displayed, depending on the value of a Boolean variable declared in main().
If the Boolean value = true, display the message.
If the Boolean value = false, do not display the message.
STEP 3: Program Operations
Function main() should instatiate two objects of class resistor.
Function main() should display the current values of all resistor objects.
Function main() should also calculate and display the minimum and maximum in-tolerance resistance values of each resistor object from the resistor data members.
Function main() should allow the user to change the values of the nominal resistance and the resistor tolerance of both resistor objects, and it should also correctly handle out of numeric-range input. Main() is also responsible for making sure that the user can successfully enter only correct, EIA resistance and tolerance values.
The user should be given the following data-entry choices:
accept current EIA values for resistance and tolerance;
The function main() should display the new, modified values of the resistor object, including the new min and max in-tolerance resistance values.
The function main() should be executed twice: once with the test messages displayed and once without.
STEP 4: Lab Questions
You are not required to copy the question text into your document, but all answers should be listed with the question number they answer.
List the complete reference-source information for where you found the EIA standard resistor value and tolerance information.
How was this reference discovered and where?
The constructor requires the initialization values for the nominal resistance and the tolerance when an object is instantiated to be a correct E-series resistance and tolerance combination. Describe how this was accomplished in your program design and implementation.
In the lab, you were required to provide mutator functions to change the nominal-resistance and tolerance values of a resistor object.
Describe how this was accomplished so that the user could not enter an invalid nominal-resistance and E-series tolerance combination.
Describe how this process was different and/or similar to how you implemented this validation in the class constructor.
Explanation / Answer
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
//
//CLASS DECLARATION SECTION
//
class ResistorClass {
public:
// public data member
string m_cResistorName;
// public member functions
void DisplayResistor(void);
void EnterResistance(void);
void AddSeries (const ResistorClass& , const ResistorClass&);
// public constructors/destructor
ResistorClass();
ResistorClass(string Name, double nominalResistance, double Tolerance);
ResistorClass(const ResistorClass &ResistorObject);
~ResistorClass();
private:
// private data members
double m_dResValue;
double m_dTolerance;
double m_dMinResistance;
double m_dMaxResistance;
};
int main()
{
/*********************************
* Create a Resistor Class object called oResOne. This object should be
instantiated using the default constructor.
* Create a Resistor Class object called oResTwo. This object should be
instantiated using the parameterized constructor with the following arguments:
Name = "Resistor 2"
Nominal resistance = 4700 ohms
Resistor tolerance = 20%
* Using the copy constructor, create a third Resistor Class object called
oResThree. Pass oResTwo as an argument to the copy constructor.
* The program should then display the current values of all three Resistor
objects using the member function DisplayResistor( ).
**********************************/
{
//Create the first resistor
ResistorClass oResOne;
//Create the second resistor
ResistorClass oResTwo("Resistor 2", 4700, 0.20);
//Create the third resistor
ResistorClass oResThree = oResTwo;
//Add Resistor 2 to Resistor 3
oResThree.AddSeries(oResOne,oResTwo);
//Display the values of the resistor objects
oResOne.DisplayResistor();
oResTwo.DisplayResistor();
oResThree.DisplayResistor();
}
cin.ignore(2);
} //end main
//
//CLASS IMPLEMENTATION SECTION
//prompt for m_cResistorName
//Set m_dResValue = 1000.0
//Set m_dTolerance = 0.10
ResistorClass::ResistorClass()
{
string inputStr = "";
cout << "Enter resistor name (default): "; cin >> inputStr;
m_cResistorName = inputStr;
m_dResValue = 1000.0;
m_dTolerance = 0.10;
//Calculate new Values
m_dMinResistance = m_dResValue - (m_dResValue * m_dTolerance);
m_dMaxResistance = m_dResValue + (m_dResValue * m_dTolerance);
cout << "default constructor called" << endl;
}
ResistorClass::ResistorClass(string Name, double nominalResistance, double Tolerance)
{
m_cResistorName = Name;
m_dResValue = nominalResistance;
m_dTolerance = Tolerance;
//Calculate new Values
m_dMinResistance = m_dResValue - (m_dResValue * m_dTolerance);
m_dMaxResistance = m_dResValue + (m_dResValue * m_dTolerance);
cout << "parameterized constructor called" << endl;
}
ResistorClass::ResistorClass(const ResistorClass &ResistorObject)
{
string inputStr = "";
cout << "Enter resistor name (copy): "; cin >> inputStr;
m_cResistorName = inputStr;
m_dResValue = ResistorObject.m_dResValue;
m_dTolerance = ResistorObject.m_dTolerance;
m_dMaxResistance = ResistorObject.m_dMaxResistance;
m_dMinResistance = ResistorObject.m_dMinResistance;
cout << "copy contructor called" << endl;
}
ResistorClass::~ResistorClass()
{
cout << "Destructor called for " << m_cResistorName << endl;
}
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 << " ";
cout << setprecision(5) << fixed << showpoint << setfill(' ');
cout << "Values for " << m_cResistorName << " are: " ;
cout << left << setw(25) << "Resistor Nominal Value = "
<< right << setw(10) << m_dResValue << " ";
cout << left << setw(25) << "ohmsResistorTolerance = "
<< right << setw(10) << m_dTolerance * 100 << "% ";
cout << left << setw(25) << "Mininimum Resistance = "
<< right << setw(10) << m_dMinResistance << " ohms ";
cout << left << setw(25) << "Maximum Resistance = "
<< right << setw(10) << m_dMaxResistance << " ohms " ;
}
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 << " ";
cout << "Enter the Nominal Resistor Value :";
cin >> m_dResValue;
cout << "Enter the Resistor Tolerance Value :";
cin >> m_dTolerance;
//input error checking
hasError = false;
if (m_dResValue <= 0 || m_dResValue > 10000) {
cout << " ";
cout << "Error: Resistor Value" << endl << "Entry must be between 0 and 10,000 ";
m_dResValue = -1; //denotes error value
hasError = true;
}
if (m_dTolerance > 1) {
cout << " ";
cout << "Error: Tolerance Value" << endl << "Entry must be a decimal value <= 1 ";
m_dTolerance = -1; //denotes error value
hasError = true;
}
} while (hasError);
//Calculate new Values
m_dMinResistance = m_dResValue - (m_dResValue * m_dTolerance);
m_dMaxResistance = m_dResValue + (m_dResValue * m_dTolerance);
//display the new values
DisplayResistor();
}
void ResistorClass::AddSeries (const ResistorClass &Resistor1, const ResistorClass &Resistor2){
//Adds two objects of class Resistor passed as function arguments and save them as the calling object's data member values.
this->m_dResValue = Resistor1.m_dResValue + Resistor2.m_dResValue;
//Decided which Resistor object's tolerance to use
if (Resistor1.m_dTolerance > Resistor2.m_dTolerance)
this->m_dTolerance = Resistor1.m_dTolerance;
else
this->m_dTolerance = Resistor2.m_dTolerance;
//Calculate new Values
m_dMinResistance = m_dResValue - (m_dResValue * m_dTolerance);
m_dMaxResistance = m_dResValue + (m_dResValue * m_dTolerance);
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.