-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path12_overload_subscript.cpp
126 lines (96 loc) · 3.17 KB
/
12_overload_subscript.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
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
// // Overload subscript operator [] that will be useful when we want to check for an index out of bound.
/*
In C++, the use of const after the parenthesis of a member function indicates that the function is a const member function. This means that the function does not modify the state of the object on which it is called. The const qualifier is part of the method's signature and is used to enforce the const-correctness of your code.
Here's an explanation of how const is used in the context of member functions:
Const Member Function:
If a member function is declared as const, it means that the function promises not to modify the state of the object on which it is called. This is particularly useful when you have a constant object or a pointer/reference to a constant object.
Const After Parenthesis:
When you see const after the parenthesis, like in void someFunction() const, it indicates that the member function is a const member function.
class MyClass {
public:
void modifyState() {
// This function can modify the state of the object
}
void readState() const {
// This function is a const member function and cannot modify the state
}
};
*/
// // Header files
#include <iostream>
#include <iomanip>
// // use namespace
using namespace std;
// // define class Array
class Array
{
private:
// // instance member variables
int a[5], size = 5;
public:
// // constructors
Array() {}
// // instance member function to input matrix
void inputArray()
{
for (int i = 0; i < size; i++)
{
cin >> a[i];
}
}
// // instance member function to show matrix
void showArray()
{
for (int i = 0; i < size; i++)
{
cout << a[i];
}
}
// // instance member function to set element
void setElement(int index, int value)
{
if (index >= 0 && index < size)
a[index] = value;
else
cout << "\nInvalid Index...";
}
// // instance member function to get element
int getElement(int index, int value)
{
if (index >= 0 && index < size)
a[index] = value;
else
cout << "\nInvalid Index...";
}
// // overload subscript ([]) operator for reading element
int operator[](int index) const
{
if (index < 0 || index >= size)
cout << "\n!!! Invalid Index, Array Index Out of Bound...\n";
return a[index];
}
// // overload subscript ([]) operator for writing element
int &operator[](int index)
{
if (index < 0 || index >= size)
cout << "\n!!! Invalid Index, Array Index Out of Bound...\n";
return a[index];
}
};
// // Main Function Start
int main()
{
Array arr1; // // create object of Array class
// // input elements of array
cout << "\nEnter 5 Elements of Array => ";
for (int i = 0; i < 5; i++)
cin >> arr1[i];
// // show elements of array
cout << "\n>>>>>>>>>> Elements of Array Are <<<<<<<<<<<<\n";
for (int i = 0; i < 5; i++)
cout << arr1[i] << " ";
cout << endl; // Add new line
cin.ignore();
return 0;
}
// // Main Function End