-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConstructor-example.cpp
More file actions
104 lines (89 loc) · 1.93 KB
/
Copy pathConstructor-example.cpp
File metadata and controls
104 lines (89 loc) · 1.93 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
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
103
104
#include<bits/stdc++.h>
#include<iostream>
using namespace std;
class Constructor
{
int a,b;
public:
Constructor() //default constructor
{
a=0;
b=0;
}
Constructor(int x , int y) //parametrized constructor
{
a = x;
b = y;
}
Constructor( Constructor &old_c1,int p) //Copy constructor
{
a = old_c1.a; //a of c1 is copied to new object's a
b = p; //b of c1 is copied to new object's b
}
void printdata()
{
cout<<a<<endl;
cout<<b<<endl<<endl;
}
};
int main()
{
int p;
Constructor c;
c.printdata();
Constructor c1(10,20);
c1.printdata();
cout<<"Enter some value for b : ";
cin>>p;
Constructor c3 = c1; //Copy Constructor calling
c3.printdata();
//c3 is calling the copy constructor made by compiler as per the c1
Constructor c4 (c1,p); //other way to use copy constructor
c4.printdata();
}
/*
#include <iostream>
using namespace std;
class Rational
{
private:
int numerator,denominator;
public:
Rational()
{
numerator=0;
denominator=0;
}
Rational(int a,int b)
{
numerator=a;
denominator=b;
if(b==0)
{
numerator=0;
denominator=0;
cout<<"Not Defined"<<endl;
}
}
void check()
{
if(numerator > denominator)
cout<<"Greater number is : "<<numerator;
else if(denominator > numerator)
cout<<"Greater number is : "<<denominator;
else if(numerator == 0 && denominator==0)
cout<<endl;
else
cout<<"Both are same\n";
}
};
int main()
{
int a,b;
cout<<"Enter two numbers"<<endl;
cin>>a>>b;
Rational r1(a,b);
r1.check();
return 0;
}
*/