-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConstructorOverloading.cpp
More file actions
55 lines (45 loc) · 1.11 KB
/
Copy pathConstructorOverloading.cpp
File metadata and controls
55 lines (45 loc) · 1.11 KB
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
#include<iostream>
using namespace std;
class Complex
{
int a,b;
public:
Complex()//default constructor
{
a=0;
b=0;
}
Complex(int x)//parameterized constructor
{
a=x;
b=0;
}
Complex(int x,int y)//parameterized constructor
{
a=x;
b=y;
}
void PrintComplex()
{
cout<<a<<"+"<<b<<"i"<<endl;
}
};
int main()
{
Complex c1;
c1.PrintComplex();
Complex c2(1);
c2.PrintComplex();
Complex c3(4,6);
c3.PrintComplex();
return 0;
}
/*
Constructor overloading is a concept in which one class can have
multiple constructors with different parameters.
The main thing to note here is that the constructors
will run according to the arguments for example
if a program consists of 3 constructors with 0, 1, and 2 arguments,
so if we pass 1 argument to the constructor the compiler will automatically run
the constructor which is taking 1 argument.
*/