-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path33copyconstr.cpp
More file actions
40 lines (37 loc) · 1.05 KB
/
Copy path33copyconstr.cpp
File metadata and controls
40 lines (37 loc) · 1.05 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
#include <iostream>
using namespace std;
class number
{
int a;
public:
number() {a = 0;} //default constructor
number(int x) //parameterized constructor
{
a = x;
}
number(number &ob) //copy constructor
{
a = ob.a; //this takes an class object address as a parameter
cout << "Using copy constructor function!!" << endl; //to indicate that copy constructor is used
}
void show()
{
cout << "The number is " << a << endl;
}
};
int main()
{
//without using a copy constructor, the compiler will provide a self made constructor to copy the data
//so the program will operate without any hinderance
number p, q, r(6);
p.show();
q.show();
r.show();
number s(r), t; //here s is declared with r as a parameter passed, so copy constructor will be fetched due to overloading
s.show();
t = r; //here no copy constructor is fetched as r is assigned to t manually
t.show();
number u = r; //only upon declaration, constructor is fetched
u.show();
return 0;
}