Pointers and Pointer Operations Essay Sample
- Word count: 1262
- Category: data
Get Full Essay
Get access to this section to get all help you need with your essay and educational issues.Get Access
Pointers and Pointer Operations Essay Sample
This lab will explore the use of pointers in several ways. Pointers will be used to dynamically allocate memory for new class objects on demand from the user, and they will be used to access class-member functions. Pointer arithmetic will be used to access and sort class objects according to criteria explained in the lab. Pointers are, essentially, address variables, or variables that hold as their value the address of other variables. In terms of memory management, they are very powerful devices, and they more closely and efficiently use the actual internal hardware registers of the microprocessor that the program operates on. Pointers also have the requirement that the pointer type must be of the same data type as the variable, or the data that it points to or holds the address of. The power of pointers also hints at the potential complexity of their use, which is why this lab is focused almost entirely on several different aspects and uses of pointers. The lab also introduces pointer arrays and pointers to pointers. The Resistor class created in the Week 2 lab will be used as the class that the pointer operations will be used upon. The lab also requires the use of accessor functions and static data members, which may need to be added to the Resistor class definition and implementation. Deliverables
1. Submit a single Notepad file containing the source code for all the files of the lab to the Dropbox for Week 5. Your source code should use proper indentation and be error free. Be sure that your last name and the lab number are part of the file name: for example, YourLastName_Lab5.txt.
Each program should include a comment section that includes, at a minimum, your name, the lab and exercise number, and a description of what the program accomplishes. 2. Submit a lab report (a Word document) containing the following information to the Dropbox for Week 5. * Include your name and the lab or lab-exercise number. * Specification: Include a brief description of what the program accomplishes, including its input, key processes, and output. * Test Plan: Include a brief description of the method you used to confirm that your program worked properly. If necessary, include a clearly labeled table with test cases, predicted results, and actual results. * Summary and Conclusions: Write a statement summarizing your predicted and actual output, and identify and explain any differences. For conclusions, write at least one nontrivial paragraph that explains, in detail, either a significant problem you had and how you solved it or, if you had no significant problems, something you learned by doing the exercise. * A UML diagram: This should show all classes, class members, access specifiers, data types, and function arguments, along with any and all class-to-class relationships. * Answers to Lab Questions: Answer any and all lab questions included in the lab steps. Each lab exercise should have a separate section in the lab-report document. Your lab grade will be based upon
1. the formatting of your source code;
2. the use of meaningful identifiers;
3. the extent of internal documentation;
4. the degree to which an exercises’ specifications are met;
5. the completeness of your lab report.
STEP 1: Create a Multifile Project
Create a new multifile project using the Resistor class developed in Week 1, and include a new main() test function.
STEP 2: Modify the Resistor-Class Definition
1. Add a static data member of the class Resistor to keep track of the number of Resistor objects that are dynamically created. This will also be used to limit the number of objects that are dynamically created to the maximum array size of the pointer array in the test function main(). 2. In the Resistor constructor, use a random-number generator to randomly assign a nominal resistance value to the object between 1,000 and 10,000 ohms. The resistance values are not required to be standard resistance values. STEP 3: Create the Test Function Main() and the Support Function 1. Create an array of pointers of type Resistor.
2. Use elements of the pointer array to allow the user to dynamically allocate memory and to instantiate objects of the Resistor class. 3. Use the indirect member-selection operator (pointer) in the test routine to access function members of the Resistor class. 4. Write a new, nonclass function called in function main() to sort the pointers to the Resistor objects in order from lowest nominal resistance value to highest, passing a pointer to the Resistor-object pointer as the only passed data argument. 5. Display the sorted Resistor objects according to the nominal resistance value, from lowest to highest. 6. Within the sorting function, use pointer arithmetic to access the individual Resistor objects. 7. Function main() should also ensure that there are no memory leaks when the program ends.
The resistor class will, at minimum, have members that do the following.
1. store the nominal resistance value of a resistor
2. store the tolerance of a resistor
3. initialize any and all nominal-resistance values to correct, EIA, nonzero values that are greater than 0 and less than 1,000,000 ohms 4. initialize any and all resistance-tolerance values to correct, E12, E24, E48, or E96 resistance-tolerance values 5. allow the nominal-resistance and tolerance values of a resistor object to be changed by the user 6. All member functions should have a test message stating the name of the function. All the test messages should be displayed or not displayed, depending on the value of a Boolean variable declared in main(). a. If the Boolean value = true, display the message.
b. If the Boolean value = false, do not display the message. STEP 3: Program Operations
1. Function main() should instatiate two objects of class resistor. 2. Function main() should display the current values of all resistor objects.
a. Function main() should also calculate and display the minimum and maximum in-tolerance resistance values of each resistor object from the resistor data members. 3. Function main() should allow the user to change the values of the nominal resistance and the resistor tolerance of both resistor objects, and it should also correctly handle out of numeric-range input. Main() is also responsible for making sure that the user can successfully enter only correct, EIA resistance and tolerance values. b. The user should be given the following data-entry choices: 1. accept current EIA values for resistance and tolerance; 4. The function main() should display the new, modified values of the resistor object, including the new min and max in-tolerance resistance values. 5. The function main() should be executed twice: once with the test messages displayed and once without. STEP 4: Lab Questions
You are not required to copy the question text into your document, but all answers should be listed with the question number they answer. 1. List the complete reference-source information for where you found the EIA standard resistor value and tolerance information. 2. How was this reference discovered and where?
3. The constructor requires the initialization values for the nominal resistance and the tolerance when an object is instantiated to be a correct E-series resistance and tolerance combination. Describe how this was accomplished in your program design and implementation. 4. In the lab, you were required to provide mutator functions to change the nominal-resistance and tolerance values of a resistor object. a. Describe how this was accomplished so that the user could not enter an invalid nominal-resistance and E-series tolerance combination. 5. Describe how this process was different and/or similar to how you implemented this validation in the class constructor.