Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

reate a C++ console application that utilizes the core concepts of inheritance a

ID: 3620595 • Letter: R

Question

reate 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.
Question Details
Hi again, I am not good to see how to write this program, and hoping you guys can help.

The formatting of your source code

The use of meaningful identifiers

The extent of internal documentation

The degree to which an exercises’ specifications are meet and the completeness of your lab report.


L A B
Exercise 1: Resistor Class and Pointers (20 points)


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 (# indicates a protected member):

Class: ResistorClass
# double *m_dptrRes
# char *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[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
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.

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
3. Carbon
4. Power

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

Explanation / Answer

Dear,
Here is the code..
#include < iostream >
#include < string >
#include < iomanip >
using namespace std;

//
//CLASS DECLARATION SECTION
//
enum resistorValues {NOMINAL, TOLERANCE, MIN, MAX}; Resistor class
class ResistorClass {
public:
static int m_istResCounter;
void DisplayResistor(void);
void EnterResistance(void);
ResistorClass( );
ResistorClass(char Name[], double nominalResistance, double Tolerance);
~ResistorClass( );

protected:
double *m_dptrRes;
char *m_sptrResName;
};

int ResistorClass ::m_istResCounter;
ResistorClass::ResistorClass ()
{
m_dptrRes = new double[4];
m_sptrResName = new char [50];
m_dptrRes[NOMINAL] = 1000.0 ; //the nominal resistance value
m_dptrRes[TOLERANCE] = .10; //the tolerance value
m_dptrRes[MIN] = m_dptrRes[NOMINAL]-m_dptrRes[NOMINAL] * m_dptrRes[TOLERANCE]; //Nominal - Nominal * Tolerance;//the max resistance value
m_dptrRes[MAX] = m_dptrRes[NOMINAL]+m_dptrRes[NOMINAL] * m_dptrRes[TOLERANCE] ; //Nominal + Nominal * Tolerance;//the min resistance value
cout< }
ResistorClass::ResistorClass (char Name[], double nominalResistance, double Tolerance)
{
m_dptrRes = new double[4];
m_sptrResName = new char [50];
m_dptrRes[NOMINAL] = nominalResistance ; //the nominal resistance value
m_dptrRes[TOLERANCE] = Tolerance; //the tolerance value
m_dptrRes[MIN] = m_dptrRes[NOMINAL]-m_dptrRes[NOMINAL] * m_dptrRes[TOLERANCE]; //Nominal - Nominal * Tolerance;//the max resistance value
m_dptrRes[MAX] = m_dptrRes[NOMINAL]+m_dptrRes[NOMINAL] * m_dptrRes[TOLERANCE] ; //Nominal + Nominal * Tolerance;//the min resistance value
int length = strlen(Name);
m_sptrResName = new char[length + 1];
//copy from Name to the pointer
strcpy_s(m_sptrResName, length + 1, Name);
//increment registor count
m_istResCounter++;
cout< }
ResistorClass::~ResistorClass ()
{
cout< cout << setprecision(5) << fixed << showpoint << setfill(' ');
cout << "Values for " << m_sptrResName<< " are: " ;
cout << left << setw(25) << "Resistor Nominal Value = "
<< right << setw(10) << m_dptrRes[NOMINAL] << " ";
cout << left << setw(25) << "ohmsResistorTolerance = "
<< right << setw(10) << m_dptrRes[TOLERANCE] * 100 << "% ";
cout << left << setw(25) << "Mininimum Resistance = "
<< right << setw(10) << m_dptrRes[MIN] << " ohms ";
cout << left << setw(25) << "Maximum Resistance = "
<< right << setw(10) << m_dptrRes[MAX] << " ohms " ;

}

void ResistorClass::EnterResistance (){
//Displays the current value for m_dResValue
//and prompt the user to enter a new value.
m_dptrRes = new double[4];
bool hasError = false; //default error condition

//continue prompting user for input until values are valid
do {

//input data
cout << " ";
cout << "Enter the Nominal Resistor Value :";
cin >> m_dptrRes[NOMINAL];

cout << "Enter the Resistor Tolerance Value :";
cin >> m_dptrRes[TOLERANCE];

//input error checking
hasError = false;
if (m_dptrRes[NOMINAL] <= 0 || m_dptrRes[NOMINAL] > 10000) {
cout << " ";
cout << "Error: Resistor Value" << endl << "Entry must be between 0 and 10,000 ";
m_dptrRes[NOMINAL] = -1; //denotes error value
hasError = true;
}

if (m_dptrRes[TOLERANCE] > 1) {
cout << " ";
cout << "Error: Tolerance Value" << endl << "Entry must be a decimal value <= 1 ";
m_dptrRes[TOLERANCE] = -1; //denotes error value
hasError = true;
}
} while (hasError);


//Calculate new Values
m_dptrRes[MIN] = m_dptrRes[NOMINAL] - (m_dptrRes[NOMINAL] * m_dptrRes[TOLERANCE]);
m_dptrRes[3] = m_dptrRes[NOMINAL] + (m_dptrRes[NOMINAL] * m_dptrRes[TOLERANCE]);

//display the new values
DisplayResistor();
}
FancyResistorClass
class FancyResistorClass : public ResistorClass
{
public :
FancyResistorClass( char Name [], double nominalResistance, double Tolerance);
void DisplayResistor (void);
~FancyResistorClass();
protected:
char *m_sptrResType;

};

FancyResistorClass::FancyResistorClass(char Name [],double nominalResistance, double Tolerance) : ResistorClass(Name, nominalResistance, Tolerance)
{
//constructor
cout< //Allocate the memory for the resType pointer
string resType;

//Declare a variable to hold the user’s input value for the resistor type
char userInput='c';

do {
cout< cout<<"2. Metal Film (Enter m)"< cout<<"3. Carbon (Enter b)"< cout<<"4. Power (Enter p)"< //Prompt user for an single character entry
cout<<"Enter your choice : ";
//get the user’s input
cin>>userInput;

//Use a switch statement to map the user’s character to the full resistor type
switch(userInput)
{
case 'C': case 'c':
resType = "Carbon Film";
break;
case 'M': case 'm':
resType = "Metal Film";
break;
case 'B': case 'b':
resType = "Carbon";
break;
case 'P': case 'p':
resType = "Power";
break;
default:
userInput='x';
}

} while (userInput == 'x'); // x is an error indicator that keep’s the loop iterating when the
//user enters invalid data

//allocate memory for the m_sptrResType member variable
m_sptrResType = new char [50];
//use strcpy_s to copy the resType into m_sptrResType
int length = (int)resType.length();
strcpy_s(m_sptrResType, length + 1, resType.c_str());
}


void FancyResistorClass::DisplayResistor()
{
//overrides ResistorClass
//Displays all Resistor object data members
//set the output parameters
cout << setprecision(2); //see Ch3 page 136 in text book
cout << fixed << showpoint;
//display the output
cout << " ";
cout << "Fancy Resistor: ";
ResistorClass::DisplayResistor();
cout << "Resistor Type : " << m_sptrResType;
cout << endl;
}//end display resistor

FancyResistorClass::~FancyResistorClass(){
//destructor
cout< delete m_sptrResType; //avoid a memory leak
m_sptrResType = NULL; //not really required here, the class will soon be destroyed
} //end destructor
Main function:
int main(){
//Create the first resistor using the default constructor
ResistorClass oResOne;
oResOne.DisplayResistor();
//Create the second resistor using the parameterized constructor
ResistorClass oResTwo("RESISTOR2", 4700, .20);
oResTwo.DisplayResistor();

//Create a fancy Resistor
cout << endl;
FancyResistorClass oResThree("FancyResistor", 3000,.10);
oResThree.DisplayResistor();
system("pause");
} //end main Hope this will help you..