Implement the following functions as members of the AList class

Question

Which has only one member variable, a pointer named listPtr that points to an STL list of type int.

1. Copy constructor
Recall: What must a constructor (any constructor) do first?

 

#include "AList.h"
using namespace std;

// Definition copy constructor
// You code here...

 

2. Copy assignment operator (this is also called overloaded assignment operator)
Recall: What do you need to check when implementing the assignment operator?
Differently from the constructor, the calling object for this function already exists; therefore, before you start copying the data from the parameter object into the calling object, deallocate any dynamic data used by the calling object.

 

#include "AList.h"
using namespace std;

// Definition copy assignment constructor
// If objects are the same, print error message:
// "Objects are the same"
// You code here...

 

3. Move constructor
Recall: What must a constructor (any constructor) do first?
After stealing the data structure from the parameter object, make sure you reset the parameter object to prevent it from being reused.
Add noexcept to the function header.

 

#include "AList.h"
using namespace std;

// Definition move constructor
// You code here...

 

4. Move assignment operator

    •  Recall: What do you need to check when implementing the assignment operator?
    • Differently from the constructor, the calling object for this function already exists; therefore, before you start “stealing the data from the parameter object into the calling object, deallocate any dynamic data used by the calling object.
    • After ‘stealing the data structure from the parameter object, make sure you reset the parameter object to prevent it from being reused.
    • Add noexcept to the function header.
#include "AList.h"
using namespace std;

// Definition move assignment constructor
// If objects are the same, print error message:
// "Objects are the same"

 

Summary

We have implemented a class with the name AList with an STL List inside it as a data member.
And, we have defined a Copy constructor which copies the data from an object to the current instance.
We also have defined a copy assignment operator. It is similar to an overloaded assignment operator.
Finally, we defined a move constructor.

 

Explanation

We have implemented the class AList, with a data member, which is a pointer to the STL list. And, we have defined a default constructor, which allocates memory for the listPtr, and we have pushed the values {10, 20, 30, 40, 50, 60} into the list.

We have defined a copy constructor, and we have passed an AList pointer by reference to it. We have copied the data of object ‘a’ to the current instance, and the data of object ‘a’ are reset.

Here, we have made the listPtr null after using it, and it is to release the source object. We have overloaded an assignment operator, in which we have again copied the data of the source object (passed as a parameter) to the current instance by means of an “=” operator. Before, copying, we have deleted the current instance listPtr.
Finally, we defined a display method that prints the contents of the listPtr of the current instance.

Code

#include <bits/stdc++.h>
using namespace std;
/* to implement AList class */
class AList{
    public:
    /* data member */
    list<int> *listPtr;
    /* default constructor */
    AList(){
        listPtr=new list<int>;
        listPtr->push_back(10); 
        listPtr->push_back(20); 
        listPtr->push_back(30);
        listPtr->push_back(40); 
        listPtr->push_back(50); 
        listPtr->push_back(60);
    }
    /* 1. Copy constructor */
    /* 3. Move constructor */
    AList(AList &a){
        listPtr=a.listPtr;
        
        /* to release the source object */
        a.listPtr=nullptr;
    }
    /* 2. Overloaded assignment operator */
    void operator=(AList &a){
        /* to delete dynamically allocated list before copying */
        /* freeing existing resource */
        delete listPtr;
        listPtr=a.listPtr;
    }
    /* display the list */
    void display()
    {
        for(auto it=listPtr->begin(); it!=listPtr->end();it++)
        cout<<*it<<"  ";
        cout<<endl;
    }
};
/* driver code */
int main() {
    AList a; 
    AList b(a);
    b.display(); 
    AList c=b;
    c.display();
}

Output

STL list Implementation output

 

 

Also read, Write C++ statements to accomplish each of the following

Share this post

Leave a Reply

Your email address will not be published. Required fields are marked *