lundi 17 février 2020

Template function calling

#include <iostream>
#include <memory>
#include <initializer_list>
#include <cassert>
template <typename T>
class Vector
{
    // Your implementation of the Vector class starts here

    private:
//attributes
    int length;
    T* data;

    public:
//constructors
    Vector()
    :length(0),
    data(nullptr) 
    {
        std::cout<<"New empty object created"<<std::endl;
    }

    Vector(int length)
    :length(length), 
    data(new T[length])
    {
        std::cout<< "Length of object is = " <<length <<std::endl;
    }

    //use a copy of another Vector
    Vector(const Vector& other)
    : Vector(other.length)
    {
        for(int i=0;i<length;i++)
            data[i]=other.data[i];

    }

    // using initializer list
    Vector(std::initializer_list<T> list)
    : Vector((int)list.size())
    {
        std::uninitialized_copy(list.begin(), list.end(), data);
        std::cout<< "The elements in the object are: ";
        for (auto it = std::begin(list); it!=std::end(list); ++it)
        std::cout << ' ' << *it;
        std::cout<<std::endl;
    }

//operators

    //copy
    Vector<T>& operator=(const Vector<T>& other)
    {
        if(this!= &other)
        {
            std::cout<<"Copied constructor"<<std::endl;
            delete[] data;
            length = other.length;
            std::cout<<"New length ="<<length<<std::endl;
            data = new T[length];
            std::cout<<"The elements in the object are: ";
            for(int i=0;i<length;i++)
            {
                data[i]=other.data[i];
                std::cout<<data[i]<<" ";
            }
        }
        std::cout<<std::endl;
        //std::cout << "copy operator" << std::endl;
        return *this;
    }
    //move
    Vector<T>& operator=(Vector<T>&& other)
    {
        if(this!= &other)
        {
            delete[] data;
            length = other.length;
            data = new T[length];
            for(int i=0;i<length;i++){
                data[i]=other.data[i];}
            other.length = 0;
            delete[] other.data;
            other.data = nullptr;
        }
        //std::cout << "Move operator" << std::endl;
        return *this;
    }
    ///////////////////////////////////////////////
    //add
    Vector<T> operator+(const Vector<T>& other) const
    {
        assert(length == other.length);
        Vector<T> newer(length);
        std::cout<<"The addition gives: ";
        for(auto i=0;i<length;i++)
        {
            newer.data[i] = data[i]+other.data[i];
            std::cout<<newer.data[i]<<" ";
        }
        std::cout<<std::endl;
        return newer;
    }
    //minus
    Vector<T> operator-(const Vector<T>& other) const
    {
        assert(length == other.length);
        Vector<T> newer(length);
        for(auto i=0;i<length;i++)
            newer.data[i] = data[i]-other.data[i];
        return newer;
    }
    // Multiply by a scalar
    Vector<T> operator*(T scalar)
    {
        Vector<T> newer(length);
        std::cout<<"Multiplication of a new vector by scalar provides: ";
        for (auto i=0; i<length; i++)
        {
            newer.data[i] = data[i] * scalar;
            std::cout<<newer.data[i]<<" ";
        }
        std::cout<<std::endl;
        return newer;
    }
    //////////////////////////////////////////
    // Add to existing Vector
    Vector<T>& operator+=(const Vector<T>& other)
    {
        for (auto i=0; i<length; i++)
            data[i] += other.data[i];
        return *this;
    }
    // Multiply existing Vector by a scalar
    Vector<T>& operator*=(T scalar)
    {
        std::cout<<"Multiplication of an existing vector by scalar provides: ";
        for (auto i=0; i<length; i++)
        {
            data[i] *= scalar;
            std::cout<<data[i]<<" ";
        }
        std::cout<<std::endl;
        return *this;
    }
    double Valueform(int i)
    {
        return data[i];
    }
    int Lengthfinder()
    {
        return length;
    }
    ///////////////////////////////////////////
//destructor
    ~Vector()
    {
        delete[] data;
        data = nullptr;
        length = 0;
    }
};

template<typename T>
T dot(const Vector<T>& lhs, const Vector<T>& rhs)
{
    // Your implementation of the dot function starts here
    T result = 0;
    for (auto i=0; i<lhs.Lengthfinder(); i++)
    {
        result = lhs.Valueform(i)*rhs.Valueform(i);
        //result + = multiply(lhs.data[i],rhs.data[i]);
       // result += lhs.data[i]*rhs.data[i];
    }
    return result;
}
//failsafe for value multiplied by a vector
template <typename T, typename I>
Vector<T> operator*(I i,Vector<T> other)
{
    std::cout<<"Multiplication was done by a vector and failsafe activated"<<std::endl;
    return(other* T(i)) ;
}


int main()
{
    Vector<double> a(5);
    Vector<double> b({ 1 , 2 , 3 , 4 });
    Vector<double> c({ 2 , 3 , 4 });
    Vector<double> d({ 5 , 2 , 1 });   

   // std::cout<< "a=c" <<std::endl;
    a = c;      
  //  std::cout<< "e = c+d" <<std::endl;
    Vector<double> e;
    e = c+d;
  //  std::cout<< "f = c*5" <<std::endl;
    Vector<double> f;
    f = c*5;
 //   std::cout<< "g = 5*d" <<std::endl;
    Vector<double> g;
    g = 5*d;
    Vector<double> Dott;
    Dott = dot(c,d);
    return 0;
}

The code does not allow me to call for the functions Valueform and Lengthfinder, anyone has a possible work around where I can get specific data values and length since the class variables are private? The functions are mostly worked around the template but I would like to just call 2 functions to get certain attributes and they are giving me errors that shouldn't exist, althought I'm not sure why exactly.

Aucun commentaire:

Enregistrer un commentaire