-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path19_array_of_objects.cpp
102 lines (101 loc) · 1.93 KB
/
19_array_of_objects.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
//array of objects and passing objects as function
// example 1 --> array of objects
/*
#include <iostream>
using namespace std;
class employee {
int id;
float salary;
public:
void getinfo()
{
cout<<"enter id "<<endl;
cin>>id;
cout<<"enter salary "<<endl;
cin>>salary;
}
void showinfo()
{
cout<<"the id is :"<<id<<endl;
cout<<"the salary is :"<<salary<<endl;
}
};
int main()
{
employee e[3];
for (int i = 0; i <3; i++) {
cout<<"enter details of "<<i+1<<" employee"<<endl;
e[i].getinfo();
}
for (int i = 0; i <3; i++) {
cout<<"details of "<<i+1<<" employee are "<<endl;
e[i].showinfo();
}
return 0;
}
*/
// example 2--> passing objects as functions
/*
#include <iostream>
using namespace std;
class complex{
int a,b;
public:
void getnumber(int x, int y)
{
a=x,b=y;
}
void displaynumber()
{
cout<<"complex no. is "<<a<<" + "<<b<<"i"<<endl;
}
void sum (complex u, complex v)//passing objects as function
{
cout<<"sum :"<<endl;
a=u.a+v.a;
b=u.b+v.b;
}
};
int main() {
complex c1,c2,c3;
c1.getnumber(3,4);
c1.displaynumber();
c2.getnumber(4,5);
c2.displaynumber();
c3.sum(c1,c2);
c3.displaynumber();
return 0;
}
*/
// same previous program to take info from user
#include <iostream>
using namespace std;
class complex{
int a,b;
public:
void getnumber()
{
cout<<"enter values of a and b"<<endl;
cin>>a>>b;
}
void displaynumber()
{
cout<<"complex no. is "<<a<<" + "<<b<<"i"<<endl;
}
void sum (complex u, complex v)//passing objects as function
{
cout<<"sum :"<<endl;
a=u.a+v.a;
b=u.b+v.b;
}
};
int main() {
complex c1,c2,c3;
c1.getnumber();
c1.displaynumber();
c2.getnumber();
c2.displaynumber();
c3.sum(c1,c2);
c3.displaynumber();
return 0;
}