-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path31constroverload.cpp
More file actions
43 lines (38 loc) · 823 Bytes
/
Copy path31constroverload.cpp
File metadata and controls
43 lines (38 loc) · 823 Bytes
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
#include<iostream>
using namespace std;
class complex
{
int a, b;
public:
complex(int x, int y)
{
a = x;
b = y;
}
complex(int x)
{
a = x;
b = 0;
}
void display()
{
cout<<"z = "<<a<<" + "<<b<<"i"<<endl;
}
};
int main()
{
complex c1(6, 3);
c1.display();
complex c2(1);
c2.display();
//this is constructor overloading
//here we created 2 constructor of same class that takes different number of parameters and based how many parameters we pass
// they will be automatically be assigned to that particular object
//passing default arguments is same as functions where a formal parameter is set to a constant value
/*complex(int x, int y = 0)
{
a = x;
b = y;
}*/
return 0;
}