DigestCPP

Lets Understand With Example

  • Home
  • Design Principal
  • Design Patterns
  • C++ 11 Features
  • C++11 Multithreading
  • Contact Us

Implement Own Vector Class in C++11

Implement Own Vector Class in C++

Source code of Own Vector Class:

#include<iostream>

using namespace std;


template<class T>
class myVector
{
    T* ptr;
    int size;
    int capacity;
public:
    //default constructor
    myVector();
    //assignment constructor
    myVector(int t);
    //destructor
    ~myVector();
    //copy constructor
    myVector(const myVector<T>& actualObj);
    //assignment operator
    myVector<T>& operator = (const myVector<T>& actualObj);
    
    //move constructor
    myVector(myVector<T>&& tmp);
    //move assignment operator
    myVector<T>& operator=(myVector<T>&& tmp);

    void push_back(T);
    void resize(T);
    void display();
    T pop_back();
};

//Default constructor
template<class T>
myVector<T>::myVector()
{
    cout<<"In dafault cons"<<endl;
    size = -1;
    capacity = 1;
    ptr = new T[capacity];
}

//Para contructor
template<class T>
myVector<T>::myVector(int t)
{
    cout<<"In parameter cons"<<endl;
    capacity = t;
    size = -1;
    ptr = new T[capacity];

}

//Destructor
template<class T>
myVector<T>::~myVector()
{
    cout<<"In destructor"<<endl;
    size = -1;
    capacity = 0;
    if(ptr != NULL)
    {
        delete[] ptr;
        ptr = NULL;
    }
}

//copy contructor
template<class T>
myVector<T>::myVector(const myVector<T>& actualObj)
{
    cout<<"In Copy construstor"<<endl;
    size = actualObj.size;
    capacity = actualObj.capacity;
    ptr = new T[capacity];
    for(int i = 0; i<=size; i++)
    {
        *(ptr+i) = *(actualObj.ptr+i);
    }
}

//assignment operator
template<class T>
myVector<T>& myVector<T>::operator = (const myVector<T> & actualObj)
{
    delete[] ptr;
    ptr = NULL;
    size = -1;
    capacity = 0;
    cout<<"Assignment Operator"<<endl;
    size = actualObj.size;
    capacity = actualObj.capacity;
    ptr = new T[capacity];
    for(int i = 0; i<=size; i++)
    {
        *(ptr+i) = *(actualObj.ptr+i);
    }
    return *this;
}

//move contructor
template<class T>
myVector<T>::myVector(myVector<T>&& actualObj)
{
    cout<<"In move construstor"<<endl;
    size = actualObj.size;
    capacity = actualObj.capacity;
    ptr = actualObj.ptr;
    //reset object
    actualObj.size = actualObj.capacity = -1;
    actualObj.ptr = nullptr;
}

//move assignment operator
template<class T>
myVector<T>& myVector<T>::operator = (myVector<T>&& actualObj)
{
    delete[] ptr;
    ptr = NULL;
    size = -1;
    capacity = 0;
    cout<<"move Assignment Operator"<<endl;
    size = actualObj.size;
    capacity = actualObj.capacity;
    ptr = actualObj.ptr;
    //reset object
    actualObj.size = actualObj.capacity = -1;
    actualObj.ptr = nullptr;
    return *this;
}

//push back
template<class T>
void myVector<T>:: push_back(T value)
{
    cout<<"push_back"<<endl;
    size = size + 1;
    if(size  < capacity)
    {        
        *(ptr+size) = value;
       // cout<<"loc:"<<size<<endl;
       // cout<<"Value:"<<*(ptr+size)<<endl;
    }
    else
    {
        resize(value);
    }

}

//resize
template<class T>
void myVector<T>:: resize(T value)
{
    cout<<"resize"<<endl;
    capacity = capacity*2;
    T* temp = new T[capacity];
    for(int i = 0; i<=size; i++)
    {
        *(temp+i) = *(ptr+i);
    }
    delete[] ptr;
    ptr = NULL;
    ptr = temp;
    ptr[size] = value;
    //cout<<"loc:"<<size<<endl;
    //cout<<"Value:"<<*(ptr+size)<<endl;


}

//display
template<class T>
void myVector<T>::display()
{
    cout<<"============Display========="<<endl;
    for(int i = 0; i<=size; i++)
    {
        cout<<"Loc:"<<i<<" value:"<<*(ptr+i)<<endl;
    }
    cout<<"============Display END========="<<endl;
}

//pop up
template<class T>
T myVector<T>::pop_back()
{
cout<<"pop back"<<endl;
T temp = ptr[size];
ptr[size] = '\0';
size = size - 1;
return temp;
}

//client
int main()
{
    cout<<"In Main"<<endl;
    myVector<int> obj4(3);
    obj4.push_back(1);
    obj4.push_back(2);
    obj4.push_back(3);
    obj4.push_back(4);
    obj4.push_back(5);
    obj4.display();
    obj4.pop_back();
    obj4.pop_back();
    obj4.display();
    obj4.push_back(6);
    obj4.display();
    myVector<int> obj6(std::move(obj4)); //move contructor
    obj6.display();
   
    /*
    myVector<float> obj5(3);
    obj5.push_back(1.1);
    obj5.push_back(2.2);
    obj5.push_back(3.1);
    obj5.push_back(4.1);
    obj5.push_back(5.1);
    obj5.display();
    obj5.pop_back();
    obj5.pop_back();
    obj5.display();
    obj5.push_back(6.1);
    obj5.display();
    */

    return 0;
}

Output:

In Main
In parameter cons
push_back
push_back
push_back
push_back
resize
push_back
============Display=========
Loc:0 value:1
Loc:1 value:2
Loc:2 value:3
Loc:3 value:4
Loc:4 value:5
============Display END=========
pop back
pop back
============Display=========
Loc:0 value:1
Loc:1 value:2
Loc:2 value:3
============Display END=========
push_back
============Display=========
Loc:0 value:1
Loc:1 value:2
Loc:2 value:3
Loc:3 value:6
============Display END=========
In move construstor
============Display=========
Loc:0 value:1
Loc:1 value:2
Loc:2 value:3
Loc:3 value:6
============Display END=========
In destructor
In destructor

Primary Sidebar




DigestCPP © 2023. All rights reserved.

    About Privacy Policy Terms and Conditions Contact Us Disclaimer