Question

4 Derived class JPG 4.1 Class declaration • The class JPG inherits from File and is a non-abstract class. 1. Hence objects of4.2 Constructor & destructor • The signature is as follows. 4.3 Copy & assign • The signature is as follows. JPG(const atd::s4.4 Virtual functions 4.4.1 Function clone () • This is a pure virtual function in the base class. • Hence we must override iplease write in c++.

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

#include <iostream>
#include <iomanip>
#include <string>
using namespace std;

class File
{
public:
virtual ~File() {} // virtual destructor

const string& getName() { return name;}
const string& getType() { return type;}
int getSize() {return size;}

virtual void print() const {} // virtual
virtual File* clone() const = 0; // pure virtual

protected:
File(const string& n, const string& t, int x){ // protected constructor
name = n;
type = t;
size = x;
}

string name;
string type;
int size;
};

//end of File class

class PDF: public File
{
public:
PDF(const string& n, const string& text) : File(n, "pdf", text.length())
{
p_text = new string(); // allocate memory for p_text
(*p_text) = text; // copy the text contents
}

~PDF()
{
delete p_text; // delete the dynamically allocated
}

PDF(const PDF& orig) : File(orig) // call File's copy constructor // copy constructor
{
p_text = new string; // allocate memory for p_text
(*p_text) = *(orig.p_text); // copy the text in p_text of orig
}

PDF& operator=(const PDF& orig) // assignment operator
{
*this = orig; // call File's assignment operator
(*p_text) = *(orig.p_text); // copy the text of p_text from orig

return *this;
}

virtual File* clone() const // override pure virtual function
{
return new PDF(*this); // return a new PDF object created using copy constructor by passing this object
}

virtual void print() const{ // override virtual function
// display name, type, size and p_text
cout<<name<<", "<<type<<", "<<size<<endl<<(*p_text)<<endl;
}

protected:
string *p_text; // data (allocated dynamically)
};

//end of PDF class

class JPG : public File
{
public:
JPG(const string& n, int h, int w, const double rgb[]) : File(n, "jpg",h*w)
{
// set height and width
height = h;
width = w;
// allocate memory for p_pixels
p_pixels = new double[size];
// loop to copy p_pixels form rgb
for(int i=0;i<size;i++)
{
p_pixels[i] = rgb[i];
}
}

~JPG() // destructor
{
delete[] p_pixels; // delete the dynamically allocated array
}

JPG(const JPG& orig) : File(orig) // copy constructor
{
// copy height and width
height = orig.height;
width = orig.width;
// allocate memory for p_pixels
p_pixels = new double[size];
// loop to copy p_pixels form orig's p_pixels
for(int i=0;i<size;i++)
p_pixels[i] = orig.p_pixels[i];
}

JPG& operator=(const JPG& orig) // assignment operator
{
*this = orig; // call File's assignment operator
// copy height and width
height = orig.height;
width = orig.width;
// delete the existing array
delete [] p_pixels;
// allocate memory for the new array
p_pixels = new double[size];
// loop to copy values from orig's p_pixels
for(int i=0;i<size;i++)
p_pixels[i] = orig.p_pixels[i];

return *this;
}

virtual File* clone() const // pure virtual function
{
return new JPG(*this);
}

virtual void print() const{ // virtual function
// display the name, type, size
cout<<name<<", "<<type<<", "<<size<<endl;
// display height and width
cout<<height<<", "<<width<<endl;
// loop to display the pixel values
for(int i=0;i<height;i++)
{
for(int j=0;j<width;j++)
{
cout<<p_pixels[i*width+j]<<" ";
}
cout<<endl;
}
}

protected:
int height, width;
double *p_pixels;
};

//end of JPG class

Add a comment
Know the answer?
Add Answer to:
please write in c++. 4 Derived class JPG 4.1 Class declaration • The class JPG inherits...
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
  • please write the code in C++ 2 Base class File 3 Derived class PDF 3.1 Class...

    please write the code in C++ 2 Base class File 3 Derived class PDF 3.1 Class declaration • The class PDF inherits from File and is a non-abstract class 1. Hence objects of the class PDF can be instantiated. 2. To do so, we must override the pure virtual function clone) in the base class • The class declaration is given below. • The complete class declaration is given below, copy and paste it into your file. . It is...

  • 2 Class Vec Message1 2.1 Introduction • Write a class Vec Message1 with the following declaration....

    2 Class Vec Message1 2.1 Introduction • Write a class Vec Message1 with the following declaration. class Vec_Message1 { public: Vec_Message1(); Vec_Message1(int n); Vec_Message1(int n, const Message1 &a); Vec_Message1(const Vec_Message1 &orig); Vec_Message1& operator= (const Vec_Message1 &rhs); ~Vec_Message1(); int capacity() const; int size() const; Message1 front() const; Message1 back() const; void clear(); void pop_back(); void push_back(const Message1 &a); Message1& at(int n); private: void allocate(); void release(); int _capacity; int _size; Message1 * _vec; }; 2.2 allocate() and release() • Examine the...

  • Code in c++ please. Does not have to be complete, just write code for the ones you know. Thank y...

    Code in c++ please. Does not have to be complete, just write code for the ones you know. Thank you. template <typename T> class smart_ptr { public: smart_ptr(); // Create a smart_ptr that is initialized to nullptr. The reference count // should be initialized to nullptr. explicit smart_ptr(T* raw_ptr); // Create a smart_ptr that is initialized to raw_ptr. The reference count // should be one. smart_ptr(const smart_ptr& rhs); // Copy construct a pointer from rhs. The reference count should be...

  • Your will write a class named Gasket that can be used to build Gasket objects (that...

    Your will write a class named Gasket that can be used to build Gasket objects (that represent Sierpinski gaskets) that you can display graphically.  Your Gasket class is defined in the provided file Gasket.h.   DO NOT CHANGE the file Gasket.h.  The attributes and methods defined for class Gasket are described below.     ·sideLength            an int that holds the length of each side of the Gasket.  The length of the side is measured as a number of pixels ·xLocation              an int that holds the x coordinate (in pixels)...

  • Write a code in C++ by considering the following conditions :- Tasks :- 1. Create a...

    Write a code in C++ by considering the following conditions :- Tasks :- 1. Create a class Employee (with member variables: char * name, int id, and double age). 2. Create a constructor that should be able to take arguments and initialize the member variables. 3. Create a copy constructor for employee class to ensure deep copy. 4. Create a destructor and de allocate the dynamic memory. 5. Overload the assignment operator to prevent shallow copy and ensure deep copy....

  • C++ Write a MyString class that stores a (null-terminated) char* and a length and implements all...

    C++ Write a MyString class that stores a (null-terminated) char* and a length and implements all of the member functions below. Submit your completed source (.cpp) file. Default constructor: empty string const char* constructor: initializes data members appropriately Copy constructor: prints "Copy constructor" and endl in addition to making a copy Move constructor: prints "Move constructor" and endl in addition to moving data Copy assignment operator: prints "Copy assignment" and endl in addition to making a copy Move assignment operator:...

  • 1. Here are codes to define a stack class based on dynamic array, please complete the...

    1. Here are codes to define a stack class based on dynamic array, please complete the copy constructor //--- Definition of Stack copy constructor Stack::Stack(const Stack & original) : myCapacity(original.myCapacity), myTop(original.myTop) { //--- Get new array for copy myArray = new(nothrow) StackElement[myCapacity]; if (myArray != 0) // check if memory available                         // copy original's array member into this new array {              // Please complete the function here        } else {          cerr << "*Inadequate memory to allocate...

  • The following program contains the definition of a class called List, a class called Date and...

    The following program contains the definition of a class called List, a class called Date and a main program. Create a template out of the List class so that it can contain not just integers which is how it is now, but any data type, including user defined data types, such as objects of Date class. The main program is given so that you will know what to test your template with. It first creates a List of integers and...

  • you will write a templated array class. Called MyArray with member variables ptr and array_size. This...

    you will write a templated array class. Called MyArray with member variables ptr and array_size. This array must use dynamic memory--see program shell, names should remain unchanged. The following is a list of required member functions of the array class along with a rubric: (1pts) program compiles (2pts) default constructor (2pts) parametered constructor which feeds MyArray a size. (10pts) copy constructor 8pts for performing a *deep* copy 2pts for correct syntax (10pts) overloaded = operator 7pts for functioning properly 3pts...

  • Multiple Choice Multiple Choice Section 4.1 Pointers and Dynamic Memory Consider the following statements: int *p;...

    Multiple Choice Multiple Choice Section 4.1 Pointers and Dynamic Memory Consider the following statements: int *p; int i; int k; i = 42; k = i; p = &i; After these statements, which of the following statements will change the value of i to 75? A. k = 75; B. *k = 75; C. p = 75; D. *p = 75; E. Two or more of the answers will change i to 75. Consider the following statements: int i =...

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