Question

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.3.2 Constructor & destructor • The signature is as follows. 3.3 Copy & assign . The signature is as follows, PDF (const std::3.4 Virtual functions 3.4.1 Function clone () . This is a pure virtual function in the base class. • Hence we must override iplease write the code in C++

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

// C++ code to create File and PDF class

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

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

const string& getName() { return name;} // return name
const string& getType() { return type;} // return type
int getSize() {return size;} // 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;
}

   // attributes
string name;
string type;
int size;
}; //end of File class

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

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

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

   // assignment operator
PDF& operator=(const PDF& orig)
{
       if(this != &orig) // avoids self-assignment
       {
           *this = orig; // call File's assignment operator
           delete p_text; // deallocate existing memory
           p_text = new string; // allocate new memory
           (*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: "<<name<<", Type: "<<type<<", Size: "<<size<<endl<<(*p_text)<<endl;
}

protected:
string *p_text; // data (allocated dynamically)
};
//end of PDF class

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

    please write in c++. 4 Derived class JPG 4.1 Class declaration • The class JPG inherits from File and is a non-abstract class. 1. Hence objects of the class JPG 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. class JPG : public File { public: JPG(const std::string& n, int n, int w, const double rgb()) : File(...) { // ... } *JPG()...

  • what does this output? Derived *d = new Derived; delete d; and Base b; Derived d;...

    what does this output? Derived *d = new Derived; delete d; and Base b; Derived d; given: class Base { public: Base() {std::cout << "Base Default Constructor" << std::endl;} Base(Base &base) {std::cout << "Base Copy Constructor" << std::endl;} virtual ~Base() {std::cout << "Base Destructor" << std::endl;} Base &operator=(const Base &base) {std::cout << "Base Overloaded Assignment" << std::endl; return *this;} }; class Derived : public Base { public: Derived() {std::cout << "Derived Default Constructor" << std::endl;} ~Derived() {std::cout << "Derived Destructor"...

  • 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:...

  • 2. (50 marks) A string in C++ is simply an array of characters with the null...

    2. (50 marks) A string in C++ is simply an array of characters with the null character(\0) used to mark the end of the string. C++ provides a set of string handling function in <string.h> as well as I/O functions in <iostream>. With the addition of the STL (Standard Template Library), C++ now provides a string class. But for this assignment, you are to develop your own string class. This will give you a chance to develop and work with...

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

    Write a MyString class that stores a (null-terminated) char* and a length and implements all of the member functions below. 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: prints "Move assignment" and endl in addition...

  • SCREENSHOTS ONLY PLEASE!!! DON'T POST ACTUAL CODE PLEASE LEAVE A SCREENSHOT ONLY! ACTUAL TEXT IS NOT NEEDED!!! myst...

    SCREENSHOTS ONLY PLEASE!!! DON'T POST ACTUAL CODE PLEASE LEAVE A SCREENSHOT ONLY! ACTUAL TEXT IS NOT NEEDED!!! mystring.h: //File: mystring1.h // ================ // Interface file for user-defined String class. #ifndef _MYSTRING_H #define _MYSTRING_H #include<iostream> #include <cstring> // for strlen(), etc. using namespace std; #define MAX_STR_LENGTH 200 class String { public: String(); String(const char s[]); // a conversion constructor void append(const String &str); // Relational operators bool operator ==(const String &str) const; bool operator !=(const String &str) const; bool operator >(const...

  • 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...

  • IN c++ i post this question 5 times. hope this time somebody answer.. 16.5 Homework 5...

    IN c++ i post this question 5 times. hope this time somebody answer.. 16.5 Homework 5 OVERVIEW This homework builds on Hw4(given in bottom) We will modify the classes to do a few new things We will add copy constructors to the Antique and Merchant classes. We will overload the == operator for Antique and Merchant classes. We will overload the + operator for the antique class. We will add a new class the "MerchantGuild." Please use the provided templates...

  • C++ assignment help! The instructions are below, i included the main driver, i just need help...

    C++ assignment help! The instructions are below, i included the main driver, i just need help with calling the functions in the main function This assignment will access your skills using C++ strings and dynamic arrays. After completing this assignment you will be able to do the following: (1) allocate memory dynamically, (2) implement a default constructor, (3) insert and remove an item from an unsorted dynamic array of strings, (4) use the string class member functions, (5) implement a...

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