String Class

string1.h // string1.h -- fixed and augmented string class definition #ifndef STRING1_H_ #define STRING1_H_ #include <iostream> using std::ostream; using std::istream; class String { private: char * str; // pointer to string int len; // length of string static int num_strings; // number of objects static const int CINLIM = 80; // cin input limit public: // constructors and other methods String(const char * s); // constructor String(); // default constructor String(const String &); // copy constructor ~String(); // destructor int length () const { return len; } // overloaded operator methods String & operator=(const String &); String & operator=(const char *); char & operator[](int i); const char & operator[](int i) const; // overloaded operator friends friend bool operator<(const String &st, const String &st2); friend bool operator>(const String &st1, const String &st2); friend bool operator==(const String &st, const String &st2); friend ostream & operator<<(ostream & os, const String & st); friend istream & operator>>(istream & is, String & st); // static function static int HowMany(); }; #endif string1.cpp ...

3 min · 526 words · cwHsueh

Student Class

studenti.h // studenti.h -- defining a Student class using private inheritance #ifndef STUDENTC_H_ #define STUDENTC_H_ #include <iostream> #include <valarray> #include <string> class Student : private std::string, private std::valarray<double> { private: typedef std::valarray<double> ArrayDb; // private method for scores output std::ostream & arr_out(std::ostream & os) const; public: Student() : std::string("Null Student"), ArrayDb() {} explicit Student(const std::string & s) : std::string(s), ArrayDb() {} explicit Student(int n) : std::string("Nully"), ArrayDb(n) {} Student(const std::string & s, int n) : std::string(s), ArrayDb(n) {} Student(const std::string & s, const ArrayDb & a) : std::string(s), ArrayDb(a) {} Student(const char * str, const double * pd, int n) : std::string(str), ArrayDb(pd, n) {} ~Student() {} double Average() const; double & operator[](int i); double operator[](int i) const; const std::string & Name() const; // friends // input friend std::istream & operator>>(std::istream & is, Student & stu); // 1 word friend std::istream & getline(std::istream & is, Student & stu); // 1 line // output friend std::ostream & operator<<(std::ostream & os, const Student & stu); }; #endif studenti.cpp // studenti.cpp -- Student class using private inheritance #include "studenti.h" using std::ostream; using std::endl; using std::istream; using std::string; // public methods double Student::Average() const { if (ArrayDb::size() > 0) return ArrayDb::sum()/ArrayDb::size(); else return 0; } const string & Student::Name() const { return (const string &) *this; } double & Student::operator[](int i) { return ArrayDb::operator[](i); // use ArrayDb::operator[]() } double Student::operator[](int i) const { return ArrayDb::operator[](i); } // private method ostream & Student::arr_out(ostream & os) const { int i; int lim = ArrayDb::size(); if (lim > 0) { for (i = 0; i < lim; i++) { os << ArrayDb::operator[](i) << " "; if (i % 5 == 4) os << endl; } if (i % 5 != 0) os << endl; } else os << " empty array "; return os; } // friends // use String version of operator>>() istream & operator>>(istream & is, Student & stu) { is >> (string &)stu; return is; } // use string friend getline(ostream &, const string &) istream & getline(istream & is, Student & stu) { getline(is, (string &)stu); return is; } // use string version of operator<<() ostream & operator<<(ostream & os, const Student & stu) { os << "Scores for " << (const string &) stu << ":\n"; stu.arr_out(os); // use private method for scores return os; }

2 min · 384 words · cwHsueh

Vector Class

