Question

-------c++-------- The Passport class takes a social security number and the location of a photo file. The Passport class has a Photo class member, which in a full-blown implementation would store the...

-------c++--------

The Passport class takes a social security number and the location of a photo file. The Passport class has a Photo class member, which in a full-blown implementation would store the passport photo that belongs in that particular passport. This Photo class holds its location or file name and the pixel data of the image. In this lab the pixel data is only used to show how memory maybe used when a program uses large objects. We will not be modifying the pixel data. As an example of how large image data can be, if we took at standard 4k image, the resolution is 7680 x 4320 = 33177600 pixels in a frame. At 36 bits per pixel or 4.5 bytes that’s roughly 149 MBs. Our photo class sets the pixel data size to 16kBs, an unrealistically small size in this digital age. The application creates NUMBER_PASSPORTS_TO_PROCESS number of passport objects and queues them up in passports_to_process. Each passport is given a unique SSN, but for ease of programming uses the same photo file name.

For the final part of the lab, fix the application so more than a million passport objects maybe allocated at one time. Do not modify main.cpp. A good solution to this problem is using C++ heap memory management in the Passport of Photo. To make sure memory allocation is handled appropriately and no memory leaks are introduced, use the “rule of three” described in section 8.12 of the zyBook. If you modify the Photo class be aware that the delete operator for arrays is different from the delete operator for objects,

-------------main.cpp-------------

#include <iostream>
#include <vector>

#include "passport.hpp"

using namespace std;

int main()
{
const int NUMBER_PASSPORTS_TO_PROCESS = 2000;
Passport passports_to_process[NUMBER_PASSPORTS_TO_PROCESS];

for(int ssn = 0; ssn < NUMBER_PASSPORTS_TO_PROCESS; ssn++)
{
passports_to_process[ssn] = Passport(ssn, "picture_of_passport_holder.jpg");
}

cout << NUMBER_PASSPORTS_TO_PROCESS
   << " passports, ready for processing." << endl;
return 0;
}
------Passport.cpp----

#include <iostream>
#include "passport.hpp"

using namespace std;

Passport::Passport()
{
//ctor
}

Passport::Passport(int ssn, std::string pic_file) :
ssn_(ssn), picture_(pic_file)
{
//ctor
}

void Passport::print()
{
   cout << ssn_ << endl;
   picture_.print();
}
------passport.hpp-----

#ifndef PASSPORT_HPP
#define PASSPORT_HPP

#include "photo.hpp"

class Passport
{
public:
Passport();
Passport(int ssn, std::string pic_file);

   void print();

private:
int ssn_;
Photo picture_;
};

#endif // PASSPORT_HPP

--------------------photo.cpp----------

#include <iostream>
#include <cstring>
#include "photo.hpp"

using namespace std;

Photo::Photo(std::string file_name) :
location_(file_name)
{
//ctor
}

Photo::Photo() :
location_("") {}


void Photo::print()
{
   cout << location_ << endl;
   cout << pixel_data_ << endl;
}

-----------photo.hpp--------------

#ifndef PHOTO_HPP
#define PHOTO_HPP

#include <string>

class Photo
{
public:
Photo();
Photo(std::string location);

   void print();

private:
std::string location_;
static const int IMAGE_BUFFER_SIZE_ = 4096;
int pixel_data_[IMAGE_BUFFER_SIZE_];
};

#endif // PHOTO_HPP

Thanks in advanced.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

//main.cpp

// Task1.cpp : This file contains the 'main' function. Program execution begins and ends there.
//

#include <iostream>
#include <vector>
#include "passport.hpp"

using namespace std;

int main()
{
   const int NUMBER_PASSPORTS_TO_PROCESS = 2000;
   Passport passports_to_process[NUMBER_PASSPORTS_TO_PROCESS];

   for (int ssn = 0; ssn < NUMBER_PASSPORTS_TO_PROCESS; ssn++)
   {
       passports_to_process[ssn] = Passport(ssn, "picture_of_passport_holder.jpg");
   }

   cout << NUMBER_PASSPORTS_TO_PROCESS
       << " passports, ready for processing." << endl;
   return 0;
}


// Run program: Ctrl + F5 or Debug > Start Without Debugging menu
// Debug program: F5 or Debug > Start Debugging menu

