-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcpp_note
243 lines (166 loc) · 8.92 KB
/
cpp_note
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
URL:
-----
https://www.youtube.com/watch?v=LZFoktwiars&list=PL0gIV7t6l2iIsR55zsSgeiOw9Bd_IUTbY
Topic Cover:
-------------
https://www.youtube.com/watch?v=pX6LufLso2M&list=PL0gIV7t6l2iIsR55zsSgeiOw9Bd_IUTbY&index=10
https://www.youtube.com/watch?v=6ki_W7cXdM0&list=PL0gIV7t6l2iIsR55zsSgeiOw9Bd_IUTbY&index=22
https://www.youtube.com/watch?v=wtuks_f3vP4&list=PL0gIV7t6l2iIsR55zsSgeiOw9Bd_IUTbY&index=24
https://www.youtube.com/watch?v=jXTTOZUT1iU&list=PL0gIV7t6l2iIsR55zsSgeiOw9Bd_IUTbY&index=27
https://www.youtube.com/watch?v=apdzg2A90CI&list=PL0gIV7t6l2iIsR55zsSgeiOw9Bd_IUTbY&index=30
https://www.youtube.com/watch?v=gh51t49tKUQ&list=PL0gIV7t6l2iIsR55zsSgeiOw9Bd_IUTbY&index=33
https://www.youtube.com/watch?v=DOhMUTHrdJI&list=PL0gIV7t6l2iIsR55zsSgeiOw9Bd_IUTbY&index=36
lecture going to start: 9, 10, 12, 14, 16, 21, 23, 25, 29, 32, 35
//////////////////////////////////////////////////////////////////////////////////////////
Define vector
--------------
#define MAX 10
vector<int> arr(MAX) : int -> Type of array. arr --> Name of array MAX --> Size of array define as a macro
1> Diff between array and vector?
Array size is fixed and decide at compile time. But vector size is not fixed size it can be change with "arr.resize(20)".
2> Inline function can not be recursive.
Reference:
-----------
1> This is the alias of a variable.
int a = 10; Below Both the case a and b address is same.
int &b = a;
int &c; => Error becz there is no varibale for alias.
2> int &j = 6 ==> Error becz we can not refer to constant But we can refer to const int &j = 6
3> int &j = i + j ===> Error above resons const int &j = i + j
4> NEVER EVER RETURN LOCAL VARIABLE REFERENCE.
Default Paramter and Function Overloading:
-------------------------------------------
1> If we are not pasing any parameter at the time of call system will take default value mention in function declaration.
2> i> All the parameter right of default parameter should be defaulted.
Ex: void foo(int, double = 1, char *) // Error becz char * shuld be defaulted
void foo(int, double = 1, char * = NULL) //OK
ii> Default argument can not be redefind.
Ex: void foo(int, double = 1, char * = NULL) //OK
void foo(int, double = 2, char * = NULL) //Error redefind
iii> All non-default parameter should mention at the time of function call.
3> Always use default parameter in headerfile.
4> Function Overloading is called static Polymerprisim
5> Function Binding happen at compile time in function Overloading.
6> In Function Overloading different return type in overloading function is not possible.
Operator Overloading:
----------------------
1> Example: a + b ===> operator+(a, b) ==> This defination type always user define not int, char, double etc
c = a + b ===> operator= (c, operator+(a, b))
2> New operator symbol can not be oveload. Only existing opeartor can overload.
3> prefix operator Mytype& operator++(Mytype& s1)
postfix operator Mytype& operator++(Mytype& s1, int) ===> No need to pass any parameter in int place. this use to identyfy compiler.
4> ::, ., *, sizeof, ternary can not be overloaded
5> -> this overloading should return pointer. LATER WILL DISCUSS IN SMART POINTER
Dynamic Memory Allocation:
---------------------------
1> int *p = new int (5) ==> Here new will allocate memory and initialize 5 to particular "p"\
If we don't want initilization only "int *p = new int" is sufficient.
2> new and delete are operator. malloc is function.
3> Array memory allocation: int *a = new int [3]; delete [] a;
4> If we try to overload operator new below is the defination need to be follow.
void *operator new(size_t n)
{
--------
}
Class and Object:
------------------
1> Object is the instances of class.
Access Specifier:
------------------
1> This is use for hiding information. This is very strong concept in cpp.
2> Default class datamembers are private.
3> public is use for interface and private is use for implementation.
4> Mostly ".h" file is called interface and every body needs to be known. Implementation will go in different file .cpp. And application only use ".h" file which is called interface.
Constructure, Destructure:
---------------------------
1> Object come to life fom execution of constructer body and destroy from destoctor sequnce.
Copy Constructure:
------------------
1> Data members initialization happen according to the order it define inside class not the order it intialize in constructure.
2> PLEASE NOTE ABOVE CONDITION BECZ IT WILL VERY HELPFUL FOR OUTPUT. SO MAIN RULE IS ORDER OF DATA MEMEBR PRESENT INSIDE CLASS AND AT THE TIME OF INITILIZATION IN CONSTRUCTURE ORDER SHOULD BE SAME.
3> For more info check sample program file name "const_order.cpp"
4> And the Destructure order is equaly opposite order.
5> Syntax of Copy constructure: Complex:: Complex(const Complex &)
6> Invokation Of Copy Constructure: c2(c1) or c3 = c2;
7> Why Copy Constructure Required?
i> At the time of user define(class created by user) call-by-value this is required becz we can't pass value without copy.
ii>
8> We can not pass object as call-by-value in copy constructure.
9> Copy Assignment Operator ==> This required when 2 or more than objects are already define
syntax: invoking c2 = c3;
Defination: Complex& operator=(const Complex& c)
{
if (this != &c)
{
free(c); // This is required for avoide memory leak. becz c will not frred up once this function returnd.
// Procede now becz this condition will help for self assignmate object. Ex c1 = c1;
}
-------------
}
Why need & at the time of return? Because chain assignment is possible c1 = c2 = c3.
10> If user does not provide copy consructure compile will provide free copy constructure. And it is just bit copy. Always advise to provide copy consructure.
11> SHALLOW COPY COPIES THE POINTER BUT DEEP COPY ONLY COPY POINTED VALUE.
Const-ness:
-----------
1> User define object can be mention as const. If we make this const the member of object can not be change.
Ex:- const Myclass * const this; ==> Const pointer to const object
Myclass * const this; =====> Const pointer to Non-constant object
2> To access member using const object we need to provide const member functions.
Ex:- void print() const
{
--------------
}
3> If datamembers are const it can not change at all. The value is assign at the time of initialization.
4> mutable(It means it can change) is use if object is declare as const in that case only mutable data member can change but all other data member can not be change.
5> mutable is only applicable for data member not variable.
6> Reference data member can not be declare as mutable.
7> static and const data member can not be declare as mutable.
8> Syntax: class x {
mutable int a;
};
Static Members:
----------------
1> static data member is only created one and share commonly all instances.
2>static data member is constructed before main() function starts and destructed after main () ends.
3> static member function:
i> Does not have this pointer.
ii> can not access non-static data members.
iii> can not invoke non-static memberfunction.
iv> it will access using class name :: operator.
v> Can not declare as const becz it does not have this pointer.
Friend Function:
-----------------
1> A friend function of a class has access to private and protected data member of class.
2> Must have to include prototype inside the scope of class mention key word friend.
3> This feature required when i want to access 2 or more different classes private data member.
Operator Overloading For User define:
--------------------------------------
1> Post increment will use retun by value.
Basic implementation:
Myclass operator++(int)
{
Myclass t(data);// Here data has to be assign to temp varibale.
// Any opeartion //
return t;
}
2> Pre increment is retun by reference and ii always return same object means this pointer
Basic implementation:
Myclass& operator++()
{
// Any Operation //
return *this;// Here this has to be return this pointer
}
3> explicit is use befpre member function to inform compiler to not to do any casting.
Overloading of cout and cin.
-----------------------------
1>
Complex d1, d2;
cout << d1 << d2;
1>The signature of operator global function
ostream& operator<< (ostream& os, const Complex &a);
2>Member function in ostream
ostream& ostream::operator<< (const complex &a);
3>Member function in Complex
ostream& Complex::operator<< (ostream& os);
https://www.youtube.com/watch?v=JPtXZblI1sg&list=PL0gIV7t6l2iIsR55zsSgeiOw9Bd_IUTbY&index=35 ==> URL is very much help full
THIS COUT AND CIN IS NOT POSSIBLE TO OVELOAD. ONLY PT 1 IS POSSIBLE BUT IT WILL NOT ACCORDING TO THE ENCAPSULATION SO BETTER USE FRIEND FUNCTION. PT 2 IS NOT POSSIBLE BECZ OSTREAM IS STANDARD C++ LIB OBJECT WE CAN NOT ADD ANYTHING. IN PT 3 WILL WORK IF I WILL RIGHT (D1 << COUT) BUT IT WILL NOT LOOKES GOOD BECZ THIS IS NOT THE STANDARD C++ SYNTAX FOR COUT.