-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathnewOperator.cpp
68 lines (60 loc) · 1.44 KB
/
newOperator.cpp
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
/*
* Program to understand new and delete operators in C++
*/
#include <iostream>
using namespace std;
class Matrix {
private:
int **matrix;
int rows;
int columns;
public:
Matrix(int r, int c) {
cout << "Creating the matrix of size: " << r << "x" << c << "\n";
rows = r;
columns = c;
// matrix = new int[rows][columns];
matrix = new int*[rows];
for (int i = 0; i < rows; i ++) {
matrix[i] = new int[columns];
}
}
void printMatrix() {
cout << "Printing the matrix: \n";
for (int i = 0; i < rows; i ++) {
for (int j = 0; j < columns; j ++) {
cout << matrix[i][j] << " ";
}
cout << endl;
}
}
};
int main () {
// new operator allocates memory from the heap memory
// and returns the address of the space allocated
// more powerful than malloc in C
// because it constructs the object rather
// than just allocating the memory, that will be clear when you use it with classes
// single integer
int *a = new int;
*a = 10;
cout << "Value pointed by a: " << *a << endl;
// array
int *b = new int[5];
for (int i = 0; i < 5; i ++) {
b[i] = (i + 1);
}
cout << "Array of size 5 allocated dynamically: \n";
for (int i = 0; i < 5; i ++) {
cout << b[i] << " ";
}
cout << endl;
// a new matrix object will also be constructed
// using the default constructor
Matrix *m = new Matrix(3, 4);
// make sure to delete the allocated memory
delete(a);
delete[](b);
delete(m);
return 0;
}