// Tips for Getting Started:
//   1. Use the Solution Explorer window to add/manage files
//   2. Use the Team Explorer window to connect to source control
//   3. Use the Output window to see build output and other messages
//   4. Use the Error List window to view errors
//   5. Go to Project > Add New Item to create new code files, or Project > Add Existing Item to add existing code files to the project
//   6. In the future, to open this project again, go to File > Open > Project and select the .sln file

//photo.hpp

#ifndef PHOTO_HPP
#define PHOTO_HPP

#include <string>

class Photo
{
public:
   Photo(); // constructor
   Photo(std::string location); // parameterized constructor
   void print();
   // for destroying arrays we use "delete[] 'arrayname'"
   ~Photo(); // destructor
private:
   std::string location_;
   static const int IMAGE_BUFFER_SIZE_ = 4096;
   // the idea here is that when we make objects the memory is not allocated on heap.
   // so now we keep a pointer to the object, and initialize it with new operator in the
   // constructors, so that the memery is initialized in heap.
   int *pixel_data_;
};

#endif // PHOTO_HPP

//photo.cpp

#include <iostream>
#include <cstring>
#include "photo.hpp"

using namespace std;

Photo::Photo(std::string file_name) :
   location_(file_name)
{
   // changes made here, explained in respective hpp file.
   pixel_data_ = new int[IMAGE_BUFFER_SIZE_];
   for (int i = 0; i < IMAGE_BUFFER_SIZE_; i++)
   {
       pixel_data_[i] = 0;
   }
}

Photo::Photo() :
   location_("") {
   // changes made here, explained in respective hpp file.
   pixel_data_ = new int[IMAGE_BUFFER_SIZE_];
   for (int i = 0; i < IMAGE_BUFFER_SIZE_; i++)
   {
       pixel_data_[i] = 0;
   }
}


void Photo::print()
{
   cout << location_ << endl;
   cout << pixel_data_ << endl;
}

Photo::~Photo()
{
   // changes made here, explained in respective hpp file.
   delete[] pixel_data_;
}

//passport.hpp

#ifndef PASSPORT_HPP
#define PASSPORT_HPP

#include "photo.hpp"

class Passport
{
public:
   Passport(); // constructor
   Passport(int ssn, std::string pic_file); // parameterized constructor
   void print();
   ~Passport(); // destructor
private:
   // changed both attributes to pointers, so they are allocated on heap.
   int *ssn_;
   Photo *picture_;
};

#endif // PASSPORT_HPP

//passport.cpp

#include <iostream>
#include "passport.hpp"

using namespace std;

Passport::Passport()
{
   ssn_ = new int();
   picture_ = new Photo();
}

Passport::Passport(int ssn, std::string pic_file)
{
   ssn_ = new int();
   *ssn_ = ssn;
   picture_ = new Photo();
   *picture_ = pic_file;
}

void Passport::print()
{
   cout << ssn_ << endl;
   picture_->print();
}

Passport::~Passport()
{
   ssn_ = 0;
}

C Microsoft Visual Studio Debug Console 2000 passports, ready for processing

for any query leave a comment here ill get back to you as soon as possible :)

PLEASE DO NOT FORGET TO LEAVE A THUMBS UP! THANKS! :)

C Microsoft Visual Studio Debug Console 2000 passports, ready for processing

