Homework 6
Physics 131/231: Simulations in C++
Winter 2000
Problem Set 6
Due 2:00 pm Tuesday, February 22.
1. The following program is supposed to demonstrate polymorphism.
However, it has a number of mistakes. Correct the mistakes, print
out the correct program, and
show what happens when you compile and run the program.
#include <iostream.h>
typedef double real;
class RealFunction
{
public:
real operator()()=0;
~RealFunction(){};
};
class Square: RealFunction
{
public:
real operator(())
{return x*x;}
Square(real xinput):x(xinput){}
private:
real x;
};
class Cube: RealFunction
{
public:
real operator()
{return x*x*x;}
Cube(real xinput):x(xinput){}
private:
real x;
};
void ExtFunc(RealFunction f)
{
cout << f() <<endl;
}
void main()
{
real y=5;
Square sq(y);
Cube cu(y);
ExtFunc(sq);
ExtFunc(cu);
}