In: Computer Science
// note the different names for the C libraries #include <cstdio> // cstdio instead of stdio.h #include <cstdlib> // sstdlib instead of stdlib.h // a simple class to explore the ideas of: // data members // access modifiers of private, public // keyword static class LabExample { private: int storage; public: void setStorage (int s) { storage = s; // this->storage = s; } int getStorage () { // return storage; return this->storage; } void modStorage () { storage = storage * 2; } static void staticMethod ( ) { printf ("Inside staticMethod()\n"); // the following would cause an error if attempted //printf ("The value of storage is: %d\n", storage); } }; int main ( int argc, char** argv) { // create two instances of the above class LabExample inst1, inst2; // LINE 1 printf ("Inside main\n"); inst1.setStorage (5); // LINE 2 inst2.setStorage (1); // LINE 3 printf ("inst1 contains: %d \n", inst1.getStorage() ); // LINE 4 printf ("inst2 contains: %d \n", inst2.getStorage() ); inst1.modStorage(); inst1.modStorage(); inst2.modStorage(); printf ("inst1 now contains: %d \n", inst1.getStorage() ); printf ("inst2 now contains: %d \n", inst2.getStorage() ); // LINE 5 // repeat above with pointers to the above class LabExample *ptr1, *ptr2; // LINE 6 // Use malloc to allocte heap memory and store address in pointers ptr1 = (LabExample*) malloc ( sizeof (LabExample) ); // LINE 7 ptr2 = (LabExample*) malloc ( sizeof (LabExample) ); // LINE 8 // must use -> instead of . since these are pointers ptr1->setStorage (15); // LINE 9 ptr2->setStorage (11); printf ("ptr1 contains: %d \n", ptr1->getStorage() ); printf ("ptr2 contains: %d \n", ptr2->getStorage() ); ptr1->modStorage(); ptr1->modStorage(); ptr2->modStorage(); printf ("ptr1 now contains: %d \n", ptr1->getStorage() ); printf ("ptr2 now contains: %d \n", ptr2->getStorage() ); // LINE 10 // de-allocate the pointers free (ptr1); // Use free() when allocated with malloc() LINE 11 free (ptr2); // Use free() when allocated with malloc() LINE 12 // call the static method LabExample::staticMethod(); // LINE 13 printf ("End of main\n"); return 1; }
Question 1: Describe what is occurring at LINE 2 and LINE 3.
Question 2: Look at the lines of code starting at LINE 4 and ending at LINE 5. There are no parameters in any of the method calls in this code. Where are the values that get printed?
Question 3: LINE 6 creates two class pointers. What is actually being allocated by this line? In what type of memory does this allocation occur?
Question 4: What is being allocated by the lines marked LINE 7 and LINE 8? In what type of memory does this allocation occur?
Question 5: Why does the code from LINE 9 through LINE 10 use the arrow operator while the code from LINE 2 through LINE 5 use the dot operator?
Answer and Explanation for your Questions is provided below. If you need any further clarification, feel free to ask in comments. I would really appreciate if you would let me know if the explanation provided is to your satisfaction.
Answer 1:
inst1.setStorage (5);
// LINE 2
inst2.setStorage (1); // LINE 3
LINE1 is calling mutator function setStorage() for inst1 object. The setStorage() function sets the value of storage data member of inst1 hence the inst1.storage=5 .
LINE2 is calling mutator function setStorage() for inst2 object. The setStorage() function sets the value of storage data member of inst2 hence the inst2.storage=5 .
Answer 2:
printf
("inst1 contains: %d \n", inst1.getStorage() ); // LINE 4
printf ("inst2 contains: %d \n", inst2.getStorage()
);
inst1.modStorage();
inst1.modStorage();
inst2.modStorage();
printf ("inst1 now contains: %d \n",
inst1.getStorage() );
printf ("inst2 now contains: %d \n",
inst2.getStorage() ); // LINE 5
printf method prints the value returned by getStorage() function of class. getStorage() is a accessor function of class. It returns the value of dat member storage. When it is called by any object, it returns the value of storage for that object. So inst1.getStorage() function returns value of inst1.storage and inst2.getStorage() returns value of inst2.storage. So output will be:---->>>
inst1
contains: 5
inst2 contains: 1
inst1 now contains: 20
inst2 now contains: 2
Answer 3:
LabExample *ptr1, *ptr2; // LINE 6
There are two pointers created. but they are not pointing to anything right now. So only the name of pointer is allocated and not the actual object to which the pointer will point to. As it is just a variable of pointer type , So it is allocated on Stack memory.
Answer 4:
ptr1 =
(LabExample*) malloc ( sizeof (LabExample) ); // LINE 7
ptr2 = (LabExample*) malloc ( sizeof (LabExample) );
// LINE 8
LINE7 and LINE8 creates the object of type LabExample dynamically. Now the pointer ptr1 and ptr2 points to these two objects. This is a dynamic allocation hence it is allocated on Heap memory.
Answer 5:
LINE2-LINE5 are direct objects of class so they are using dot operator to access the data members but LINE9-LINE10 are pointers so they use arrow operator to access the data members. arrow operator means pointer to member. We use arrow operator for convenience otherwise ptr1->getStorage() is equivalent to (*ptr1).getStorage().