CSC 220

Introduction

First Steps

Control

Functions

Arrays

Containers

Classes

Trivial Class

Point Class

Point Driver

Polynomial Class

Poly Imple

Poly Driver

Poly Ptr

Primgen Algorithm

Primgen Code

Templates

Memory Management

Exceptions

Class Inheritance

Multi-File Programs

Other Things

/*
* Polynomial simple tester (again). This is exactly like the
* first one, except we pass the polynomial object by pointer.
*/
#include <string>
#include <iostream>
using namespace std;
#include "poly.h"
void polyrunner(string label, const Polynomial *p)
{
cout << "f(x) = " << p->tos() << endl;
cout << "f'(x) = " << p->dx().tos() << endl;
for(double d: { 3.1, 0.0, -5.2, 8.33, 1.0, 7.15, -1.0, 2.13, -4.2 } ) {
cout << "f(" << d << ") = " << p->f(d) << endl;
}
cout << endl;
}
int main()
{
Polynomial p1(13.1);
polyrunner("p1", &p1);
Polynomial p2(-2.8, 1.0, 3.3);
polyrunner("p2", &p2);
Polynomial p3 = { 3, 8.1, 0.0, -4.1 };
polyrunner("p3", &p3);
Polynomial p4 = { 7.8, 0.0, 0.0, -2.31, 0.0, 0.0, 7.0, 0.0 };
polyrunner("p4", &p4);
}

This poly driver is just like the last one, except we pass the Polynomial object using a pointer. We mentioned earlier that it is possible to use the ampersand operator to explicitly create a pointer to pretty much anything. We have done that, and passed the resulting pointer as a parameter to polyrunner. Inside the body of polyrunner, now that p is a pointer, we run methods using the arrow operator, -> instead of the dot.