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 (# indicates a protected member):
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:
double *m_dptrRes
char *m_sptrResName
static int m_istResCounter
Member Functions:
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[0] = the nominal resistance value
m_dptrRes[1] = the tolerance value
m_dptrRes[2] = the max resistance value
m_dptrRes[3] = 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
string Name;
getline(cin, Name);
//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:
strcpy_s(char *Destination,
int numberOfElements,
char *Source);
* 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.
What do you think? I do not even know where to get started on something like this? Can somebody help me out? Thanks
That is a lot of information they have given you.
They have even told you where to start.
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 (# indicates a protected member):
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.
I think we should start by copying Week 4's lab and deleting the member functions m_dResValue, m_dTolerance, m_dMinResistance and m_dMaxResistance. I have it on good authority that this is what your teacher wants.
NOTE: Copy and paste the code into a new project instead of overwritting and destroying your working one. Teachers always think this is common sense but they never TELL you to do it.
Okay, the code we used for Wk 4 Lab is similar for the resistor that we need this time. I will start there. Then, the resistor class declaration. Then test each one at a time. Also, are there any examples of how to do this. The book does not give very good examples and I am not sure where to start.
Relax, the hardest part about this is going to be remembering what the crazy names of the variables that your teacher created acctually mean. Just do things one step at a time and remember you can't lose points by compiling and test running the program every few lines. Writing in C\C++ can be intimidating at first but after some practice it will flow from your head to the screen like water.
- Start with the Class, declare all of the things your teacher asks you to.
- Next do the constructor & destructor.
- Then write up the member functions, write one then test it, write another test it and the ones before it.
- Keep calc.exe open to make sure that your program is calculating everything right.
- Tip: Do not start by writing in the code that the teacher hands to you, everyone will have their own style and cramming his into your program is just going to frustrate you. Remeber it is there though, come back to it if you think you need to.