Add a comment
Know the answer?
Add Answer to:
-------c++-------- The Passport class takes a social security number and the location of a photo file. The Passport class has a Photo class member, which in a full-blown implementation would store the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a C++ Program. You have a following class as a header file (dayType.h) and main()....

    Write a C++ Program. You have a following class as a header file (dayType.h) and main(). #ifndef H_dayType #define H_dayType #include <string> using namespace std; class dayType { public:     static string weekDays[7];     void print() const;     string nextDay() const;     string prevDay() const;     void addDay(int nDays);     void setDay(string d);     string getDay() const;     dayType();     dayType(string d); private:     string weekDay; }; #endif /* // Name: Your Name // ID: Your ID */ #include <iostream>...

  • For the LinkedList class, create a getter and setter for the private member 'name', constructing your...

    For the LinkedList class, create a getter and setter for the private member 'name', constructing your definitions based upon the following declarations respectively: std::string get_name() const; and void set_name(std::string); In the Main.cpp file, let's test your getter and setter for the LinkedLIst private member 'name'. In the main function, add the following lines of code: cout << ll.get_name() << endl; ll.make_test_list(); ll.set_name("My List"); cout << ll.get_name() << endl; Output should be: Test List My List Compile and run your code;...

  • This is for my c++ class and I would really appreciate the help, Thank you! Complete...

    This is for my c++ class and I would really appreciate the help, Thank you! Complete the definitions of the functions for the ConcessionStand class in the ConcessionStand.cpp file. The class definition and function prototypes are in the provided ConcessionStand.h header file. A testing program is in the provided main.cpp file. You don’t need to change anything in ConcessionStand.h or main.cpp, unless you want to play with different options in the main.cpp program. ___________________ Main.cpp ____________________ #include "ConcessionStand.h" #include <iostream>...

  • The following C++ code include 3 files: Patient.h, Patient.cpp and Main.cpp. The program basically creates and stores patient records. The original code has everything in a single .cpp file. I tried t...

    The following C++ code include 3 files: Patient.h, Patient.cpp and Main.cpp. The program basically creates and stores patient records. The original code has everything in a single .cpp file. I tried to divide the code in 3 parts (Patient.h, Patient.cpp and Main.cpp), but it is giving me errors. Patient.h #ifndef PATIENT_H #define PATIENT_H #include <string> #include "Patient.cpp" using namespace std; class Patient{ private : string firstname; string lastname; string location; static int cnt; int id; public : Patient(string, string, string);...

  • You are to implement a MyString class which is our own limited implementation of the std::string...

    You are to implement a MyString class which is our own limited implementation of the std::string Header file and test (main) file are given in below, code for mystring.cpp. Here is header file mystring.h /* MyString class */ #ifndef MyString_H #define MyString_H #include <iostream> using namespace std; class MyString { private:    char* str;    int len; public:    MyString();    MyString(const char* s);    MyString(MyString& s);    ~MyString();    friend ostream& operator <<(ostream& os, MyString& s); // Prints string    MyString& operator=(MyString& s); //Copy assignment    MyString& operator+(MyString&...

  • create a programn in C++ that creates a class that represents a manufactured part on a...

    create a programn in C++ that creates a class that represents a manufactured part on a bill of material (BOM). with the following attributes: identifier (The parts identifier as an alpha numeric string), drawing (The AutoCAD drawing file that represents the part), quantity (The number of parts that are required). implement the functions for the Part class in the file Part.cpp. The file main.cpp will only be used for testing purposes, no code should be written in main.cpp. -part.cpp file:...

  • C++ Language I have a class named movie which allows a movie object to take the...

    C++ Language I have a class named movie which allows a movie object to take the name, movie and rating of a movie that the user inputs. Everything works fine but when the user enters a space in the movie's name, the next function that is called loops infinetly. I can't find the source of the problem. Down below are my .cpp and .h file for the program. #include <iostream> #include "movie.h" using namespace std; movie::movie() { movieName = "Not...

  • Greetings, everybody I already started working in this program. I just need someone to help me...

    Greetings, everybody I already started working in this program. I just need someone to help me complete this part of the assignment (my program has 4 parts of code: main.cpp; Student.cpp; Student.h; StudentGrades.cpp; StudentGrades.h) Just Modify only the StudentGrades.h and StudentGrades.cpp files to correct all defect you will need to provide implementation for the copy constructor, assignment operator, and destructor for the StudentGrades class. Here are my files: (1) Main.cpp #include <iostream> #include <string> #include "StudentGrades.h" using namespace std; int...

  • Im making a generic linked list. I cant figure out how to make an object of...

    Im making a generic linked list. I cant figure out how to make an object of my class from main. My 3 files are main.cpp dan.h dan.cpp The error is: 15 6 [Error] prototype for 'void ll<T>::insert()' does not match any in class 'll<T>' #include <iostream> #include <string> #include "dan.h" using namespace std; int main() { ll<int> work; work.insert(55);//THIS IS THE LINE THATS GIVING ME THE ERROR, Without this line it compiles and //runs. } #ifndef DAN_H #define DAN_H #include...

  • Use main.cpp, Student.h, Student.cpp (written below) and write classes StudentClub and a non-member function find youngest...

    Use main.cpp, Student.h, Student.cpp (written below) and write classes StudentClub and a non-member function find youngest to make main.cpp compile. Put the declaration and definition of find youngest in StudentClub.h and StudentClub.cpp separately. You may not modify provided files and only submit StudentClub.h and StudentClub.cpp. Non-member function find youngest is declared as follows. It returns the names of students who have the youngest age. Note there may exist more than one students who are youngest. std::vector find_youngest(const std::vector member); StudentClub...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT