i***0 发帖数: 8469 | 1 大家来讨论一下c++吧
Please answer the following c++ questions. Do not simply answer yes/no.
Always explain your assumptions and reasoning.
1. memset is sometimes used to initialize data in a constructor like the
example below. What is the benefit of initializing this way? Does it work
in this example? Does it work in general ? Is it a good idea in general?
class A {
public:
A();
private:
int a;
float f;
char str[35];
long *lp;
};
A::A()
{
memset(this, 0, sizeof(*this));
}
2. Your task is to implement the Reduce function using templates. The
Reduce fn applies a function of two arguments cumulatively to the items of
an STL container, from begin() to end(), so as to reduce the sequence to a
single value. For example, Reduce(, std::plus
>()) should calculate ((((1+2)+3)+4)+5).
class NotEnoughElements {};
template
typename Container::value_type
Reduce(const Container& c, Function fn) throw (NotEnoughElements)
{
FILL OUT
}
3. Write a C++ program that would find and print the first longest
ascending or descending contiguous subsequence for a vector of integers. For
example, given a vector with
4, 2, 1, 2, 3, 4, 3, 5, 1, 2, 4, 6, 5
the program would find the underlined subsequence and print it.
4. You have a class that many libraries depend on. Now you need to modify
the class for one application. Which of the following changes require
recompiling all libraries before it is safe to build the application?
a. add a constructor
b. add a data member
c. change destructor into virtual
d. add an argument with default value to an existing member function |
|