
2.12 | Data Structures and Algorithms Using C++
}
};
void main()
{
vector a(10,20,30),b(1,2,3),c;
clrscr();
cout<<“\nVector A:”;
a.show();
cout<<“\nVector B:”;
b.show();
c=a-b;
cout<<“\n A-B:”;
c.show();
}
Output
Vector A:10i+20j+30k
Vector B:1i+2j+3k
A-B:9i+18j+27k
In Program 2.7, the
vector
is de ned as a class with
x,y,z
as local variables. To solve the
vector
subtrac-
tion problem, the minus (
–
) operator cannot be applied on the objects of the
vector
class. By overloading the
minus operator in the
vector operator-(vector v)
form, the subtraction problem of vector can be solved.
e return type of the operator–here is a
vector
type.
2.3.3 O