// vect.h -- Vector class with <<, mode state #ifndef VECTOR_H_ #define VECTOR_H_ #include <iostream> namespace VECTOR { class Vector { public: enum Mode {RECT, POL}; // RECT for rectangular, POL for Polar modes private: double x; // horizontal value double y; // vertical value double mag; // length of vector double ang; // direction of vector in degrees Mode mode; // RECT or POL // private methods for setting values void set_mag(); void set_ang(); void set_x(); void set_y(); public: Vector(); Vector(double n1, double n2, Mode form = RECT); void reset(double n1, double n2, Mode form = RECT); ~Vector(); double xval() const {return x;} // report x value double yval() const {return y;} // report y value double magval() const {return mag;} // report magnitude double angval() const {return ang;} // report angle void polar_mode(); // set mode to POL void rect_mode(); // set mode to RECT // operator overloading Vector operator+(const Vector & b) const; Vector operator-(const Vector & b) const; Vector operator-() const; Vector operator*(double n) const; // friends friend Vector operator*(double n, const Vector & a); friend std::ostream & operator<<(std::ostream & os, const Vector & v); }; } // end namespace VECTOR #endif // vect.cpp -- methods for the Vector class #include <cmath> #include "vect.h" // includes <iostream> using std::sqrt; using std::sin; using std::cos; using std::atan; using std::atan2; using std::cout; namespace VECTOR { // compute degrees in one radian const double Rad_to_deg = 45.0 / atan(1.0); // should be about 57.2957795130823 // private methods // calculates magnitude from x and y void Vector::set_mag() { mag = sqrt(x * x + y * y); } void Vector::set_ang() { if (x == 0.0 && y == 0.0) ang = 0.0; else ang = atan2(y, x); } // set x from polar coordinate void Vector::set_x() { x = mag * cos(ang); } // set y from polar coordinate void Vector::set_y() { y = mag * sin(ang); } // public methods Vector::Vector() // default constructor { x = y = mag = ang = 0.0; mode = RECT; } // construct vector from rectangular coordinates if form is r // (the default) or else from polar coordinates if form is p Vector::Vector(double n1, double n2, Mode form) { mode = form; if (form == RECT) { x = n1; y = n2; set_mag(); set_ang(); } else if (form == POL) { mag = n1; ang = n2 / Rad_to_deg; set_x(); set_y(); } else { cout << "Incorrect 3rd argument to Vector() -- "; cout << "vector set to 0\n"; x = y = mag = ang = 0.0; mode = RECT; } } // reset vector from rectangular coordinates if form is // RECT (the default) or else from polar coordinates if // form is POL void Vector:: reset(double n1, double n2, Mode form) { mode = form; if (form == RECT) { x = n1; y = n2; set_mag(); set_ang(); } else if (form == POL) { mag = n1; ang = n2 / Rad_to_deg; set_x(); set_y(); } else { cout << "Incorrect 3rd argument to Vector() -- "; cout << "vector set to 0\n"; x = y = mag = ang = 0.0; mode = RECT; } } Vector::~Vector() // destructor { } void Vector::polar_mode() // set to polar mode { mode = POL; } void Vector::rect_mode() // set to rectangular mode { mode = RECT; } // operator overloading // add two Vectors Vector Vector::operator+(const Vector & b) const { return Vector(x + b.x, y + b.y); } // subtract Vector b from a Vector Vector::operator-(const Vector & b) const { return Vector(x - b.x, y - b.y); } // reverse sign of Vector Vector Vector::operator-() const { return Vector(-x, -y); } // multiply vector by n Vector Vector::operator*(double n) const { return Vector(n * x, n * y); } // friend methods // multiply n by Vector a Vector operator*(double n, const Vector & a) { return a * n; } // display rectangular coordinates if mode is RECT, // else display polar coordinates if mode is POL std::ostream & operator<<(std::ostream & os, const Vector & v) { if (v.mode == Vector::RECT) os << "(x,y) = (" << v.x << ", " << v.y << ")"; else if (v.mode == Vector::POL) { os << "(m,a) = (" << v.mag << ", " << v.ang * Rad_to_deg << ")"; } else os << "Vector object mode is invalid"; return os; } } // end namespace VECTOR

4 min · 738 words · cwHsueh

Worker0 Class

worker0.h // worker0.h -- working classes #ifndef WORKER0_H_ #define WORKER0_H_ #include <string> class Worker // an abstract base class { private: std::string fullname; long id; public: Worker() : fullname("no one"), id(0L) {} Worker(const std::string & s, long n) : fullname(s), id(n) {} virtual ~Worker() = 0; // pure virtual destructor virtual void Set(); virtual void Show() const; }; class Waiter : public Worker { private: int panache; public: Waiter() : Worker(), panache(0) {} Waiter(const std::string & s, long n, int p = 0) : Worker(s, n), panache(p) {} Waiter(const Worker & wk, int p = 0) : Worker(wk), panache(p) {} void Set(); void Show() const; }; class Singer : public Worker { protected: enum {other, alto, contralto, soprano, bass, baritone, tenor}; enum {Vtypes = 7}; private: static char *pv[Vtypes]; // string equivs of voice types int voice; public: Singer() : Worker(), voice(other) {} Singer(const std::string & s, long n, int v = other) : Worker(s, n), voice(v) {} Singer(const Worker & wk, int v = other) : Worker(wk), voice(v) {} void Set(); void Show() const; }; #endif worker0.cpp ...

2 min · 418 words · cwHsueh