Constructors and deconstructors.

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"

using namespace std;

int main()
{

// Creates the first resistor.

ResistorClass myResistorObj;

myResistorObj.m_cResistorName = "Resistor One";

myResistorObj.EnterResistance();

// Creates the second resistor.

ResistorClass myResistorObj2;

myResistorObj2.m_cResistorName = "Resistor two";

myResistorObj2.EnterResistance();

// Create the third resistor.

ResistorClass myResistorObj3;

myResistorObj3.m_cResistorName = "Resistor Three";

myResistorObj3.EnterResistance();

// Add Resistor 1 to Resistor 2.

myResistorObj3.AddSeries(myResistorObj,myResistorObj2);

// Display the values of the resistor objects.

myResistorObj.DisplayResistor();

myResistorObj2.DisplayResistor();

myResistorObj3.DisplayResistor();

cout << endl;

cout << "Please press enter to exit the program" << endl;

cin.ignore(2);
return 0;

} // End main

The functions:

#include <iostream>
#include <string>
#include <iomanip>
#include "ResistorClass.h"

using namespace std;

/* 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 << setprecision(2);

cout << fixed << showpoint;

// Displays the output

cout << "\n\n";
cout << setprecision(5) << fixed;

cout << "Values for " << m_cResistorName << " are:\n" ;

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.

bool hasError = false; // Default error condition.

// 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";

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();

} // End EnterResistance function.

/* 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. */

void ResistorClass::AddSeries (ResistorClass Resistor1, ResistorClass Resistor2)

{

// Adds two objects of class Resistor passed as function arguments and save them as the calling objects data member values.

m_dResValue = Resistor1.m_dResValue + Resistor2.m_dResValue;

// Decides which Resistor object's tolerance to use (Which ever is the higher of resister 1 and 2).

if (Resistor1.m_dTolerance > Resistor2.m_dTolerance)

m_dTolerance = Resistor1.m_dTolerance;

else

m_dTolerance = Resistor2.m_dTolerance;

// Calculates the new Values

m_dMinResistance = m_dResValue - (m_dResValue * m_dTolerance);

m_dMaxResistance = m_dResValue + (m_dResValue * m_dTolerance);

} // End AddSeries function.

The header file:

#include <iostream>
#include <string>
#include <iomanip>

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 , ResistorClass);


}; // End class

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. =)
Read the section on constructors and destructors http://www.cplusplus.com/doc/tutorial/classes/

Edit: Also, please place all of your code between the [ code ][ /code ] tags.
Last edited on
Topic archived. No new replies allowed.