Exercise 1 Resistor Class and Pointers (20 points) Objective…
Exercise 1: Resistor Class...
Exercise 1: Resistor Class and Pointers (20 points) Submitted: 10 years ago.Category: Programming
Objective: Create a C++ console application that utilizes the core concepts of inheritance and pointers by creating a class and a program that tests important features of that class.
Overview: This lab will require you to use C++ class inheritance to define and use a resistor class derived from a base resistor class. The program will allow the user to specify resistor component types and values.
You will be required to define a base class (ResistorClass) for a generic resistor device and a derived classes (FancyResistorClass) for specific resistor device type.
Resistor Class UML Diagram (# XXXXX a protected member):
# XXXXX *m_dptrRes
# XXXXX *m_sptrResName
+ int m_istResCounter (static)
+ void DisplayResistor(void)
+ void EnterResistance (void)
+ ResistorClass( )
+ ResistorClass(char Name, double nominalResistance, double Tolerance)
+ ~ResistorClass( )
Begin work on this program by first coding the Resistor Class class declaration. Then code and test the Resistor class constructor functions one at a time. Code and test the rest of the functions one function at a time. To avoid compiler complaints, comment out the member function prototypes in the Resistor Class declaration that have not yet been completed.
Resistor Class Member Specifications:
Member Variables Specification
double *m_dptrRes A protected member that is a pointer to an array. This pointer will hold the address of a four element dynamic array as described below.
char *m_sptrResName A protected member that is pointer to a character array. This pointer will hold the address of a dynamic array that contains the resistor's name.
static int m_istResCounter A public member that is a static integer variable used to count the number of current Resistor Class objects.
Member Functions Specification
ResistorClass( ) This is the default constructor.
* You will need to create two dynamic arrays using the new operator. The first array will store the resistor values (nominal, tolerance, min and max) and the second array (a character array) will hold the resistor's name.
* The resistor array will have four elements. The values stored in the array will be of data type double. The values will be stored to the array in the following positions:
m_dptrRes = the nominal resistance value
m_dptrRes = the tolerance value
m_dptrRes = the max resistance value
m_dptrRes = the min resistance value
The nominal value should be initialized to 1000.0 and the tolerance value should be initialized to .10.
The minimum and maximum resistance should then be calculated using the nominal and tolerance variables. The formulas are:
Minimum Resistance = Nominal - Nominal * Tolerance
Max Resistance = Nominal + Nominal * Tolerance
* The user should be prompted to supply a name for the resistor. The value supplied by the user will be stored using the resistor's name pointer. To conserve memory the name array should allocate only enough room to hold the number of characters supplied by the user. You'll want the user's entry to be saved to a string variable using the getline function. Then you'll need to determine the number of characters in the string variable, allocate the required memory and copy the characters from the string variable to the pointer. This requirement is more for demonstration purposes than it is for practicality. Here is the essential code:
//get the user's input
//get the number of characters in the string
int length = (int)Name.length();
//add one to account for the null character \0
m_sptrResName = new char[length + 1];
//copy from Name to the pointer
strcpy_s(m_sptrResName, length + 1, Name.c_str());
The strcpy_s function performs a deep-copy of character array data. You could replace the strcpy_s function with a for/loop that performs a deep copy. The syntax for strcpy_s is as follows:
* To keep count of the number of resistor objects in existence, be sure to increment the static variable m_istResCounter.
* Be sure to include an output statement that displays the message "Default Constructor Called"
ResistorClass(char Name , double nominalResistance, double Tolerance) The parameterized constructor will accept arguments for the resistor's name, nominal resistance and tolerance.
Follow the guidelines given for creating the default constructor but with these modifications:
1. Do not prompt the user for a resistor name. The resistor name will be passed to the constructor as an argument.
2. Do not use default values for the resistor's nominal and tolerance values. These values will be passed to the constructor as arguments. The resistor's minimum and maximum values must be calculated based on the nominal and tolerance values passed as arguments.
3. Do include an output statement that displays the message "Parameterized Constructor Called"
You will need to use the strcpy_s function (as describe above) to copy the name argument into the character array pointed to by m_sptrResName.
void DisplayResistor(void) This is a public member function that displays the resistor object's name, nominal, tolerance, maximum and minimum values.
void EnterResistance(void) This is a public member function that:
Displays the current nominal resistance value and then requires the user to enter a value for nominal resistance greater than 0 and less than or equal to 10,000,000.
Displays the current tolerance value and then requires the user to enter a tolerance value greater than 0 and less than or equal to 50%.
After the user has entered the nominal and tolerance values, the minimum and maximum resistance values should be calculate using the formula given in the specification for the default constructor.
~ResistorClass( ) This is the destructor function.
This function should display the message "Destructor Called for " and complete the message by displaying the resistor's name.
We do not want any memory leaks so be sure to deallocate all of the previously allocated memory.
We want to maintain an accurate count of resistor objects so be sure to decrement the static variable m_istResCounter and for fun display the current value of the m_istResCounter.
NOTE: This resistor class has many member functions similar to the previous resistor class we created. However, the data for this lab's resistor class is not stored in the member variables m_dResValue, m_dTolerance, m_dMinResistance and m_dMaxResistance. In fact, they are no longer members of the resistor class. The resistor data is now stored in a dynamic array that is referenced using the pointer member m_dptrRes. I recommend copying the solution from Week 4's lab as a starting point, but be sure to remove the above mentioned member variables. If you leave them in the class declaration they will cause you nothing but trouble.
It is best to start work by creating the class declaration and coding the default constructor function -- code it, test it and then move on to the next function.
Exercise 2: Enhance and Test the Resistor Class (20 points)
Objective: Practice inheritance and using pointers. Create a program that tests the members of the Resistor Class and Fancy Resistor Class to ensure that they work properly.
Create a derived class named FancyResistorClass by inheriting the ResistorClass.
Begin work on this class by first coding the class declaration. Then code and test the class constructor function. Code and test the rest of the functions one function at a time. To avoid compiler complaints, comment out the member function prototype declarations that have not yet been completed.
The Fancy Resistor Class should include the following members:
Protected Member Variable Specification
char *m_sptrResType A protected member that is a pointer to a character array. This pointer will hold the address of a dynamic character array that contains the resistor's type - Carbon Film, Metal Film, Carbon or Power.
Public Member Functions Specification
FancyResistorClass( char Name , double nominalResistance, double Tolerance) This is the parameterized constructor, which will accept arguments for the resistor's name, nominal resistance and tolerance.
You will need to use the new operator to allocate memory for a character array variable that will be pointed to by m_sptrResType.
The user will be displayed an menu of resistor types and be prompted to select a type. The menu options will be:
1. Carbon Film
2. Metal Film
The menu must reject bad input data and force the user to enter a correct resistor type selection.
The user's valid selection will then be stored in the resistor type character array as either Carbon Film, Metal Film, Carbon or Power.
Be sure to include an output statement that displays the message "FancyResistorClass Constructor Called"
void DisplayResistor (void) This function will override the the base class function of the same name.
This function will output the resistor's type and call the base class display resistor function.
~FancyResistorClass( ) This is the Fancy Resistor Class destructor function.
This function should prevent memory leaks by deallocating any previously allocated memory.
Be sure to display the message "FancyResistorClass Destructor Called"
Test the base and derived resistor classes by completing the following tasks:
The main() function should instantiate two objects of ResistorClass: oResOne, oResTwo. oResOne should be instantiated using the default constructor and oResTwo should use the parameterized constructor. In using the parameterized constructor simply pass the three required arguments to test if the constructor works correctly.
Create a third resistor that is a FancyResistorClass object. Use the FancyResistorClass parameterized constructor to instantiate this object.
Display the values of each of the resistor objects.
End the program