Functions and Methods Questions: 1
1. What is a reference in C++? 2
2. In the following code, which variable is being declared as a reference? Also, suppose we attempt to change the object of a reference, as in the fifth line below, will that work? int i = 30; int j = 100; int& r = i; r++; // i is now? r = j; // i is now? r++; // i is now? 3
3. If q is of type int*, is the following C++ syntax legal? int*& p = q; 4
4. Do the following two statements use the same operator for memory allocation? string* s1 = new string( "hello" ); string* s2 = new string[100]; 5
5. With regard to memory allocation, what s the crucial difference between the following two statements? In both cases we are allocating memory for an int array of 1000 elements. int* data = new int[1000]; int data[1000]; 6
6. What s the relationship between the operators new and new[], on the one hand, and the following two functions on the other? void* operator new( size_t size ); void* operator new[] (size_t size ); 7
7. Are there any memory leaks in the following program? Is so how will you plug them? Is there anything else wrong with the code? If so, how will you fix it? Will the program compile as it is? #include <string> class X ; int main() string* words[1000] = 0; X* xp = new X(); X* xdata = new X[1000]; delete xp; delete xdata; // More code that could possibly contain references // to xp and xdata. But this additional code does // not deallocate the memory allocated above. 8
8. What is the rule-of-thumb to remember to prevent memory leaks in C++ programs? 9
9. What s the access privilege of the two data members in the following C++ structure? struct User string name; int age; ; 10
10. What s the main difference between C and C++ with regard to the following structure? struct User string name; int age; ; 11
11. Is this syntax legal in C++? enum weight light, medium, heavy, very_heavy; int main() weight w1; w1 = heavy; weight w2 = (weight) (w1 + 200); cout << w2; 12
Function Declarations In C++, a function cannot be called unless it was declared previously. C++ function declarations, also called function prototypes, look like this: double cos( double ); double cos( double x); void f(int, int, int*); void g(); A function prototype may contain parameter names for better program readability, as is the case with the second example. However, the compiler simply ignores such names. Java does not require function declarations. 13
Passing Arguments in C++ When a function is called, each of its parameters is initialized with the corresponding argument in the function call, provided their types agree after taking into account any allowable type conversions. There are three different ways to pass arguments to functions in C++: pass by value pass by pointer pass by reference 14
Passing a Primitive Type Argument by Value #include <iostream> void g( int ); int main() int x = 100; g(x); //(A) cout << x << endl; // will output 100 void g( int y ) y++; //(B) 15
Passing a Primitive Type Argument by Pointer #include <iostream> void g( int* ); void h( int* ); int main() int x = 100; int* p = &x; g(p); //(A) cout << x << endl; // outputs 100 h(p); cout << x << endl; // outputs 200 void g( int* q ) int y = 200; q = &y; void h( int* q ) *q = 200; 16
Passing a Primitive Type Argument by Reference #include <iostream> void g( int& ); int main() int x = 100; g(x); //(A) cout << x << endl; // outputs 101 void g( int& y ) y++; 17
Passing a Class Type Argument by Value Passing a class type argument by value works the same way as passing a primitive type argument. The called function makes a local copy of the argument elsewhere in the memory and any changes made to this local copy by the called function are not visible in the calling function. 18
#include <iostream> #include <string> class User public: string name; int age; User( string nam, int yy ) name = nam; age = yy; ; void g( User ); int main() User u( "Xenon", 89 ); //(A) g(u); //(B) cout << u.name << " " << u.age << endl; // Xenon 89 void g( User v ) v.name = "Yukon"; v.age = 200; //(C) 19
Passing a Class Type Argument by Pointer When an argument is passed by pointer in a C++ program, any changes brought about inside the called function can become visible in the calling function through side effect, as was the case with the primitive types. 20
#include <iostream> #include <string> class User public: string name; int age; User( string nam, int yy ) name = nam; age = yy; ; void g( User* ); void h( User* ); int main() User* p = new User( "Xeno", 89 ); //(A) g( p ); //(B) cout << p->name << " " << p->age << endl; // Xeno 89 h( p ); //(C) cout << p->name << " " << p->age << endl; // Yuki 200 void g( User* q ) q = new User( "Yuki", 200 ); //(D) void h( User* q ) q->name = "Yuki"; q->age = 200; 21
Passing a Class Type Argument by Reference #include <iostream> #include <string> class User public: string name; int age; User( string nam, int yy ) name = nam; age = yy; ; void g( User& ); int main() User u( "Xenon", 89 ); // (A) g(u); // (B) cout << u.name << " " << u.age << endl; // Yukon 200 void g( User& v ) v.name = "Yukon"; v.age = 200; 22
Passing Arguments in Java Java gives you just one mode for passing arguments to methods pass by value. For class type arguments, a more precise way to say how arguments are passed to methods is that it is pass by value of object reference. 23
Passing a Primitive Type Argument class Test public static void main( String[] args ) int x = 100; // (A) g(x); // (B) System.out.println( x ); // outputs 100 static void g( int y ) y++; // (C) 24
Passing a Class Type Argument Passing a class type argument by value in Java is different from passing a class type argument by value in C++. 25
class User String name; int age; User( String nam, int yy ) name = nam; age = yy; ; class Test public static void main( String[] args ) User u = new User( "Xeno", 89 ); // (A) g(u); // (B) System.out.println( u.name + " " + u.age ); // Yuki 200 static void g( User v ) v.name = "Yuki"; v.age = 200; 26
The mode pass argument by value of object reference in Java is similar to pass by pointer in C++. 27
It might seem that there exists a similarity between pass by value of object reference in Java and the pass by reference in C++. In both cases, the changes brought about by the called function were visible in the calling function. So can we say the mode for argument passing in Java is similar to pass argument by reference in C++? The answer is a categorical No. 28
class User String name; int age; User(String nm, int a) name=nm; age=a; class test public static void main(string[] args) User u1 = new User("Xeno", 95); User u2 = new User("Yuki", 98); swap( u1, u2 ); System.out.println("u1 s name is: " + u1.name + ", and u2 s name is " + u2.name); static void swap(user s, User t) User temp = s; s = t; t = temp; 29
#include <iostream> #include <string> class User public: string name; int age; User(string nm, int a) name=nm; age=a; ; void swap( User&, User& ); int main() User u1 = User("Xeno", 95); User u2 = User("Yuki", 98); swap( u1, u2 ); cout << "u1 is: " << u1.name << ", and u2 is " << u2.name << endl; void swap(user& s, User& t) User temp = s; s = t; t = temp; 30
#include <iostream> #include <string> class User public: string name; int age; User(string nm, int a) name=nm; age=a; ; void swap( User*, User* ); int main() User u1 = User("Xeno", 95); User u2 = User("Yuki", 98); swap( &u1, &u2 ); cout << "u1 is: " << u1.name << ", and u2 is " << u2.name << endl; void swap(user* s, User* t) User temp = *s; *s = *t; *t = temp; 31
To pass an argument by value in C++ means that the parameter of the called function is handed a copy of the argument object in the calling function. On the other hand, to pass an argument by value in Java means that the parameter of the called function is handed a copy of the object reference held by the argument. Said another way, in call by value in Java, the calling function hands the called function a copy of the object reference, but not a copy of the object itself. On the other hand, in call by value in C++, the calling function hands to the called function a copy of the object. A call by reference not available in Java means that you are providing to the function parameter the object itself, the object that is held by the argument in the calling function. The reference parameter in the called function simply serves as just another name for the argument in the calling function. 32
Functions Returning Reference Types What exactly is returned by a function whose return has been specified as not void? Is it the local object itself, or is it a copy of the local object? 33
#include <iostream> #include <string> class User public: string name; int age; User( string nam, int yy ) name = nam; age = yy; ; User f( User u ) return u; int main() User x( "Xino", 120 ); User y = f( x ); cout << "y: " << y.name << endl; 34
What the function f returns is a copy of the local object it is supposed to return. Therefore, the program will make a copy of the argument object when it invokes the functionfand then another copy of that copy when it executes its return statement. That can amount to a lot of copying for large objects. We have already seen how this copying can be eliminated when passing arguments by using either a reference parameter or a pointer parameter. But what about the copying entailed in the objects that are returned? 35
#include <iostream> #include <string> class User public: string name; int age; User( string nam, int yy ) name = nam; age = yy; ; User& f( User u ) return u; int main() User x( "Xino", 120 ); User y = f( x ); cout << "y: " << y.name << endl; 36
If you wanted to guarantee that the User object being passed to the function as a reference would not get corrupted inside the function, you could use the following version of the function: User f( const User& u ) return u; And if in addition to guaranteeing that the object passed by reference would not get corrupted inside the function, you also wanted to suppress copying upon return, you could use the following version of the function: const User& f( const User& u ) return u; User& f( const User& u ) return u; 37