// File: poly.h - header file for Polynomial class (partial version) // // TYPEDEF AND MEMBER CONSTANTS // typedef ____ Coefficient // Polynomial::Coefficient is the data type of each coefficient // // static const DEFAULT_SIZE = ____ // initial maximum degree // Polynomial::DEFAULT_SIZE is the maximum initial degree of a polynomial // higher degrees are accomodated using dynamic memory allocation // // CONSTRUCTORS AND DESTRUCTOR // Polynomial(int initial_size = DEFAULT_SIZE) // Postcondition: the polynomial is 0 and initially has maximum degree initial_size // // CONSTANT MEMBER FUNCTIONS // Coefficient get_coeff(int power) const // Precondition: power >= 0 // Postcondition: the value returned is the coefficient of the given power // // FRIEND FUNCTIONS // friend Polynomial operator +(const Polynomial& p, const Polynomial& q) // Postcondition: the polynomial returned is the sum of p and q // // friend Polynomial operator *(const Polynomial& p, const Polynomial& q) // Postcondition: the polynomial returned is the product of p and q // class Polynomial { public: typedef int Coefficient; // data type of each coefficient static const int DEFAULT_SIZE = 10; // initial maximum degree Polynomial(int initial_size = DEFAULT_SIZE); // CONSTRUCTOR Coefficient get_coeff(int power) const; // returns the given coefficient int get_degree() const; // returns the degree of the polynomial friend Polynomial operator +(const Polynomial& p, const Polynomial& q); friend Polynomial operator *(const Polynomial& p, const Polynomial& q); private: Coefficient *data; // data[k] contains coefficient of k-th power int degree; // highest power with nonzero coefficient int size; // size of currently allocated data array };
int *stuff; stuff = new int[12]; delete [] stuff;
Polynomial::Coefficient temp = 0; for (j=0; j<=i; j++) temp += p.data[j]*q.data[i-j]; Notes: 1) the member function get_coeff() may be used to access the coefficients instead of accessing the private data members directly as the above version does. 2) extra code is needed for the more general case in which i varies in the full range 0 <= i <= p.get_degree() + q.get_degree().
Polynomial::Coefficient Polynomial::get_coeff(int power) const { return(data[power]); }
power=25, degree=11, size=15, data = any Coefficient array of size 15 Any values for which power > degree will do, as there is no guarantee in such cases that data[power] has been defined.
Coefficient Polynomial::get_coeff(int power) const { if (power <= p.get_degree()) return(data[power]); else return(0); }
Here's a simple solution. Fancier versions would not write the full x^i part for i=0 or i=1, and would omit the "+" sign for negative coefficients to avoid "+-" symbols in the output. Such things can be done using standard versions of the iomanip library. See appendix F of Main and Savitch for brief descriptions of selected iomanip functions. ostream& operator <<(ostream& outs, const Polynomial& p) int i; for (i=0; i<=p.get_degree(); i++) { outs << " +" << p.get_coeff(i) << "x^" << i; } outs << endl; return outs; }
private part of the class definition ------------------------------------------------------------------------- ( public part of the class definition - with the other *member* functions ) ------------------------------------------------------------------------- after the class definition, with the other non-member functions
void Polynomial::operator =(const Polynomial& q) { Polynomial::Coefficient *newdata; newdata = new Polynomial::Coefficient[q.size]; for (int i=0; i<=q.degree; i++) { newdata[i] = q.data[i]; } delete [] data; data = newdata; degree = q.degree; size = q.size; }
In the following problems, reference is made to the Clock class as considered in Lab0 for CS 2005 in B Term 1999. Use the following definition of the Clock class. Observe that this definition includes two private member variables together with specific conventions regarding their admissible values and meanings.
// File clocks.h // class Clock { public: // CONSTRUCTOR Clock(); // Postcondition: clock is set to 12:00 midnight // MODIFICATION MEMBER FUNCTIONS void set_time(int hour, int minute, bool pm); // Precondition: hour is between 1 and 12, minute is between 0 and 59 // Postcondition: clock is set to time hour:minute. If pm is true, // then the time is between 12:00 noon and 11:59 pm, // otherwise it's between 12:00 midnight and 11:59 am. void advance(int minutes); // Postcondition: clock has been moved forward by the given number of minutes, // or backward by the absolute value |minutes| if minutes is negative. // CONSTANT MEMBER FUNCTIONS int get_hour() const; // Postcondition: returns current hour on clock // in 12 hour format (1 <= returned value <= 12) int get_minutes() const; // Postcondition: returns current minute reading // in 60 min format (0 <= returned value <= 59) bool is_pm() const; // Postcondition: returns true if current time is in afternoon (12pm-11:59pm) // returns false otherwise private: int current_hour; // in 24 hour format (0 <= current_hour <= 23) // 0 means 12:00 am (midnight) int current_minute; // in 60 minute format (0 <= current_minute <= 59) };
clickclock.set_time(1, 23, false);
int thisminute; thisminute = clickclock.get_minutes();
clickclock.advance(30);
void Clock::set_time(int hour, int minute, bool pm) { if (!pm) current_hour = hour; else current_hour = hour + 12; current_minute = minute; }
#include <iostream> #include "clocks.h" void print_time(const Clock& anyclock) { // use const reference parameter for efficiency // also clarifies that anyclock isn't changed cout << "The time is " << anyclock.get_hour() << ":"; if (anyclock.get_minutes() < 10) cout << "0" << anyclock.get_minutes(); else cout << anyclock.get_minutes(); if (anyclock.is_pm()) cout << " pm" << endl; else cout << " am" << endl; }
Box b, c; ----------- ( Box d(c); ) ----------- b.fillbox(); -------------- ( printbox(b); ) -------------- c=b;