-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpractice.cpp
More file actions
144 lines (127 loc) · 2.64 KB
/
practice.cpp
File metadata and controls
144 lines (127 loc) · 2.64 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#include <bits/stdc++.h>
using namespace std;
/*
------------------------------
Pointer to structure
*/
// struct Rectangle{j
// int length;
// int breadth;
// }*R;
// int main(){
// R=new Rectangle;
// R->length=10;
// R->breadth=14;
// cout<<R->length<<" "<<R->breadth;
// }
// struct Rectangle{
// int length;
// int breadth;
// }R;
// int main(){
// R.length=10;
// R.breadth=14;
// cout<<R.length<<" "<<R.breadth;
// }
/*
-----------------------------------
Functions
1.Pass by value
2.Pass by address
3.Pass by reference (only in cpp)
*/
// 1.Pass by value
// void swap(int x,int y){
// int temp=x;
// x=y;
// y=temp;
// cout<<x<<" "<<y<<"\n";
// }
// int main(){
// int a=1,b=2;
// cout<<a<<" "<<b<<"\n";
// swap(a,b);
// cout<<a<<" "<<b<<"\n";
// }
// 2.Pass by address
// void swap(int *x,int *y){
// int temp=*x;
// *x=*y;
// *y=temp;
// // cout<<x<<" "<<y<<"\n";
// }
// int main(){
// int a=1,b=2;
// cout<<a<<" "<<b<<"\n";
// swap(&a,&b);
// cout<<a<<" "<<b<<"\n";
// }
// 3.Pass by reference (only in cpp)
// void swap(int &x,int &y){
// int temp=x;
// x=y;
// y=temp;
// // cout<<x<<" "<<y<<"\n";
// }
// int main(){
// int a=1,b=2;
// cout<<a<<" "<<b<<"\n";
// swap(a,b);
// cout<<a<<" "<<b<<"\n";
// }
/*
--------------------------------------------
Passing Structure as function parameter
*/
// struct rectangle{
// int length;
// int breadth;
// };
// //Parameter is passed as reference in void initialize and void changelength
// // coz we need to initialize/change structure components
// void initialize(struct rectangle *r,int l,int b){
// r->length=l;
// r->breadth=b;
// }
// int area(struct rectangle r){
// return r.length*r.breadth;
// }
// void changelength(struct rectangle *r,int l){
// r->length=l;
// }
// int main(){
// struct rectangle r;
// initialize(&r,2,5);
// cout<<r.length<<" "<<r.breadth<<"\n";
// cout<<area(r)<<"\n";
// changelength(&r,10);
// cout<<r.length<<" "<<r.breadth<<"\n";
// }
/*
Above structure as class
*/
class Rectangle{
private:
int length;
int breadth;
public:
// void initialize(int l,int b){
// length=l;
// breadth=b;
// }
// Instead of the above initlialize function we can use constructor (which is same as class name)
Rectangle(int l,int b){
length=l;
breadth=b;
}
int area(){
return length*breadth;
}
void changelength(int l){
length=l;
}
};
int main(){
Rectangle r(10,5);
cout<<r.area()<<" ";
}