Objective: Create a C++ console application that models the characteristics of a
ID: 3648953 • Letter: O
Question
Objective: Create a C++ console application that models the characteristics of a resistor, as described in Step 2.Create a C++ empty project called ResistorClassTestFirstNameLastName
Create and add to the project a header file called ResistorFirstNameLastName.h) with the Resistor class definition.
Create and add to the project the implementation file for the Resistor class, ResistorFirstNameLastName.cpp.
Create and add to the project the test program file, ResistorTestFirstNameLastName.cpp, with a main() function that instantiates a Resistor object and tests all member functions.
STEP 2: Required Class Members
The Resistor class must have these three private data members:
nominal resistance value (any real number, so use type double)
tolerance as a percentage (any real number, so use type double)
name (string)
The Resistor class must have the following twelve public member methods:
Parameterized constructor that receives, in order, the nominal value, tolerance and name, and sets the data members to the passed values.
setNominalResistance that receives and sets the nominal value.
getNominalResistance that returns (but does NOT display) the nominal value.
displayNominalResistance that displays the nominal value.
setTolerance that receives and sets the tolerance value.
getTolerance that returns (but does NOT display) the tolerance value.
displayTolerance that displays the tolerance value.
setName that receives and sets the name value.
getName that returns (but does NOT display) the name value.
displayName that displays the name value.
maximumR that calculates and returns the maximum resistance = nominal Resistance + (tolerance/100) X nominal Resistance.
minimumR that calculates and returns the minimum resistance = nominal Resistance - (tolerance/100) X nominal Resistance.
STEP 3: Resistor Class Test Program
The main program to test your Resistor class must do the following IN THE EXACT ORDER shown:
Output a message to the user that you are creating a resistor R1 = 3.4 kOhm & 20% tolerance.
Create (instantiate) a Resistor variable firstR called R1 with nominal value 3400 and tolerance 20%.
Announce to the user that you are testing the display functions for firstR.
Call each of the three display functions to display the name, nominal value and tolerance on separate lines.
Announce to the user that you are testing the get functions for firstR.
Use cout and each of the three get methods to display the name, nominal value and tolerance on separate lines.
Announce to the user that you are testing the maximumR function for firstR.
Use cout and maximumR function to display the maximum value of firstR.
Announce to the user that you are testing the minimumR function for firstR.
Use cout and minimumR function to display the minimum value of firstR.
Announce to the user that you are testing the set functions for firstR and changing it to R5 = 1.5 Ohms & tolerance 10% and to expect minimum value = 1.35.
Call each of the three set methods to change the name, nominal value and tolerance.
Call each of the three display functions to display the new name, nominal value and tolerance on separate lines.
Call the minimum and maximum functions.
Explanation / Answer
////////////////////////////////////////////////////////////////////////////
//ResistorFirstNameLastName.h
////////////////////////////////////////////////////////////////////////////
#ifndef RESISTORFIRSTNAMELASTNAME_H
#define RESISTORFIRSTNAMELASTNAME_H
#include<iostream>
#include<string>
using namespace std;
class Resistor{
private:
double m_NomR,m_TolerancePct;
string m_name;
public:
Resistor(double nom, double tol, string name){//Parameterized constructor that receives, in order, the nominal value, tolerance and name,
setNominalResistance(nom); //and sets the data members to the passed values.
setTolerance(tol);
setName(name);
};
void setNominalResistance(double x){m_NomR=x;}//setNominalResistance that receives and sets the nominal value.
double getNominalResistance(){return m_NomR;} //getNominalResistance that returns (but does NOT display) the nominal value.
void displayNominalResistance(){cout<<m_NomR<<" Ohm ";}//displayNominalResistance that displays the nominal value.
void setTolerance(double x){m_TolerancePct=x;}//setTolerance that receives and sets the tolerance value.
double getTolerance(){return m_TolerancePct;}//getTolerance that returns (but does NOT display) the tolerance value.
void displayTolerance(){cout<<m_TolerancePct<<"% ";}//displayTolerance that displays the tolerance value.
void setName(string x){m_name=x;}//setName that receives and sets the name value.
string getName(){return m_name;}//getName that returns (but does NOT display) the name value.
void displayName(){cout<<m_name<<endl;}//displayName that displays the name value.
//maximumR that calculates and returns the maximum resistance = nominal Resistance + (tolerance/100) X nominal Resistance.
double maximumR(){ double max_R = (m_NomR + (m_TolerancePct/100)*m_NomR);
return max_R;}
//minimumR that calculates and returns the minimum resistance = nominal Resistance - (tolerance/100) X nominal Resistance.
double minimumR(){double min_R = (m_NomR - (m_TolerancePct/100)*m_NomR); return min_R;}
//~Resistor(){};//destructor
};
#endif
////////////////////////////////////////////////////////////////////////////
//ResistorTestFirstNameLastName.cpp
////////////////////////////////////////////////////////////////////////////
#include<iostream>
#include<string>
#include "ResistorFirstNameLastName.h";
using namespace std;
int main(){
char wait;
cout<<"Creating a resistor R1 = 3.4 kOhm with 20% tolerance... ";//Output a message to the user that you are creating a resistor R1 = 3.4 kOhm & 20% tolerance.
Resistor firstR(3400,20,"R1");//Create (instantiate) a Resistor variable firstR called R1 with nominal value 3400 and tolerance 20%.
cout<<"Testing the display function for firstR.... ";//Announce to the user that you are testing the display functions for firstR.
//Call each of the three display functions to display the name, nominal value and tolerance on separate lines.
firstR.displayName();
firstR.displayNominalResistance();
firstR.displayTolerance();
cout<<" Testing the get functions for firstR... ";//Announce to the user that you are testing the get functions for firstR.
//Use cout and each of the three get methods to display the name, nominal value and tolerance on separate lines.
cout<<"Name: "<<firstR.getName()<<" Nominal Value: "<<firstR.getNominalResistance()<<" Tolerance: "<<firstR.getTolerance();
cout<<" Testing the maximumR function for firstR... ";//Announce to the user that you are testing the maximumR function for firstR.
cout<<"Maximum value of firstR: "<<firstR.maximumR()<<" Ohm";// Use cout and maximumR function to display the maximum value of firstR.
cout<<" Testing the minimumR function for firstR... ";//Announce to the user that you are testing the minimumR function for firstR.
cout<<"Minimum value of firstR: "<<firstR.minimumR()<<" Ohm";// Use cout and minimumR function to display the maximum value of firstR.
//Announce to the user that you are testing the set functions for firstR and changing
//it to R5 = 1.5 Ohms & tolerance 10% and to expect minimum value = 1.35.
cout<<" Testing the set functions for firstR and changing it to: R5 = 1.5 Ohms with tolerance 10%.Expecting minimum value of 1.35... ";
//Call each of the three set methods to change the name, nominal value and tolerance.
firstR.setName("R5");
firstR.setNominalResistance(1.5);
firstR.setTolerance(10);
//Call each of the three display functions to display the new name, nominal value and tolerance on separate lines.
firstR.displayName();
firstR.displayNominalResistance();
firstR.displayTolerance();
//Call the minimum and maximum functions.
cout<<" Minimum value of firstR: "<<firstR.minimumR()<<" Ohm";
cout<<" Maximum value of firstR: "<<firstR.maximumR()<<" Ohm";
cin>>wait;
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.