Click here to Skip to main content
15,348,881 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hello, please, help me someone.

I wrote a simple program for creating and editing vector. It is necessary to make an imput of these two vectors. As you see, in my code, vectors are set by default:
vect x(5,6,7,8,9),y(4,3,2,1,7);


#include <conio.h>
#include <iostream.h>

class vect {
public:
   vect(int=0,int=0,int=0,int=0,int=0);   
   vect operator+(const vect &) const;   
   vect operator-(const vect &) const;
   vect operator*(const vect &); 
   vect operator*(const int &); 
   
   void print() const;
   private:
   int a,b,c,d,e;
};
vect::vect(int a1,int b1,int c1,int d1,int e1)
{
   a = a1;
   b = b1;
   c = c1;
   d = d1;
   e = e1;
}
vect vect::operator+(const vect &operand2) const
{
   vect sum;
   sum.a = a + operand2.a;
   sum.b = b + operand2.b;
   sum.c = c + operand2.c;
   sum.d = d + operand2.d;
   sum.e = e + operand2.e;
   return sum;
}
vect vect::operator-(const vect &operand2) const
{
   vect diff;
   diff.a = a - operand2.a;
   diff.b = b - operand2.b;
   diff.c = c - operand2.c;
   diff.d = d - operand2.d;
   diff.e = e - operand2.e;
   return diff;
}
vect vect::operator*(const vect &operand2)
{
   vect proizv;
   proizv.a = a * operand2.a;
   proizv.b = b * operand2.b;
   proizv.c = c * operand2.c;
   proizv.d = d * operand2.d;
   proizv.e = e * operand2.e;
   return proizv;
}
vect vect::operator*(const int &operand2)
{
   vect proizv;
   proizv.a = a * operand2;
   proizv.b = b * operand2;
   proizv.c = c * operand2;
   proizv.d = d * operand2;
   proizv.e = e * operand2;
   return proizv;
}
void vect::print() const
{
   cout<<'('<<a<<", "<<b<<", "<<c<<", "<<d<<", "<<e<<')';
}
int main(int argc, char* argv[])
{
   vect x(5,6,7,8,9),y(4,3,2,1,7);
   vect z;
   int scl = 8;
   cout<<"\n Vector 1:   ";
   x.print();
   cout<<"\n Vector 2:   ";
   y.print();
   z = x + y;
   cout<<"\n Slozhenie:   ";
   z.print();
   z = x - y;
   cout<<"\n Vichitanie:  ";
   z.print();
   z = x * y;
   cout<<"\n Vector*Vector:   ";
   z.print();
   z = x * scl;
   cout<<"\n Scalar1:   ";
   z.print();
   z = y * scl;
   cout<<"\n Scalar2:   ";
   z.print();
   getch();
   return 0;
}
Posted
Updated 8-Sep-10 3:33am
v2
Comments
LittleYellowBird 8-Sep-10 9:02am
   
Hi, I guess when you say 'imput' you mean 'input'? Even so I'm not clesr what your question is. Perhaps someone else will understand, but it maybe worth re-wording your question to make it easier to understand. Just a thought. :)
Dalek Dave 8-Sep-10 11:47am
   
I fixed the Title.

I guess you mean something like:
    integer a,b,c,d,e;

    cout << "Enter vector 1 values: " << endl;
    cin >> a;
    cin >> b;
    cin >> c;
    cin >> d;
    cin >> e;
   vect x(a, b, c, d, e);
// repeat for vector y

Obviously not very sophisticated but I trust you get the idea.
   
Comments
cccfff777 9-Sep-10 5:04am
   
That is what I thought too. Can someone tell if this is what he meant? I still can not guess what he really wants.
Have a look at how you implement the insertion and extraction operators, it'll make your code a lot easier to read. For example if you implement operator<< and operator>> you could end up writing something like:

int main()
{
    std::cout << "Please enter 10 integers, white space delimited to initialise my vectors: ";

    vect a, b;
    std::cin >> a >> b;

    std::cout << "Sum of your vectors is: " << a + b << std::endl;
    std::cout << "Difference of your vectors is: " << a - b << std::endl;
}


Now there's not a lot of error handling there but it shows how much simpler your code could be.

Whatever text you're using to learn C++ from should have some more details on how to overload operators. Have a gawp there give it a go and if you're having problems ask a more specific questions.

Cheers,

Ash
   

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



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