Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1. What are the main conceptual differences between object-oriented programming

ID: 3549604 • Letter: 1

Question

1.        What are the main conceptual differences between object-oriented programming and the other programming techniques? (8 points)

2.        What is the definition of reference variable?   What are the differences between pass-by-value and pass-by-reference? (8 points)

3.        What is memory leakage? Given an example and explain why it causes memory leakage (8 points).

4.   What is wrong with the following code? How to fix it?  If there is nothing wrong, say "Nothing is wrong" (8 points).

    

    int *p;                                            // Line 1

     int *q;                                            // Line 2

     p = new int [6];                             // Line 3

*p = 1;                                           // Line 4

for (int i = 1; i < 6; i ++)                // Line 5

{

     p[i] = p[i-1] + i;                         // Line 6

}

q = p;                                              // Line 7

delete [ ]p;                                      // Line 8

for (int  j = 0; j < 6; j++)                // Line 9

cout << q[j] << endl;                // Line 10

5.        What is the output of the following program fragment: (8 points)

void add_value(int *x, int &y, int z);

{

    *x += 10;  

    y += 10;                   

    z += 10;

}

int main (void){

int a = 5, b = 10, c = 11;

add_value(&a,b,c);

cout << a <<

Explanation / Answer

1 Ans) Object-oriented programming is based on messages while procedural programming is based on function calls.

They are both abstraction of the programming task. In object oriented programming, everything is an object, that encapsulates both data and behavior. When you want an specific behavior, you send a message to an object. How the object will modify its data isn't your problem.On the other hand, on procedural programming the data and behavior are not coupled. We you need a behavior, you call a function that does this specific task. You need to provide that function with all necessary data


2 Ans) Definition of Reference Variable:A reference is an entity that is an alias for another object.A reference is not a variable as a variable is only introduced by the declaration of an object. An object is a region of storage and, in C++, references do not (necessarily) take up any storage.As objects and references are distinct groups of entities in C++ so the term "reference variable" isn't meaningful.


Difference between passing by Reference and passing by Value:


Let us consider the example of URL, I'm passing by Reference. You can use that URL to see the same web page I can see. If that page is changed, we both see the changes. If you delete the URL, all you're doing is destroying your reference to that page - you're not deleting the actual page itself.

If I print out the page and give you the printout, I'm passing by value . Your page is a disconnected copy of the original. You won't see any subsequent changes, and any changes that you make (e.g. scribbling on your printout) will not show up on the original page. If you destroy the printout, you have actually destroyed your copy of the object - but the original web page remains intact.

3 Ans) A memory leak occurs when a piece (or pieces) of memory that was previously allocated by a programmer is not properly deallocated by the programmer. Even though that memory is no longer in use by the program, it is still