Question

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 
      // incremented by one.

    smart_ptr(smart_ptr&& rhs);
      // Move construct a pointer from rhs.

    smart_ptr& operator=(const smart_ptr& rhs);
      // This assignment should make a shallow copy of the right-hand side's
      // pointer data. The reference count should be incremented as appropriate.

    smart_ptr& operator=(smart_ptr&& rhs);
      // This move assignment should steal the right-hand side's pointer data.
      
    bool clone();
      // If the smart_ptr is either nullptr or has a reference count of one, this
      // function will do nothing and return false. Otherwise, the referred to
      // object's reference count will be decreased and a new deep copy of the
      // object will be created. This new copy will be the object that this
      // smart_ptr points and its reference count will be one. 
      
    int ref_count() const;
      // Returns the reference count of the pointed to data.

    T& operator*();
      // The dereference operator shall return a reference to the referred object.
      // Throws null_ptr_exception on invalid access. 

    T* operator->();
      // The arrow operator shall return the pointer ptr_. Throws null_ptr_exception
      // on invalid access.

    ~smart_ptr();          // deallocate all dynamic memory
    
private:
    T* ptr_;               // pointer to the referred object
    int* ref_;             // pointer to a reference count
};
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here is my C++ Code:

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
      // incremented by one.

    smart_ptr(smart_ptr&& rhs);
      // Move construct a pointer from rhs.

    smart_ptr& operator=(const smart_ptr& rhs);
      // This assignment should make a shallow copy of the right-hand side's
      // pointer data. The reference count should be incremented as appropriate.

    smart_ptr& operator=(smart_ptr&& rhs);
      // This move assignment should steal the right-hand side's pointer data.

    bool clone();
      // If the smart_ptr is either nullptr or has a reference count of one, this
      // function will do nothing and return false. Otherwise, the referred to
      // object's reference count will be decreased and a new deep copy of the
      // object will be created. This new copy will be the object that this
      // smart_ptr points and its reference count will be one.

    int ref_count() const;
      // Returns the reference count of the pointed to data.

    T& operator*();
      // The dereference operator shall return a reference to the referred object.
      // Throws null_ptr_exception on invalid access.

    T* operator->();
      // The arrow operator shall return the pointer ptr_. Throws null_ptr_exception
      // on invalid access.

    ~smart_ptr();          // deallocate all dynamic memory

private:
    T* ptr_;               // pointer to the referred object
    int* ref_;             // pointer to a reference count
};

template<typename T> smart_ptr<T>::smart_ptr(){
    ptr_ = nullptr;
    ref_ = nullptr;
}

template<typename T> smart_ptr<T>::smart_ptr(T* row_ptr){
    ptr_ = row_ptr;
    if(ref_ == nullptr){
        ref_ = new int(1);
    }
}

template<typename T> smart_ptr<T>::smart_ptr(const smart_ptr& rhs){
    ptr_ = rhs->ptr_;
    *ref_++;
}

template<typename T> smart_ptr<T>::smart_ptr(smart_ptr&& rhs){
    ptr_ = rhs->ptr_;
    *ref_++;
    rhs.ref_ = nullptr;
    rhs.ptr_ = nullptr;
}

template<typename T> smart_ptr<T>& smart_ptr<T>::operator=(const smart_ptr& rhs){
    ptr_ = rhs->ptr_;
    *ref_++;
    return *this;
}

template<typename T> smart_ptr<T>& smart_ptr<T>::operator=(smart_ptr&& rhs){
    ptr_ = rhs->ptr_;
    *ref_++;
    rhs.ref_ = nullptr;
    rhs.ptr_ = nullptr;
    return *this;
}

template<typename T> bool smart_ptr<T>::clone(){
    if(ptr_ == nullptr || *ref_ == 1)
        return false;
    else{
        *ref_--;
        smart_ptr<T> deepcopy = new smart_ptr<T>(*this);
        *(deepcopy.ref_) = 1;
        return true;
    }
}

