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

Will give best possible rating for solution to this problem! Also, it must be ab

ID: 3766580 • Letter: W

Question

Will give best possible rating for solution to this problem! Also, it must be able to compile without any errors in visual studio.

You then need to develop a C++ program to solve the problem stated by you. Below is a typical problem statement expected for the assignment.

Define two classes with appropriate data members and member functions. Class Name is defined to handle the user first name and the last name. As a minimum, the class will have one string data member in the private access region of the class, a default constructor, and other accessor and mutating member functions in the public access region of the class. The class File is defined to handle the file names for input, output, and input/output purposes. As a minimum, the class will have one string data member in the private access region of the class, a default constructor, and other accessor and mutating member functions in the public access region of the class. Here is how the program will operate.

Upon execution of the program, an object of the class Name is created and its data member is set to your first name via the class constructor (there are different ways this can be done). Another object of this class is defined and obtains your last name from the keyboard and sets your last name (this can be a constructor or a mutating function). The program will then display your full name using the function displayName() to display your full name in the format shown below.

Firstname Lastname

The class Name provides for accepting the first name and the last name as needed by the program.

Once the first name and last name are displayed, the program prepares to write the first name and last name into a file. The program creates objects of the class File as explained here. The first object, sets the data member of the class to the file name associated with the first name via the class constructor, writes the first name to the file, and closes the file. The user is then prompted for the name of the file for writing the last name to the file. Once the last name is written to the file, the file is closed. The program will then open the two files and writes their contents to the screen in the format shown below.

First name

Last name

The program will then create a file object, gets the file name (for the full name) from the user, and writes the first name from the file that has the first name, and the last name from the file that has the last name and closes the files. The next step is for the program to prompt the user to read the contents of the file that has the full name. If the user entered yes, the program will display the full name in the format shown below.

Firstname Lastname

and closes the file. If the user entered no, the program proceeds to the next step.

The user is then prompted to try other user names and file names and repeats the process explained above.

Other member functions for the two classes are defined by the programmer and as a minimum will include constructors, destructors, accessor, and mutating functions. User-defined functions will be used as needed to solve your problem.

This program leaves out a few options for you to select. For example, writing first name, last name, and full name to their respective files and then retrieving them from these file and displaying them.

Explanation / Answer

// Function overloading
#ifndef STASH3_H
#define STASH3_H

class Stash {
int size;      // Size of each space
int quantity; // Number of storage spaces
int next;      // Next empty space
// Dynamically allocated array of bytes:
unsigned char* storage;
void inflate(int increase);
public:
Stash(int size); // Zero quantity
Stash(int size, int initQuantity);
~Stash();
int add(void* element);
void* fetch(int index);
int count();
};
#endif //

// Function overloading
#include "Stash3.h"
#include "../require.h"
#include <iostream>
#include <cassert>
using namespace std;
const int increment = 100;

Stash::Stash(int sz) {
size = sz;
quantity = 0;
next = 0;
storage = 0;
}

Stash::Stash(int sz, int initQuantity) {
size = sz;
quantity = 0;
next = 0;
storage = 0;
inflate(initQuantity);
}

Stash::~Stash() {
if(storage != 0) {
    cout << "freeing storage" << endl;
    delete []storage;
}
}

int Stash::add(void* element) {
if(next >= quantity) // Enough space left?
    inflate(increment);
// Copy element into storage,
// starting at next empty space:
int startBytes = next * size;
unsigned char* e = (unsigned char*)element;
for(int i = 0; i < size; i++)
    storage[startBytes + i] = e[i];
next++;
return(next - 1); // Index number
}

void* Stash::fetch(int index) {
require(0 <= index, "Stash::fetch (-)index");
if(index >= next)
    return 0; // To indicate the end
// Produce pointer to desired element:
return &(storage[index * size]);
}

int Stash::count() {
return next; // Number of elements in CStash
}

