Question

Class BTree { private: int size; Node* root; public: // constructors BTree() : root(nullptr), siz...

class BTree

{

private:

int size;

Node* root;

public:

// constructors

BTree() : root(nullptr), size(0) {} // default constructor

BTree(const BTree& other); // copy constructor

BTree(BTree&& other); // move constructor

BTree& operator=(const BTree& other);

BTree& operator=(BTree&& other);

  

};

Implement the following function

BTree& BTree::operator=(BTree&& other)

{

if (this != &other) {

}

return *this;

}

Can you implement the move assigment and the move constructor

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

// Find the move constructor and move assignment operator function in bold below

class Node{

  

};

class BTree

{

  

private:

  

int size;

  

Node* root;

  

public:

  

// constructors

  

BTree() : root(nullptr), size(0) {} // default constructor

  

BTree(const BTree& other); // copy constructor

  

// move constructor

BTree(BTree&& other) : size(0), root(nullptr)

{

// move resources

size = other.size;

root = other.root;

  

// reset other's resources

other.size = 0;

other.root = nullptr;

}

  

BTree& operator=(const BTree& other);

  

BTree& operator=(BTree&& other);

  

  

  

};

// move assignment operator function

BTree& BTree::operator=(BTree&& other)

{

if (this!=&other)

{

// release the current object's resources

delete root;

size = 0;

     // move other's resources

size = other.size;

root = other.root;

// reset other's resources

other.size = 0;

other.root = nullptr;

}

  

return *this;

  

};

Add a comment
Know the answer?
Add Answer to:
Class BTree { private: int size; Node* root; public: // constructors BTree() : root(nullptr), siz...
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
  • #include<iostream> using namespace std; class TNode { public: int val; TNode(){} TNode(int v){val = v;} TNode...

    #include<iostream> using namespace std; class TNode { public: int val; TNode(){} TNode(int v){val = v;} TNode * left; TNode * right; TNode * parent; }; class BTree { public: //constructors and destructor BTree(); BTree(TNode *r);// initialize BTree with the root r. r is the only node. BTree(const BTree & t); // copy constructor BTree(const int *p, const int n);// similar to the copy constructor, but your input is of the array form. // input is given an array that denotes...

  • // Node.h #ifndef NODE_H #define NODE_H class Node { private: int m_entry; Node *m_next; public: Node();...

    // Node.h #ifndef NODE_H #define NODE_H class Node { private: int m_entry; Node *m_next; public: Node(); Node(int entry); int getEntry() const; void setEntry(int entry); Node *getNext(); void setNext(Node *next); }; #endif // Node.cpp #include "Node.h" Node::Node() { m_entry = 0; m_next = nullptr; } Node::Node(int entry) { m_entry = entry; } int Node::getEntry() const { return m_entry; } void Node::setEntry(int entry) { m_entry = entry; } Node *Node::getNext() { return m_next; } void Node::setNext(Node *next) { m_next = next; }...

  • C++ LinkedList I need the code for copy constructor and assignment operator #include <iostream> #include <string> using namespace std; typedef string ItemType; struct Node {    ItemType va...

    C++ LinkedList I need the code for copy constructor and assignment operator #include <iostream> #include <string> using namespace std; typedef string ItemType; struct Node {    ItemType value;    Node *next; }; class LinkedList { private:    Node *head;    // You may add whatever private data members or private member functions you want to this class.    void printReverseRecursiveHelper(Node *temp) const; public:    // default constructor    LinkedList() : head(nullptr) { }    // copy constructor    LinkedList(const LinkedList& rhs);    // Destroys all the dynamically allocated memory    //...

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

  • The function should have a class name CPizza that will have three constructors (default, type, and...

    The function should have a class name CPizza that will have three constructors (default, type, and copyl, public functions to use and implement are noted for you in the starter kit, and private member variables (all which are allocated dynamically). The private member variables include the three different sizes of pizza (Large, Medium, and Small), cost, a bool delivery, name, and delivery fee. The prices of the pizzas' are $20. $15, and $10 for large, medium, and small respectively. The...

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

  • IntList Recursion Assignment Specifications: You will add some additional recursive functions to your IntList class as...

    IntList Recursion Assignment Specifications: You will add some additional recursive functions to your IntList class as well as make sure the Big 3 are defined. IntNode class I am providing the IntNode class you are required to use. Place this class definition within the IntList.h file exactly as is. Make sure you place it above the definition of your IntList class. Notice that you will not code an implementation file for the IntNode class. The IntNode constructor has been defined...

  • Need to implement this function, mostly confused withcopy constructor , operator overloading and deleteNode#ifndef TREE_H #define...

    Need to implement this function, mostly confused withcopy constructor , operator overloading and deleteNode#ifndef TREE_H #define TREE_H #include <iostream> #include <cstdlib> // necessary in order to use NULL class TreeNode { public: TreeNode() : left(NULL), right(NULL) {}    TreeNode* left; TreeNode* right; int value; }; class Tree { public: // Default constructor Tree();    // Copy constructor Tree(const Tree& other);    //Destructor ~Tree();    // overloaded Assignment Operator Tree& operator=(const Tree& other);    // Similar to insert function we discussed...

  • public class Fish { private String species; private int size; private boolean hungry; public Fish() {...

    public class Fish { private String species; private int size; private boolean hungry; public Fish() { } public Fish(String species, int size) { this.species = species; this.size = size; } public String getSpecies() { return species; } public int getSize() { return size; } public boolean isHungry() { return hungry; } public void setHungry(boolean hungry) { this.hungry = hungry; } public String toString() { return "A "+(hungry?"hungry":"full")+" "+size+"cm "+species; } }Define a class called Lake that defines the following private...

  • Implement the following in java. 1. An insertAtBeginning(Node newNode) function, that inserts a node at the...

    Implement the following in java. 1. An insertAtBeginning(Node newNode) function, that inserts a node at the beginning(root) of the linked list. 2. A removeFromBeginning() function, that removes the node from the beginning of the linked list and assigns the next element as the new beginning(root). 3. A traverse function, that iterates the list and prints the elements in the linked list. For the insertAtBeginning(Node newNode) function: 1. Check if the root is null. If it is, just assign the new...

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