Class Vector coderinme

Class Vector coderinme

write a class vector to represent an integer array v[N] (assume N=10).
Include (i) operator functions to overload input (>>) and output (<>v1; can be used to read N values in the array v[N] of
v1 vector object. Similarly, cout<<v1; can be used to output v[N] of object v1, (ii)
operator function to overload + operator that finds sum of two vector objects,
and (iii) other necessary member functions. Write a C++ program to use this
class reads data into two vector objects and finds their sum.
Solution:

#include<iostream.h>
#include<conio.h>
const int N=10;
class vector
{
int v[N];
public:
vector();
vector(int *x);
vector operator+(vector v1);
friend istream & operator>>(istream &din,vector &b);
friend ostream & operator<<(ostream &dout,vector &b);
};
vector::vector()
{
for(int i=0;i<N;i++)
v[i]=0;
}
vector::vector(int *x)
{
for(int i=0;i<N;i++)
v[i]=x[i];
}
istream & operator>>(istream &din,vector &b)
{
cout<<"enter 10 values in the Vector:"<<endl;
for(int i=0;i<N;i++)
{
din>>b.v[i];
}
return din;
}
ostream & operator<<(ostream &dout,vector &b)
{
for(int i=0;i<N;i++)
{
dout<<b.v[i]<<",";
}
return dout;
}
vector vector:: operator+(vector v1)
{
vector sum;
for(int i=0;i<N;i++)
{
sum.v[i]=v[i]+v1.v[i];
}
return sum;
}
int main()
{
vector v1,v2,v3;
cin>>v1;
cin>>v2;
v3=v1+v2;
cout<<endl<<"The two Vectors are"<<endl;
cout<<endl<<"v1:=["<<v1<<"]"<<endl;
cout<<endl<<"v2:=["<<v2<<"]"<<endl;
cout<<endl<<"Sum of two vectors are"<<endl;
cout<<endl<<"v3:=["<<v3<<"]"<<endl;
getch();
return 0;
}

For more programs in C++, visit our C++ Coder Archives

Class Vector coderinme

All rights reserved. No part of this Post may be copied, distributed, or transmitted in any form or by any means, without the prior written permission of the website admin, except in the case of brief quotations embodied in critical reviews and certain other noncommercial uses permitted by copyright law. For permission requests, write to the owner, addressed “Attention: Permissions Coordinator,” to the admin @coderinme

Leave a reply:

Your email address will not be published.