What is a Reference ?
What is the difference between a pointer and a reference ?
Why use References ?
References are typically used when aliases are needed for existing objects such as for return values of functions and in parameter lists for functions.
An Example
#include <iostream.h>
int main( )
{
int radius, diam;
int &rAliasToRadius = radius; //create and initialze the reference
radius = 7;
diam = 20;
cout << "radius: " << radius << endl;
cout << "diam: " << diam << endl;
cout << "rAliasToRadius: " << rAliasToRadius << endl;
cout << "&radius: " << &radius << endl;
cout << "&diam: " << &diam << endl;
cout << "&rAliasToRadius: " << &rAliasToRadius << endl;
rAliasToRadius = 7 * radius;
cout << "\nradius: " << radius << endl;
cout << "rAliasToRadius: " << rAliasToRadius << endl;
rAliasToRadius = 7 * radius;
cout << "\nradius: " << radius << endl;
cout << "rAliasToRadius: " << rAliasToRadius << endl;
rAliasToRadius = diam; //reassigning the reference will affect radius as well
cout << "\nradius: " << radius << endl;
cout << "diam: " << diam << endl;
cout << "rAliasToRadius: " << rAliasToRadius << endl;
return 0;
}
radius: 0x1010e
diam: 0x1016e
rAliasToRadius: 0x1010e
radius: 49
rAliasToRadius: 49
radius: 20
diam: 20
rAliasToRadius: 20
References are not variables: You can't change them, add them, assign a reference to a pointer, etc.
They're a second name for a variable -- hopefully, a little safer
Pointer way:
*
void swap_values(float *a, float *b)
{
float temp;
temp = *a;
*a = *b;
*b = temp;
}
C++ references way
#include <iostream.h>
void swap_values(float& a, float& b)
{
float temp;
temp = a;
a = b;
b = temp;
}
Q: In C, a function which takes a multidimensional array as an argument must include all dimension except the first in the function prototype, i.e.<O:P</O:P
void foo(int arg[][10][10]);<O:P</O:P
Why is this?<O:P</O:P
A: When arrays are passed to functions, they are converted to pointers to the first element in the array, thereby losing size information. However, the compiler needs size information to know how to access a given element in the array.<O:P</O:P
For example, an array declared like "int x[10][20]" is really 10 arrays of 20 ints each (laid out sequentially in memory). So to find x[i][j], the compiler looks in x + i * 20 + j. Note that we need the "20" above to know how many ints to skip for each increment of "i". We don't need to know the "10", however.<O:P</O:P
Note: An experienced programmer should know the answer to this question.<O:P</O:P
<O:P</O:P
Q: What does the following program produce?<O:P</O:P
main(){printf("hello"); fork();} <O:P</O:P
A: Prints hello twice. Because printf buffers its output by the time the fork() is called, the string is not yet written. After the fork(), both processes have the unwritten string in their buffers, which get flushed upon the process exiting.<O:P</O:P
<O:P</O:P
Q: Enumerate and describe at least three different usages for each of the two keywords: 'static' and'const'. Although this question may seem trivial to many good C++ programmers, surprisingly many candidates fail to answer it satisfactory.<O:P</O:P
A: For 'static' the different contexts are:<O:P</O:P
<O:P</O:P
Q: What is the main distinction between C++ exception-handling mechanism and C longjmp?<O:P</O:P
A: Exceptions in C++ are guaranteed to call destructors for stack (automatic) variables<O:P</O:P
<O:P</O:P
Q (part 1):<O:P</O:P
Suppose I'm a totally novice object-oriented developer. I want to write a C++ Person class whose instances, among other things, remember their name.<O:P</O:P
A (part 1):<O:P</O:P
Here's my first crack at it: (ignore my bad syntax)<O:P</O:P
class Person, instance variables {<O:P</O:P
char *name<O:P</O:P
}<O:P</O:P
Person::setName(char *newName) {<O:P</O:P
name = newName;<O:P</O:P
}<O:P</O:P
Person::getName() {<O:P</O:P
return name;<O:P</O:P
}<O:P</O:P
<O:P</O:P
Q (part 2):<O:P</O:P
Is this a good implementation? What would you change? Candidates usually balk at this point, asking questions like "Well, what do you want to do with the class? and "Well, it works... what are your goals?" I rebuff all these questions, and tell them, look, I want a Person class. I want it to be a Person. It's going to be part of a larger program. Sometimes a Person's name changes. How should the class be?<O:P</O:P
A (part 2):<O:P</O:P
I find it useful to see how they struggle with a poorly-defined situation. It's kind of an open-ended question, but usually the code the candidate comes up with has bugs, some of them subtle:<O:P</O:P
Person::setName(char *newName) {<O:P</O:P
delete name;<O:P</O:P
name = malloc(strlen(newName) + 1);<O:P</O:P
strcpy(name, newName);<O:P</O:P
}<O:P</O:P
This code "breaks" under the following circumstances:<O:P</O:P
Person p;<O:P</O:P
char *s;<O:P</O:P
p.setName("Dave");<O:P</O:P
s = p.getName;<O:P</O:P
...<O:P</O:P
time passes<O:P</O:P
...<O:P</O:P
p.setName(s);<O:P</O:P
Because setName will delete its own backing storage and then try to refer to it in strcpy. Some candidates ask if they can use a String class. The fact that they ask is significant in itself, and I usually say yes, since there are still issues with getName that can be explored.<O:P</O:P
<O:P</O:P
Q: What are the differences between C++ and Java?<O:P</O:P
A: Some of the differences:<O:P</O:P
<O:P</O:P
Q: How is Java AWT event handling different from X/Motif event handling?<O:P</O:P
A: In Java it is possible to capture all the events for a widget in a single callback (called handleEvent). In addition, Java programmers have more control over the event propagation because every widget can decide whether it or its parent should handle the event.<O:P</O:P
<O:P</O:P
Q: In Java, which does not support C++-like enum types, how would you build such a capability? That is, the goal is to provide strongly-typed enum types in Java.<O:P</O:P
A: Use subclassing and the "InstanceOf" operator. So if you would have an enum called "Colors" with possible values "Red, Green, Blue" you create a class called "Colors" with subclasses of "Red," "Green," and "Blue." You then use an instance of the appropriate class wherever you would use the associated enum type. The InstanceOf operator can then be used to ask an object if it is of type Colors and if it is a particular color (i.e. subclass of Colors). Disadvantages: switch statements don't work - you have to use cascading if's.<O:P</O:P
void main(void)
{
float big = 10000.0;
float small = 0.00001;
float& big_alias = big;
float& small_alias = small;
swap_valus(big_alias,small_alias);
// swap_value(float& big, float& small);
cout << "Big:"<<big<<endl;
cout << "Small:"<<small<<endl;
}