A default constructor is a constructor that can be called with no arguments, which doesn't imply that the constructor has no parameters. Any constructor in which all the arguments have defaults can be considered a default constructor. For example
point::point(double x = 0.0, double y = 0.0) : x(x), y(y) { }
Yes: 6, No: 2
int a, b; ??? // your code here (if necessary) a = b; ??? // your code here (if necessary)
should swap the values in a
and b
. Fill in the rest of the
statements required to swap the values stored in a
and b
.
int a, b; int t = a; a = b; b = t;
Yes: 6, No: 2
If a variable with the parent's static type but the child's dynamic type is
deleted, a non-virtual destructor would result in the parent's destructor being
called, when it should be that the child's destructor needs to be called. A
virtual destructor in the parent assures that the child's destructor will be
called.
For example, assume triangle
is a child of shape
in
shape s = new triangle; delete s;
If shape::~shape()
is non-virtual, then it is called, but not
triangel::~triangle()
even though a triangle is being deleted. If
shape::~shape()
is virtual, then triangel::~triangle()
will be
called, followed by shape::~shape()
.
Note that non-virtual destructors in abstract classes are not automatically bad; for example, a triangle may not have anything it needs to clean up. However they do result in brittle code, because triangles could change in ways that do require non-trivial destructors, or you could add a child to shape that does have non-trivial destructors.
Yes: 1, No: 7
int x; cin >> x; while (x > 0) cin >> x;
Rewrite this code to remove the duplicate read into x
; that is, your code
should contain only one statement of the form cin >> x
.
do cin >> x; while (x > 0);
Some clever people came up with the alternative answer
int x = 1; while (x > 0) cin >> x;
Yes: 4, No: 4
point & point::operator + (const point & p) { point sum; // blah blah blah return sum; }
is wrong but
point & point::operator = (const point & rhs) { // blah blah blah return *this; }
is correct.
Both member functions return a reference to a variable. Unfortunately,
operator +()
returns a reference to a variable that disappears after the
call, leaving a dangling reference. operator =()
, on the other hand,
returns a reference to some class instance, which continues to live on after
the call, resulting in a valid reference.
Yes: 0, No: 8
A function prototype (also known as a function declaration) gives the function's name, the number and type of its arguments, and the return type (if any) but not how the function is implemented.
void bsort(int a[], size_t n);
is an example of a function prototype.
A function definition is a function prototype followed by a function body; that is, it describes how the function is implemented.
void bsort(int a[], int n) { for (int i = 0; i < n; i++) for (int j = n - 1 ; i < j; j--) if (a[j - 1] > a[j]) std::swap(a[j - i], a[j]); }
is an example of a function declaration.
Yes: 5, No: 3
int normal(int i) { /* blah blah blah */ } float normal(int i) { /* blah blah blah */ }
define overloaded functions? Explain your answer.
They do not; these two definitions are ambiguous. The return type is not used to resolve overloaded function calls. It may seem obvious which function to use in the context
float x = normal(3);
but remember that C++ allows function calls in contexts in which the return value is thrown away, as in
i = 1; normal(i); i = 2;
In this context it's impossible to determine which version of normal()
to
use.
Yes: 2, No: 6
Call-by-value isolates the caller from the called function; it is not possible for the called function to change the value of the variable the caller used as an argument.
Call-by-reference couples the caller and the called function; the called function can change the value of the variable the caller used as an argument.
Yes: 5, No: 3
int charsize(char str[]) { return (*str == '\0') ? 0 : (1 + charsize(str + 1)); }
Yes: 1, No: 7
Here are three ways, in order of decreasing usefulness:
int a1[5] = { 0 }; int a2[] = { 0, 0, 0, 0, 0 }; int a3[5]; for (int i = 0; i < 5; i++) a3[i] = 0;
Yes: 4, No: 4
If instances of the class C declaring the members are the only instances that should have access to the members, then the members should be declared private.
If, in addition to instances of C, the instances of any descendent classes of C should also have access to the members, then the members should be declared protected.
Yes: 4, No: 4
str
and a single
character ch
and returns two indices: one indicating the leftmost
occurrence of ch
in str
and the other indicating the rightmost
occurrence of ch
in str
. If ch
does not occur in str
both
indices should be -1.
For example given str = "hello world"
and ch = 'o'
, your
function should return 4 for the left index and 7 for the right index. Given
str = "hello world"
and ch = 'b'
, both the left and right
indices should be -1.
void shrink(char str[], char ch, int & left, int & right) { left = -1; right = -1; int i = 0; while ((str[i] != '\0') && (str[i] != ch)) i++; if (str[i] == ch) { left = i; right = i; while (str[++i] != '\0') if (str[i] == ch) right = i; } }
Yes: 1, No: 7
This page last modified on 7 February 2006.