In: Computer Science
In this lab, you will be completing a programming exercise through the point of view of both a
contracted library developer and the client that will use the developed code.
In the first part, you will be required to write a class in C++ that will be included in the client’s code.
Your class must be written with defensive programming in mind. It should allow the client to include or
leave out your defensive checks at compile-time.In the second part, you will use your defensively
programmed class methods to guide the revision of the provided user driver program. After
encountering failures from misuse of the library class methods, you will update the driver program,
according to the implementation guidelines in the CWE documentation for the appropriate error.
Note that the code you write does not have to be completely optimized and you will likely see better
ways to write the class and the driver to avoid the problems inherit in the client descriptions.
In Part 1 of the lab you will complete the following:
Write a class called myArray in a file named “yourlastname_lab2.cpp” where you substitute your
own name
o Constructor
two inputs: int size and string input
dynamically create a char* array of int size
parse string input (which should be a string of comma-separated characters)
and enter the characters in the array in order
o Destructor should free the memory assigned to your char* array
o ReadFromArray
one input: int index
return char at the given index for the array
o WriteToArray
two inputs: int index, char replace
overwrite char at given index with new char replace
o DeleteArray
free the memory for your char*
set char* to NULL
o PrintArray
output the contents of the char* array to stdout
o NewArray
two inputs: int size and string input
dynamically create a char* array of int size
parse string input (which should be a string of comma-separated characters)
and enter the characters in the array in order
For each class method, provide the contract for proper usage of the method
o enter as comment lines directly after the definition
o List any preconditions (what has to be true immediately before executing the method)o List any postconditions (what has to be true immediately after executing the method)
Utilize C standard assert() library calls from assert.h to test your preconditions
Use macros to give the client the option on whether to include the asserts at compile-time
Use the provided sample client driver program to test your class code
Take screenshots of your assertions being invoked for each function
In Part 2 of the lab you will complete the following:
• Using the assertions you have placed into your class methods, update the driver code to ensure
calls made to the class methods are in-contract
• Identify what CWE errors, if applicable, are occurring with out-of-contract use of your class
methods
• Review the ‘Potential Mitigation” section for those CWE errors and use the “Phase:
Implementation” entries to guide your revision of the provided program driver.
• Take screenshots of the driver code working without hitting the assertions – be sure to explain
in your word document how you tested the preconditions of each method and what changes
you made to the driver to ensure in-contract calls were made to the methods.
Graduate students should also answer the following:
• Is there a Python equivalent to the C-standard assert() calls used in class with C++?
• How would you approach defensive programming from the point-of-view of python methods?
Submit a zip file to Blackboard which contains your class file and a word document which includes the
screenshots and other information described above.
For full credit your code should compile, run as described, and be appropriately commented. If I need to
know anything in particular about how I should compile your code, include that in your document.
GIVEN cpp code:
============
//Sample client code for interfacing with myArray class
//Use this driver program to test your class and defensive
programming assertions
#include <iostream>
#include <string>
#include <stdlib.h>
#include "your_class_here.cpp" //replace this with your
own file
using namespace std;
int main(){
int size, choice, read, write;
string input;
char replace, response;
char * array;
cout << "Welcome, please enter a maximum size
for your array" << endl;
cin >> size;
cout << "Please enter a series of
comma-separated characters for your array" << endl;
cin >> input;
//create object of class type which should
dynamically allocate a char* array
//of int size and fill it with the comma-separated
values from string input
Array myArray(size, input);
while(1){
cout << "Array Menu"
<< endl;
cout << "1. Read by index"
<< endl;
cout << "2. Write by index"
<< endl;
cout << "3. Delete array"
<< endl;
cout << "4. Print array"
<< endl;
cout << "5. New Array"
<< endl;
cout << "6. Exit" <<
endl;
cin >> choice;
switch(choice){
case 1:
cout << "Enter an index to read a value from the array"
<< endl;
cin
>> read;
//call to library function ReadFromArray(int read)
//this library call should read a single character from the array
and return it
response = myArray.ReadFromArray(read);
cout << "The item in index[" << read << "] is "
<< response << endl;
break;
case 2:
cout << "Enter an index to write a value to the array"
<< endl;
cin
>> write;
cout << "What single character would you like to write to the
array?" << endl;
cin
>> replace;
//call to library function WriteToArray(int write, char
replace)
//this library call should write a single character to the
array
myArray.WriteToArray(write,replace);
cout << "The item in index[" << write << "] is "
<< myArray.ReadFromArray(write) << endl;
break;
case 3:
//call to library function DeleteArray() which should free the
dynamically allocated array
myArray.DeleteArray();
break;
case 4:
//call to library function PrintArray() which will print the
contents of the array to stdout
myArray.PrintArray();
break;
case 5:
//call to library function NewArray() which will dynamically
allocate a new array
cout << "Welcome, please enter a maximum size for your array"
<< endl;
cin
>> size;
cout << "Please enter a series of comma-separated characters
for your array" << endl;
cin
>> input;
myArray.NewArray(size, input);
break;
case 6:
exit(0);
break;
}
}
return 0;
}
yourclass:
class myArray{
char *arr;
public:
myArray(int size, char input[]){
arr = new char[size];
for(int i=0;i<size;i++){
arr[i] = '\0';
}
char *c;
string tok;
int i=0;
stringstream ss(input);
while(getline(ss, tok, ',')){
if(tok.size() > 1)
exit(0);
else arr[i++] = tok[0];
}
}
~myArray(){
delete []arr;
}
char ReadFromArray(int index){
return arr[index];
}
void WriteToArray(int index, char replace){
arr[index] = replace;
}
void DeleteArray(){
delete arr;
arr = NULL;
}
void PrintArray(){
cout<<arr<<endl;
}
void NewArray(int size, string input){
char *newarr = new char[size];
for(int i=0;i<size;i++){
newarr[i] = '\0';
}
string tok;
int i=0;
stringstream ss(input);
while(getline(ss, tok, ',')){
if(tok.size() > 1)
exit(0);
else newarr[i++] = tok[0];
}
}
};
Fullcode:
//Sample client code for interfacing with myArray class
//Use this driver program to test your class and defensive
programming assertions
#include <iostream>
#include <string.h>
#include <stdlib.h>
#include<sstream>
using namespace std;
class myArray{
char *arr;
public:
myArray(int size, char input[]){
arr = new char[size];
for(int i=0;i<size;i++){
arr[i] = '\0';
}
char *c;
string tok;
int i=0;
stringstream ss(input);
while(getline(ss, tok, ',')){
if(tok.size() > 1)
exit(0);
else arr[i++] = tok[0];
}
}
~myArray(){
delete []arr;
}
char ReadFromArray(int index){
return arr[index];
}
void WriteToArray(int index, char replace){
arr[index] = replace;
}
void DeleteArray(){
delete arr;
arr = NULL;
}
void PrintArray(){
cout<<arr<<endl;
}
void NewArray(int size, string input){
char *newarr = new char[size];
for(int i=0;i<size;i++){
newarr[i] = '\0';
}
string tok;
int i=0;
stringstream ss(input);
while(getline(ss, tok, ',')){
if(tok.size() > 1)
exit(0);
else newarr[i++] = tok[0];
}
}
};
int main(){
int size, choice, read, write;
string input;
char replace, response;
cout << "Welcome, please enter a maximum size for your
array" << endl;
cin >> size;
cout << "Please enter a series of comma-separated
characters for your array" << endl;
cin >> input;
char arr[input.size()+1];
for(int i=0;i<input.size();i++)
arr[i] = input[i];
//create object of class type which should dynamically allocate a
char* array
//of int size and fill it with the comma-separated values from
string input
myArray Array(size, arr);
while(1){
cout << "Array Menu" << endl;
cout << "1. Read by index" << endl;
cout << "2. Write by index" << endl;
cout << "3. Delete array" << endl;
cout << "4. Print array" << endl;
cout << "5. New Array" << endl;
cout << "6. Exit" << endl;
cin >> choice;
switch(choice){
case 1:
cout << "Enter an index to read a value from the array"
<< endl;
cin >> read;
//call to library function ReadFromArray(int read)
//this library call should read a single character from the array
and return it
response = Array.ReadFromArray(read);
cout << "The item in index[" << read << "] is "
<< response << endl;
break;
case 2:
cout << "Enter an index to write a value to the array"
<< endl;
cin >> write;
cout << "What single character would you like to write to the
array?" << endl;
cin >> replace;
//call to library function WriteToArray(int write, char
replace)
//this library call should write a single character to the
array
Array.WriteToArray(write,replace);
cout << "The item in index[" << write << "] is "
<< Array.ReadFromArray(write) << endl;
break;
case 3:
//call to library function DeleteArray() which should free the
dynamically allocated array
Array.DeleteArray();
break;
case 4:
//call to library function PrintArray() which will print the
contents of the array to stdout
Array.PrintArray();
break;
case 5:
//call to library function NewArray() which will dynamically
allocate a new array
cout << "Welcome, please enter a maximum size for your array"
<< endl;
cin >> size;
cout << "Please enter a series of comma-separated
characters for your array" << endl;
cin >> input;
Array.NewArray(size, input);
break;
case 6:
exit(0);
break;
}
}
return 0;
}
OUTPUT:
