Question

In: Computer Science

In a header file Record.h, create a Record structure that contains the following information: recordID, firstName,...

  1. In a header file Record.h, create a Record structure that contains the following information: recordID, firstName, lastName, startYear. recordID is an integer.

  2. In testRecord, first create a record (record1) using “normal” pointers. Set the values to 1001, Fred, Flintstone, 1995

  3. In testRecord, create a new record (record2) using a unique smart pointer. Set the values to 1002, Barney, Rubble, 2000

  4. Create a function (or functions) that will print the records to any output stream. Since the print function does not need to change the record, pass record2 as a constant. Print the records.

  5. Create a function (or functions) that will set the year value of an already created record1 or record2 to the current value minus 10. Print the records.

Solutions

Expert Solution

1). ANSWER :

GIVENTHAT :

Record.h

#include <string>
struct Record
{
int recordID,startYear;
std::string firstName,lastName;
};

testRecord.cpp

#include <iostream>
#include <memory>
#include "Record.h"
using namespace std;

unique_ptr<Record> print(unique_ptr<Record> r) //function to print details of unique pointer of Record
{
cout << "ID: " << r->recordID << endl;
cout << "First Name: " << r->firstName << endl;
cout << "Last Name: " << r->lastName << endl;
cout << "Starting Year: " << r->startYear << endl;
return r;
}

void print(Record *r) //function to print details of normal pointer of Record
{
cout << "ID: " << r->recordID << endl;
cout << "First Name: " << r->firstName << endl;
cout << "Last Name: " << r->lastName << endl;
cout << "Starting Year: " << r->startYear << endl;
}

unique_ptr<Record> change(unique_ptr<Record> r) //function to change year of unique pointer of Record
{
r->startYear-=10;
return r;
}

void change(Record *r) //function to change year of normal pointer of Record
{
r->startYear-=10;
}

int main()
{
//testing the functions
Record *r1=new Record; //dynamically allocating pointer
r1->recordID=1001;
r1->firstName="Fred";
r1->lastName="Flintstone";
r1->startYear=1995;
unique_ptr<Record> r2(new Record); //dynamically allocating unique pointer
r2->recordID=1002;
r2->firstName="Barney";
r2->lastName="Rubble";
r2->startYear=2000;
cout << "Normal pointer before changing: " << endl;
print(r1);
change(r1);
cout << "Normal pointer after changing: " << endl;
print(r1);
cout << "Smart Unique pointer before changing: " << endl;
r2=print(move(r2));
r2=change(move(r2));
cout << "Smart Unique pointer after changing: " << endl;
r2=print(move(r2));
return 0;
}

The above program Output:


Related Solutions

Study the file polygon.h. It contains the header file for a class of regular polygons. Implement...
Study the file polygon.h. It contains the header file for a class of regular polygons. Implement the methods, and provide a driver to test it. It should be in C++ polygon.h file- #ifndef POLY_RVC_H #define POLY_RVC_H #include <iostream> using namespace std; class Polygon { public:    Polygon();    Polygon(int n, double l);    //accessors - all accessors should be declared "const"    // usually, accessors are also inline functions    int getSides() const { return sides; }    double getLength()...
A header file contains a class template, and in that class there is a C++ string...
A header file contains a class template, and in that class there is a C++ string object. Group of answer choices(Pick one) 1)There should be a #include for the string library AND a using namespace std; in the header file. 2)There should be a #include for the string library. 3)There should be a #include for the string library AND a using namespace std; in the main program's CPP file, written before the H file's include.
Create a structure array that contains the following information fields concerning the road bridges in a town
Create a structure array that contains the following information fields concerning the road bridges in a town: bridge location, maximum load (tons), year built, year due for maintenance. Then enter the following data into the array:
Using C++ Create the UML, the header, the cpp, and the test file for an ArtWork...
Using C++ Create the UML, the header, the cpp, and the test file for an ArtWork class. The features of an ArtWork are: Artist name (e.g. Vincent vanGogh or Stan Getz) Medium (e.g. oil painting or music composition) Name of piece (e.g. Starry Night or Late night blues) Year (e.g. 1837 or 1958)
The requirements for this program are as follows: Create a header file named “Employee.h”. Inside this...
The requirements for this program are as follows: Create a header file named “Employee.h”. Inside this header file, declare an Employee class with the following features: Private members a std::string for the employee’s first name a std::string for the employee’s last name an unsigned int for the employee’s identification number a std::string for the city in which the employee works Public members A constructor that takes no arguments A constructor that takes two arguments, representing: the employee’s first name the...
Create a header file, iofunctions.h, containing the following function prototypes. (don't forget to follow the coding...
Create a header file, iofunctions.h, containing the following function prototypes. (don't forget to follow the coding style) int readfile( struct pokemon pokearray[ ], int* num, char filename[ ] ); int writefile( struct pokemon pokearray[ ], int num, char filename[ ] ); Then, create a source file, iofunctions.c, defining the functions above. Specifications The readfile function will read the data from a text file and store it in the array called by pokearray. It must not load the pokemons more than...
please complete the header file that contains a class template for ADT Queue and complete all...
please complete the header file that contains a class template for ADT Queue and complete all the member functions in the class template. Submit the header file only, but please write a source file that tests all the member functions to make sure they are working correctly. queue.h #ifndef _QUEUE #define _QUEUE #include"Node.h" template<class ItemType> class Queue { private:    Node<ItemType> *backPtr;    Node<ItemType> *frontPtr; public:    Queue(); //Default constructor    Queue(const Queue<ItemType> &aQueue);    bool isEmpty() const;    bool...
Build an app to enter a user’s contact information that contains entry fields for firstname, lastname,...
Build an app to enter a user’s contact information that contains entry fields for firstname, lastname, phone number, email, and address. Pick a background color (other than the default) or an image displayed in the app background. Display an image (pick any free image) to display as a banner across the top of the app. Create a button named “Save” that when clicked displays a message “Data has been saved” and clears out the text boxes. Written in C#
Consider the following header file for the intArray object and the following main.cpp and answer the...
Consider the following header file for the intArray object and the following main.cpp and answer the following questions: intArray.h main.cpp #include <iostream> using namespace std; class intArray { friend ostream& operator<<(ostream& outStream, intArray& rhs); public:     intArray();     intArray(int _size);     intArray(int _size, int array[]);     intArray(const intArray&);     ~intArray();     void Print();     void PrintSize();     int Size() const;     int operator[](int) const;     intArray operator=(const intArray ); private:     int size;     int *data; }; 7 #include <iostream>...
Create a Python program that: Reads the content of a file (Vehlist.txt) The file contains matching...
Create a Python program that: Reads the content of a file (Vehlist.txt) The file contains matching pairs of vehicle models and their respective makes Separate out the individual make and model on each line of the file Add the vehicle make to one list, and the vehicle model to another list; such that they are in the same relative position in each list Prompt the user to enter a vehicle model Search the list containing the vehicle models for a...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT