C++/c++

Advertisement


Question
QUESTION: could u pls help mi,i realli need your help.i am trying to do a simple addition of using class vector.i hope u could guide mi.i'm nt sure whether i'm doing it right anot.i will be so appericated for all the help frm u.

example:A vector can be represented in Cartesian coordinates (a, b, c).
Suppose vector X is (a1, b1, c1) and Y is (a2, b2, c2).
Addition:X + Y = (a1+a2, b1+b2, c1+c2).

example result:
Inputs
A(5,6,7)
B(3,8,2)
Outputs A + B (8,14,9)

code:
class myVector
{
  int x,y,z;
   public:
      myVector operator+(const myVector &mv)const;
};

myVector myVector::operator +(const myVector &mv)const  //Addition
{
  myVector mv;
  mv.x = x + mv.x;
  mv.y = y + mv.y;
  mv.z = z + mv.z;
  return mv;
}

//main program to test the class
int main()
{

  myVector A,B,C;
  
  cout << "Enter first vector, format (a,b,c): ";
  cin >> A;
  
  cout << "Enter second vector, format (a,b,c): ";
  cin >> B;
         
  C = A + B;
  cout<<"Addition A+B = "<< C << endl;

return 0;
}



ANSWER: Hello Lisa.

That's pretty good. But you need a few changes to make it compile. In C++ you cannot have a variable in a function with the same name as a parameter to that function. Where you have:

myVector myVector::operator +(const myVector &mv)const  //Addition
{
  myVector mv;

You need to change the name of the parameter or the vector in the function.

Try this instead:

myVector myVector::operator +(const myVector &mv)const  //Addition
{
   myVector result;
   result.x = x + mv.x;
   result.y = y + mv.y;
   result.z = z + mv.z;
   return result;
}

In other respects, the addition function is fine.

You should create a constructor myVector so that you can initialize it. For example insert this constructor into myVector:

myVector(int xPart, int yPart, int zPart) { x = xPart; y = yPart; z = zPart; }

If you insert the above constructor, you should also have a default constructor. The compiler won't give you one automatically if you define the one above. Here is a default one:

myVector() { x = y = z = 0; }


Here is a working program without user input or output:

#include <iostream>

using namespace std;

class myVector
{
   int x,y,z;
public:
   myVector() { x = y = z = 0; }
   myVector(int xPart, int yPart, int zPart) { x = xPart; y = yPart; z = zPart; }

   myVector operator+(const myVector &mv)const;
};

myVector myVector::operator +(const myVector &mv)const  //Addition
{
   myVector result;
   result.x = x + mv.x;
   result.y = y + mv.y;
   result.z = z + mv.z;
   return result;
}

//main program to test the class
int main()
{

   myVector v1(1,2,3);
   myVector v2(3,2,1);

   myVector sum = v1 + v2;
}


The only thing left is accepting user input. You cannot send cin directly into a myVector unless you define a special function to do it. So you cannot do:
myVector A;
cin >> A;


Try this instead:
   int x,y,z;
   cout << "Enter first vector, format a b c ";
   cin >> x >> y >> z;
   myVector A(x,y,z);

Finally, if you want to send a vector to cout, you need to define a special function to do it.
You cannot just say:
cout << A;

Here is the function:
ostream& operator<<(ostream& out, const myVector& vec)
{
   out << '(' << vec.x << ',' << vec.y << ',' << vec.z << ')';
   return out;
}

The function cannot be a member of myVector. The function needs access to the vector elements x,y, and z but they are private so you have three choices:
1) make the function a friend of myVector,
or
2) provide accessor functions,
or
3) make x,y, and z public.
The last option is the worst. I'll make the function a friend with this written in the myVector class:
friend ostream& operator<<(ostream& out, const myVector& vec);

Now here is the completed program:

#include <iostream>

using namespace std;

class myVector
{
   int x,y,z;
public:
   myVector() { x = y = z = 0; }
   myVector(int xPart, int yPart, int zPart) { x = xPart; y = yPart; z = zPart; }

   myVector operator+(const myVector &mv)const;

   friend ostream& operator<<(ostream& out, const myVector& vec);
};

myVector myVector::operator +(const myVector &mv)const  //Addition
{
   myVector result;
   result.x = x + mv.x;
   result.y = y + mv.y;
   result.z = z + mv.z;
   return result;
}

ostream& operator<<(ostream& out, const myVector& vec)
{
   out << '(' << vec.x << ',' << vec.y << ',' << vec.z << ')';
   return out;
}

