-
Notifications
You must be signed in to change notification settings - Fork 0
/
NotesCPPDayTwo.txt
93 lines (48 loc) · 1.75 KB
/
NotesCPPDayTwo.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
Day 2:
------
Structures Continued...
C Style structures
C Style structures using reference variables
C++ Style structures by bringing data and functions together
1. Using arrays
class Array{
int arr[SIZE];
int size;
public:
void initializeArr();
void fillArray(int first);
void printArray();
};
2. Using Linked List
struct Node{
int data;
Node *next;
};
class LinkedList{
Node *first;
public:
void initialize();
void addAtBeg();
void addAtEnd();
void addAtPos(int);
void disp();
};
////////////////////////////////////////////////////////
There are special functions that can be used for initialization and de-initialization called as constructors and destructor
Constructors:
-------------
Constructors are special functions which are automatically called when an object/variable/instance of a class is created
These constructors can take argument, because it takes arguments the signature of these functions can vary (That means) these functions can be overloaded
Destructor is a special function which gets called automatically when the scope of the variable/instance is gone.
Destructor does not take any arguments, so it cannot be overloaded in a class
What is special about constructors:
===================================
There are 3 points that make constructors special
1. Same name as class name
2. It does not have any return value (not even void)
3. The are automatically called.
Destructor:
-----------
1. name starts with ~ClassName
2. no return types
3. automatically called