forked from ManishShah120/Oops_Wi2_Cplusplus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path023OverloadingOperatorUsingFriendPr7_3.cpp
76 lines (62 loc) · 1.33 KB
/
023OverloadingOperatorUsingFriendPr7_3.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# include<iostream>
using namespace std;
const int size = 3;
class vector
{
int v[size];
public:
vector(); //constructs null vector
vector(int *x); //constructs vector from array
friend vector operator * (int a, vector b); //friend 1
friend vector operator * (vector b, int a); //friend 2
friend istream & operator >> (istream &, vector &);
friend ostream & operator << (ostream &, vector &);
};
vector :: vector()
{
for(int i=0; i<size;i++)
v[i] = 0;
}
vector :: vector(int *x)
{
for(int i=0; i<size;i++)
v[i] = x[i];
}
vector operator *(int a, vector b)
{
vector c;
for(int i=0; i<size;i++)
c.v[i] = b.v[i]*a;
return c;
}
istream & operator >> (istream &din, vector &b)
{
for(int i=0;i<size;i++)
din >> b.v[i];
return(din);
}
ostream & operator << (ostream &dout, vector &b)
{
dout << "(" << b.v[0];
for(int i =1;i<size;i++)
dout << ", "<< b.v[i];
dout << ")";
return(dout);
}
int x[size] = {2, 4 ,6};
int main()
{
vector m; //invokes constructor 1
vector n =x; //invokes constructor 2
cout << "Enter elements of vector m "<< "\n";
cin >> m; //invokes operator>>() functions
cout << "\n";
cout<< "m= "<< m << "\n";
vector p,q;
p = 2 * m; //invokes friend 1
q = n * 2; //invokes friend 2
cout << "\n";
cout<< "p = "<< p << "\n"; //invokes operator<<()
cout<< "q = "<< q << "\n";
return 0;
}