// 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 int DEFAULT_SIZE = ____ // initial maximum degree
// Polynomial::DEFAULT_SIZE is the maximum initial degree of a polynomial
// higher degrees are accomodated using dynamic memory allocation
//
// CONSTRUCTOR
// 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
};
Polynomial::Coefficient Polynomial::get_coeff(int power) const {
return(data[power]);
}
ostream& operator <<(ostream& outs, const Polynomial& p)
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)
};
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)
Box b, c; Box d(c); b.fillbox(); printbox(b); c=b;