-
Notifications
You must be signed in to change notification settings - Fork 0
/
NotesCPPDayThree.txt
143 lines (89 loc) · 3.14 KB
/
NotesCPPDayThree.txt
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
Day 3:
======
Different types of members in a class
Data Members:
-------------
Class Member:
one copy per class ---> using static keyword
Object/Instance/variable Member:
Each object has its own copy
normal data members without any keyword
const data members using const keyword
mutable data members using mutable keyword
Let me give you an example
Example: class Employee
Now Try This....
class Array{
int arr[SIZE];
const int size;
public:
Array(int = 0);
Array(int sz, int first);
void fillArray(int first);
void printArray();
};
After this :
------------
const member functions and const objects
mutable and static members
static members of a class are referred to class members
One copy Per class
Static member functions can access only static data members of the class
Static member functions cannot access instance members
Syntax:
static returnType funName(arguments){
}
dynamic memory allocation in C++
Heap based objects/variables allocated dynamically
C style:
--------
In C we used stdlib.h which has functions like malloc, calloc, realloc and free functions for allocating memory dynamically
C++ style:
==========
Remember(always) all Valid C code is a C++ Code!. In addition to the above mentioned functions C++ provides operators new and delete for dynamic memory alocation.
C Style:
int *iPtr = malloc(sizeof(int));
*iPtr = 10;
free(iPtr); ---> For deleting / removing the memory
C++ Style:
int *iPtr = new int(10);
For storing individual elements (on data only)
delete iPtr;
========================================================
For collections of elements:
C Style:
int size = 100;
int *iPtr = malloc(sizeof(int) * size);
or
int *iPtr = calloc(sizeof(int), size);
free(iPtr);
C++ Style:
int size=100;
int *iPtr = new int[size];
delete[] iPtr;
Implement this!....
class Array{
int *arr; //a pointer variable
const int size;
public:
Array(int = 0);
Array(int sz, int first);
void fillArray(int first);
void printArray();
};
For any Class in C++,
your compiler provides necessary sytax support for
1. default object creation (Default Constructor)
Array obj;
2. Support copy sematics (Copy Constructor)
Array objB = objA;
3. Supports copy assignment (Assignment operator overload)
objB = objA;
4. Support basic destructors (Destructor)
These above functionalities are provided for every class created in C++.
Your C++ compiler generates constructors and destructor IF NECESSARY(when is this case considered).
Reading on this...
Deep Copy and Shallow Copy...
=============================
We will have discussion tomorrow
Introduction to Operator overloading