//main program to test the class
int main()
{

   myVector v1(1,2,3);
   myVector v2(3,2,1);

   myVector sum = v1 + v2;
   
   int x,y,z;
   cout << "Enter first vector, format a b c ";
   cin >> x >> y >> z;
   myVector A(x,y,z);


   cout << "Enter second vector, format a b c ";
   cin >> x >> y >> z;
   myVector B(x,y,z);


   myVector C = A + B;
   cout<<"Addition A+B = "<< C << endl;

   return 0;
}


---------- FOLLOW-UP ----------

QUESTION: thankz veri much for answering my qns.i realli appericated for all the help frm u.
but tis is the result i gt wen i compile the program:
Enter first vector, format a b c: 5,6,7
Enter second vector, format a b c:
Addition A+B = (10,4587152,4587080)

the result i gt is nt correct.i onli gt to enter the first vector,i cnnot gt to enter the second vector.it added up wrong.could u pls tell mi wat i should do to correct it.
the result i gt should be tis:
example result:
Inputs:       A(5,6,7)
         B(3,8,2)
Outputs: A + B (8,14,9)


ANSWER: That's very strange. I cannot reproduce the problem. No matter what I do, it allows me to enter both vectors. Please use copy/paste to copy this main program into yours. I wonder if you made some typing error earlier. Perhaps you are missing the second cin. If you cannot fix the problem, please re-post your entire program using copy/paste.

int main()
{
  int x,y,z;

  cout << "Enter first vector, format a b c ";
  cin >> x >> y >> z;
  myVector A(x,y,z);


  cout << "Enter second vector, format a b c ";
  cin >> x >> y >> z;
  myVector B(x,y,z);


  myVector C = A + B;
  cout<<"Addition A+B = "<< C << endl;

  return 0;
}


Here are samples of my output. I notice that I don't even have to provide all the numbers on the same line.

*** SAMPLE 1 ***

Enter first vector, format a b c
1
2
3
Enter second vector, format a b c
4
5
6
Addition A+B = (5,7,9)

*** SAMPLE 2 ***

Enter first vector, format a b c 1 2 3
Enter second vector, format a b c 4 5 6
Addition A+B = (5,7,9)

---------- FOLLOW-UP ----------

QUESTION: thankz veri much for your help,i kw wat is my error already.i hv another problem,could u pls help mi,i dun knw which part of the code is wrong,wen i compile i hv errors.

vectors comparison and incremented:
X == Y if a1==a2, b1==b2 and c1==c2
X > Y is true if a1>a2, b1>b2 and c1>c2
++X will update X as (a1+1, b1+1, c1+1)

expected result:
Inputs:A(5,6,7)
      B(3,8,2)
Outputs:A == B False
       A > B False
       ++A (6,7,8)

code:
bool myVector::operator==(const myVector& mv)
{
  return(x==mv.x && y==mv.y && z==mv.z);     
}

bool myVector::operator>(const myVector& mv)
{
  return(x>mv.x && y>mv.y && z>mv.z);   
}

myVector& myVector::operator++()//prefix
{
  x++;
  y++;
  z++;
  
  return*this;
}
//main program to test the class
int main()
{
if (A == B)
cout << A equals B << endl;
else
cout << A not equal B << endl;
if (A > B)
cout << A greater than B << endl;
else
cout << A not greater than B << endl;
C = ++A;
cout << Prefix ++A = << C << endl;
return 0;
}


Answer
Hello Lisa.

If you have not done so, make sure you have declarations of your new functions in the myVector class.

class myVector
{
       /* other things ... */

  bool operator==(const myVector& mv); /* TODO make const */
  bool operator>(const myVector& mv); /* TODO make const */
  myVector& operator++();//prefix
};

You can make operator== and operator> const as I have suggested here, but don't forget to do it in your function definitions too.

Also the quotes you were using in your main program are not recognized as double quotes. Are you using a programming editor? Here is the main program you can copy/paste. You forgot to declare A,B and C too.

int main()
{
  myVector A(1,2,3);
  myVector B(1,2,3);

  if (A == B)
     cout << "A equals B" << endl;
  else
     cout << "A not equal B" << endl;
  if (A > B)
     cout << "A greater than B" << endl;
  else
     cout << "A not greater than B" << endl;

  myVector C;
  C = ++A;
  cout << "Prefix ++A = " << C << endl;

  return 0;
}

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Zlatko

Expertise

No longer taking questions.

Experience

No longer taking questions.

Education/Credentials
No longer taking questions.

©2016 About.com. All rights reserved.