Click here to Skip to main content
15,355,659 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Add an overloaded + operator method such
that it prints out the sum of two vectors. You can also add another overloaded * operator method with
a float as an argument such that it multiplies the scalar by each of the data elements

I've been tasked to add an overloaded + operator that prints the sum of two vectors along with another operator that adds another overloaded operator with a float as an argument such that it multiplies the scalar by each of the data. It works, but I've now introduced two vectors which are D and E.
Could I simply change
Vector Vector::operator*(Vector & v)
to the operator that represent it in addition? I've tried to see which operator I could use for addition but was not able to.
For E, I suppose I could use the same function in another line but instead declare that the variable will be instead multiplied by 4. So it would be
r.data[i] = data[i] *4v[i];


I had asked this question before but deleted it to upload a more clearer explanation of my issue. Thanks

What I have tried:

C++
#include <string>
#include <iostream>
using namespace std;

class Vector
{
	public:
		Vector(int, float *);
	Vector(const Vector &);
	~Vector();
	Vector operator*(Vector &); //Will take vector as argument and return it.
	float operator[](int) const;
	void print();
	int sizeOf()
	{
		return size;
	}

	private:
		float *data;
	int size;
};

class myVector: public Vector 
{
	public: myVector(const Vector &);
	void print();
};

Vector::Vector(int s, float *d): size(s)
{
	data = new float[size]; //This is our data array. 
	for (int i = 0; i < size; i++) // This is the all item in the data arrays being copied.
	{
		data[i] = d[i];
	}
}

Vector::Vector(const Vector &v): size(v.size)
{
	data = new float[size];
	for (int i = 0; i < size; i++)
	{
		data[i] = v[i];
	}
}

Vector::~Vector() // This is our destructor, this is done when the function is done with the variable.
{
	delete[] data; //This is an array of objects, so we can delete with bracketed objects.
}

Vector Vector::operator*(Vector & v)
{
	Vector r(size, data); //Both sizes should be the same.

	for (int i = 0; i < size; i++) //Do the multiplication
	{
		r.data[i] = data[i] *v[i]; // 
	}

	return r;
}



float Vector::operator[](int i) const
{
	return data[i];
}

void Vector::print()
{
	for (int i = 0; i < size; i++)
	{
		cout << data[i] << " ";
	}

	cout << endl;
}

myVector::myVector(const Vector &v): Vector(v) {}

void myVector::print() //This is the method definition
{
	for (int i = 0; i < sizeOf(); i++)
	{
		cout << (*this)[i] << endl;
	}

	cout << endl;
}

int main()
{
	float data1[] = { 1, 2, 3 };
	float data2[] = { 3, 4, 5 };

	Vector a(3, data1);
	Vector b(3, data2);
	Vector c = a * b; //This vector C that is a*b. 
	Vector d = a +b;
	Vector e = a*4;
	c.print();
	d.print();
	e.print()
	
}
Posted
Updated 8-Apr-21 20:53pm
v2

The answer is yes, you can. Although I recommend you pass a constant reference to the object. Something like this :
C++
Vector Vector::operator * ( const Vector & v );

Vector Vector::operator + ( const Vector & v );
You could make this a template class if you want to.
C++
template< typename T >
class Vector
{
public:
    Vector( int size, T * dataArray );
    ~Vector();

    T operator[]( int index ) const;

private:
    T * data;
    int size;
};
   
Comments
Member 15084336 8-Apr-21 16:18pm
   
Yeah I've declared that but I've received a few errors in the int main ().
Along with vector
.c:65:8: error: no declaration matches ‘Vector Vector::operator+(const Vector&)’
65 | Vector Vector::operator + ( const Vector & v );
| ^~~~~~
vector.c:65:8: note: no functions named ‘Vector Vector::operator+(const Vector&)’
vector.c:5:7: note: ‘class Vector’ defined here
5 | class Vector
| ^~~~~~
vector.c:66:1: error: expected unqualified-id before ‘{’ token
66 | {
| ^
vector.c:77:8: error: no declaration matches ‘Vector Vector::operator*(const Vector&)’
77 | Vector Vector::operator * ( const Vector & v );
| ^~~~~~
vector.c:53:8: note: candidate is: ‘Vector Vector::operator*(Vector&)’
53 | Vector Vector::operator*(Vector & v)
| ^~~~~~
vector.c:5:7: note: ‘class Vector’ defined here
5 | class Vector
| ^~~~~~
vector.c:78:1: error: expected unqualified-id before ‘{’ token
78 | {
You are asked to implement the addition of vectors and the multiplication of a vector by a scalar. Something similar to:


C++
// ...

class Vector
{ 
  public:
    Vector(int, float *);
  Vector(const Vector &);
  ~Vector();
  Vector operator*(Vector &); //Will take vector as argument and return it.
  
  Vector operator + (const Vector & ); // addition of vector
  Vector operator * ( float ); // multiplication of vector by scalar
 
// ... 

Vector  Vector::operator + (const Vector & w)
{ 
  Vector r(*this);
  for (int i = 0; i < size; i++)
    r.data[i] += w.data[i];
  return r;
} 

Vector Vector::operator * ( float f)
{
  Vector r(*this);
  for (int i = 0; i < size; i++)
    r.data[i] *= f;
  return r;   
}
int main()
{
  float data1[] = { 1, 2, 3 };
  float data2[] = { 3, 4, 5 };

  Vector a(3, data1);
  Vector b(3, data2);
  Vector c = a + b * 4; //This vector c that is a + b * 4
  c.print();
}
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900