template<typename T> int smart_ptr<T>::ref_count() const{
    return *ref_;
}

template<typename T> T& smart_ptr<T>::operator*(){
    return *ptr_;
}

template<typename T> T* smart_ptr<T>::operator->(){
    return ptr_;
}

template<typename T> smart_ptr<T>::~smart_ptr(){
    delete ptr_;
    delete ref_;
}

int main(){
    return 0;
}


if you have any query then feel free to ask in comment.

Add a comment
Know the answer?
Add Answer to:
Code in c++ please. Does not have to be complete, just write code for the ones you know. Thank y...
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
  • Hi all, I need help to do a project based on C++ smart pointers. I have...

    Hi all, I need help to do a project based on C++ smart pointers. I have to implement the class template my_unique_ptr which is a pointer management class template. Also, it's required to implement the following public methods: a. Default constructor that initializes the object to point to nullptr. b. Constructor that takes a pointer Type * as a parameter and sets the object to point there. We say that the newly created object takes ownership of the pointed memory....

  • Language: C++ Complete this function 1.Object &raw_front() { // Return the element at the front of...

    Language: C++ Complete this function 1.Object &raw_front() { // Return the element at the front of the list *without* using the iterator classes // (You may assume the list is not empty) // Place your code here. Code: #ifndef LIST_H #define LIST_H #include using namespace std; template class List { private: // The basic doubly linked list node. // Nested inside of List, can be public // because the Node is itself private struct Node { Object data; Node *prev;...

  • 1. void raw_push_front(const Object &x) { // insert x at the head of the list *without*...

    1. void raw_push_front(const Object &x) { // insert x at the head of the list *without* using the iterator classes // Place your code here. } 2. void raw_push_back(const Object &x) { // insert x at the tail of the list *without* using the iterator classes // Place your code here. } #ifndef LIST_H #define LIST_H #include <algorithm> using namespace std; template<typename Object> class List { private: // The basic doubly linked list node. // Nested inside of List, can...

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

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

  • I have the following c++ data structures assignment: Copy Constructors, Destructors, and Assignment Operators An understanding...

    I have the following c++ data structures assignment: Copy Constructors, Destructors, and Assignment Operators An understanding of how to implement copy constructors, destructors, and assignment operators is essential when working with data structures using dynamic memory allocation. Failure to implement these methods correctly can and probably will result in memory leaks. In this project, you are provided with a working implementation of a doubly-linked list in which the copy constructor, destructor, and assignment operator methods are not complete. To complete...

  • After the header, each line of the database file rebase210.txt contains the name of a restriction...

    After the header, each line of the database file rebase210.txt contains the name of a restriction enzyme and possible DNA sites the enzyme may cut (cut location is indicated by a ‘) in the following format: enzyme_acronym/recognition_sequence/…/recognition_sequence// For instance the first few lines of rebase210.txt are: AanI/TTA'TAA// AarI/CACCTGCNNNN'NNNN/'NNNNNNNNGCAGGTG// AasI/GACNNNN'NNGTC// AatII/GACGT'C// AbsI/CC'TCGAGG// AccI/GT'MKAC// AccII/CG'CG// AccIII/T'CCGGA// Acc16I/TGC'GCA// Acc36I/ACCTGCNNNN'NNNN/'NNNNNNNNGCAGGT// … That means that each line contains one enzyme acronym associated with one or more recognition sequences. For example on line 2:The enzyme acronym...

  • C++ When running my tests for my char constructor the assertion is coming back false and...

    C++ When running my tests for my char constructor the assertion is coming back false and when printing the string garbage is printing that is different everytime but i dont know where it is wrong Requirements: You CANNOT use the C++ standard string or any other libraries for this assignment, except where specified. You must use your ADT string for the later parts of the assignment. using namespace std; is stricly forbiden. As are any global using statements. Name the...

  • 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