Hello everyone, new member here =). I'm having a problem with a lab I'm supposed to do for school and can't figure out the code >=(. Hopefully I can get some help on here to figure it out. (I'm not that good of a programmer by the way)
We're supposed to add constructors and deconstructor classes to a previous lab of ours.
The Previous Lab's code:
The main:
#include <iostream>
#include <string>
#include <iomanip>
#include "ResistorClass.h"
/* Input- Takes all the data of the calculations and input resistance values.
Process- Aligns them properly to display them.
Output- Displays the output values. */
void ResistorClass::DisplayResistor ()
{
// Displays all Resistor object data members.
// Sets the output parameters.
cout << left << setw(25) << "Resistor Nominal Value = " << right << setw(10) << m_dResValue << "\n";
cout << left << setw(25) << "ohmsResistorTolerance = " << right << setw(10) << m_dTolerance * 100 << "% \n";
cout << left << setw(25) << "Mininimum Resistance = " << right << setw(10) << m_dMinResistance << " ohms\n";
cout << left << setw(25) << "Maximum Resistance = " << right << setw(10) << m_dMaxResistance << " ohms\n" ;
} // End DisplayResistor function.
/* Input- The user inputs the resistance values.
Process- Either accepts the input values or asks the user to input them again. Also calculates the minimum and maximum resistance.
Output- Sends the values to the DisplayResistor function to display the value in the command window. */
void ResistorClass::EnterResistance ()
{
// Displays the current value for m_dResValue and prompt the user to enter a new value.
// Continues prompting the user for input values until they are valid.
do {
// Input data
cout << "\n\n";
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 << "\n\n";
cout << "Error: Resistor Value" << endl << "Entry must be between 0 and 10,000\n";
m_dResValue = -1; // Denotes error value
hasError = true;
}
if (m_dTolerance > 1) // The program solves if the tolerance value is an acceptable value of below 1, if not, then the program would ask the user to input the value again below 1.
{
cout << "\n\n";
cout << "Error: Tolerance Value" << endl << "Entry must be a decimal value <= 1\n";
/* Input- Takes the input data from resistor 1 and 2.
Process- Calculates the total resistance of resistor 1 and 2, picks he highest tolerance value to use and calculates the max and min values using the calculated total resistance along with the highest tolerance value.
Output- Stores all the values within resistor 3 to display. */
The functions we're supposed to add to it for constructors and deconstructors:
New Member Functions Specification
ResistorClass( ) Prompt the user to input a name for the resistor object. Store the name in the member variable m_cResistorName.
Initialize the resistor data members to the following values:
Set m_dResValue = 1000.0
Set m_dTolerance = 0.10
The value of m_dMinResistance and m_dMaxResistance should be calculated using the member variables m_dResValue and m_dTolerance. Use the formula from week 3's lab assignment.
This function should output the message: “Default Constructor Called”
ResistorClass(string Name, double nominalResistance, double Tolerance) This is a parameterized constructor that accepts arguments for the resistor's name, nominal resistance and tolerance.
The parameters nominalResistance and Tolerance will be used to initialize the member variables m_dResValue and m_dTolerance.
The value of m_dMinResistance and m_dMaxResistance should be calculated using the member variables m_dResValue and m_dTolerance. Use the formula from week 3's lab assignment.
This function should output the message: “Parameterized Constructor Called”
ResistorClass(const ResistorClass &ResistorObject) This is a copy constructor used to copy the values of an already existing Resistor object.
The nominal, tolerance, minimum and maximum values should be copied from the resistor object passed to the function as an argument.
This function will prompt the user to enter a new name for the Resistor object receiving the copied values. The name will be stored in the member variable m_cResistorName.
This function should output the message: "Copy Constructor Called"
~ResistorClass( ) This is the destructor function. This function will output the message “Destructor Called for " and complete the message by displaying the resistor's name as stored in the member variable m_cResistorName.
It should copy the Second resistor's value (Resistor copy).
This lab is due by Monday next week. Any help at all would be great, please try to use visual examples as much as possible. This is C++, Visual Studios 2010 Ultimate edition. Thanks again for those who can help. =)