ood
C++

Vector Addition and Subtraction

This is the solution for 2010 fall 4(b), 2007 Spring 4(b), 2010 Spring 3(b OR) PU BCA

#include<iostream>

using namespace std;

class vector

{

float x;//real part

float y;//imaginary part

public:

vector(){}//constuuctor 1

vector(float i,float j)

{
x=i;
y=j;
}

vector operator+(vector);

vector operator-(vector);

void display(void);

void display2(void);

};

vector vector::operator+(vector c)

{

vector temp;

temp.x=x+c.x;

temp.y=y+c.y;

return(temp);

}

void vector::display(void)

{ cout<<x<<“+j”<<y<<“\n”; }

vector vector::operator-(vector c)

{

vector temp;

temp.x=x-c.x;

temp.y=y-c.y;

return(temp);

}

void vector::display2(void)

{
cout<<x<<“-j”<<y<<“\n”;
}

int main()

{

vector v1,v2,v3;

v1=vector(2.5,3.5);

v2=vector(1.6,2.7);

v3=v1+v2;

cout<<“v1= “;

v1.display();

cout<<“v2= “;

v2.display();

cout<<“v2= “;

v3.display();

cout<<“\n\n”;

v3=v1-v2;

cout<<“v1= “;

v1.display2();

cout<<“v2= “;

v2.display2();

cout<<“v3= “;

v3.display2();

return 0;

}

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.