+ 3
Manav Roy as Martin Taylor said, "it's working because of pure luck". You created an array of length 4, you accessed the address after the last element and assigned a value to it(Java would throw an error here). When you access it again it's the same because that memory hasn't been assigned some other value by some other program. And if the memory block after the last element is not accessible, then your program would crash. Look at the below code, even after you assign a value to the address next to the last element and have size increased in your size variable, the built in functions still show what i said, the size didn't increase
https://code.sololearn.com/cTki75JZuwh9/?ref=app
+ 8
There are 2 main problems.
1. You declare NoOfStudent as a member, but from its usage in the code, it should be a static member. static keyword has various uses, and one of it is to declare a member that will be shared among the class objects. You can find more info about it on the net.
2. Your students is stored as array. It's length is fixed and cannot be changed (you can't add or delete items). You need a dynamic containers such as std::vector to get the job done. You can find many tutorials about vectors and other containers on the net and it's HIGHLY RECOMMENDED to learn
+ 4
Manav Roy there are multiple errors in your codes.
1) You're defining everything as private in your class, so you can't access them from outside.
2) You're using a variable in the class without creating an object. You can't use the variable without an object associated with it.
3) You're trying to dynamically increase the size of your array, which is not supported. Once an array is defined, it's fixed and can't resize as needed.
Fixes:
1) Define them as public.
2) Define the variable static
3) Use some other dynamically resizable container instead. Me myself(and a majority of C++ devs) would prefer vector(in header "vector").
I've fixed your code by using a vector instead of the array, and dynamically resizing it as needed. Have a look at it. I know it's too much to understand(especially if you don't know about them vectors). But if you get any doubts in this code, tag me and I'll come to the rescue
https://code.sololearn.com/c0B6yO0hAlx8/?ref=app
+ 1
Manav Roy I know that. it's bad that he disappeared and so as I in the Q&A
and I just got that Rishi was referring to one of his quotes
+ 1
Manav Roy what was @Rellot's screwdriver's comment? It's now deleted but i want to see itđ„șđ„șđ„ș
+ 1
Manav Roy oh I actually referred to one of his answers to one of my questions
+ 1
Manav Roy you can change the size of a dynamically allocated array, dynamically. Static arrays can't be changed. That's why "delete" and "new" keywords exist. The whole point of std::vector too is to help with this