void Stash::inflate(int increase) {
assert(increase >= 0);
if(increase == 0) return;
int newQuantity = quantity + increase;
int newBytes = newQuantity * size;
int oldBytes = quantity * size;
unsigned char* b = new unsigned char[newBytes];
for(int i = 0; i < oldBytes; i++)
    b[i] = storage[i]; // Copy old to new
delete [](storage); // Release old storage
storage = b; // Point to new memory
quantity = newQuantity; // Adjust the size
} ///:~

// Function overloading
#include "Stash3.h"
#include "../require.h"
#include <fstream>
#include <iostream>
#include <string>
using namespace std;

int main() {
Stash intStash(sizeof(int));
for(int i = 0; i < 100; i++)
    intStash.add(&i);
for(int j = 0; j < intStash.count(); j++)
    cout << "intStash.fetch(" << j << ") = "
         << *(int*)intStash.fetch(j)
         << endl;
const int bufsize = 80;
Stash stringStash(sizeof(char) * bufsize, 100);
ifstream in("Stash3Test.cpp");
assure(in, "Stash3Test.cpp");
string line;
while(getline(in, line))
    stringStash.add((char*)line.c_str());
int k = 0;
char* cp;
while((cp = (char*)stringStash.fetch(k++))!=0)
    cout << "stringStash.fetch(" << k << ") = "
         << cp << endl;
} ///

// Unions with constructors and member functions
#include<iostream>
using namespace std;

union U {
private: // Access control too!
int i;
float f;
public:
U(int a);
U(float b);
~U();
int read_int();
float read_float();
};

U::U(int a) { i = a; }

U::U(float b) { f = b;}

U::~U() { cout << "U::~U() "; }

int U::read_int() { return i; }

float U::read_float() { return f; }

int main() {
U X(12), Y(1.9F);
cout << X.read_int() << endl;
cout << Y.read_float() << endl;
} ///

Implementation of the class:
#include "Mem.h"
#include <cstring>
using namespace std;

Mem::Mem() { mem = 0; size = 0; }

Mem::Mem(int sz) {
mem = 0;
size = 0;
ensureMinSize(sz);
}

Mem::~Mem() { delete []mem; }

int Mem::msize() { return size; }

void Mem::ensureMinSize(int minSize) {
if(size < minSize) {
    byte* newmem = new byte[minSize];
    memset(newmem + size, 0, minSize - size);
    memcpy(newmem, mem, size);
    delete []mem;
    mem = newmem;
    size = minSize;
}
}

byte* Mem::pointer() { return mem; }

byte* Mem::pointer(int minSize) {
ensureMinSize(minSize);
return mem;
} ///

// Testing the Mem class
#include "Mem.h"
#include <cstring>
#include <iostream>
using namespace std;

class MyString {
Mem* buf;
public:
MyString();
MyString(char* str);
~MyString();
void concat(char* str);
void print(ostream& os);
};

MyString::MyString() { buf = 0; }

MyString::MyString(char* str) {
buf = new Mem(strlen(str) + 1);
strcpy((char*)buf->pointer(), str);
}

void MyString::concat(char* str) {
if(!buf) buf = new Mem;
strcat((char*)buf->pointer(
    buf->msize() + strlen(str) + 1), str);
}

void MyString::print(ostream& os) {
if(!buf) return;
os << buf->pointer() << endl;
}

MyString::~MyString() { delete buf; }

int main() {
MyString s("My test string");
s.print(cout);
s.concat(" some additional stuff");
s.print(cout);
MyString s2;
s2.concat("Using default constructor");
s2.print(cout);
} ///

#ifndef MEM2_H
#define MEM2_H
typedef unsigned char byte;

class Mem {
byte* mem;
int size;
void ensureMinSize(int minSize);
public:
Mem(int sz = 0);
~Mem();
int msize();
byte* pointer(int minSize = 0);
};
#endif // MEM2_H ///

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote