Thursday, May 11, 2006

Physics 20 Reading List #8

Your first reading assignment for this week is about numerical root-finding methods: Secs. 9.0, 9.1, 9.2 (to p. 357 only), and 9.4 of Numerical Recipes.

To read about the golden ratio, see the MathWorld entry by Eric Weisstein. In fact, you'll probably find more on the web than you'll ever want to know about the golden ratio and the related Fibonacci numbers, which seem to be a favorite subject for term papers in high school. (By the way, the standard aspect ratio for Mathematica plots is the golden ratio...)

C++ Inheritance and virtual functions:
  • Read the chapter "Advanced classes" in Practical C++ Programming (which is in the lab)
  • Look at the following code, and decide what it will print. Compile and run it to check your answers.
  • Comment out the func() function in class A, and uncomment the 'virtual' version of this function. Again decide what the code will print. Compile and run to check your answers.



class A {
public:
void func() const {
cout << "'func()' in class A" << endl;
}
//virtual void func() const {
// cout << "'func()' in class A" << endl;
//}
};

class B: public A {
public:
void func() const {
cout << "'func()' in class B" << endl;
}
};

void PrintA(A& x) {
cout << "Inside PrintA --- ";
x.func();
}

void PrintB(B& x) {
cout << "Inside PrintB --- ";
x.func();
}

int main() {
A a;
B b;

PrintA(a);
PrintB(b);

PrintA(b); // This is the tricky one
}

0 Comments:

Post a Comment

<< Home