
這是一個定義的一個矢量類, 然后用矢量類模擬一個酒鬼的隨機漫步
問題很簡單, 實現也不麻煩, 但是這個小程序卻可以呈現出許多語法知識。而且代碼風格也不錯,因此保存在了這篇博客中。
?
建議:
?? ????1. 類的聲明以及函數的聲明放到一個文件夾內,?并且在一些必要的地方加上注釋!
?2.?函數的實現放到另一個文件內。
?????? 3.?將程序要具體解決的問題放到另外的一個文件里。(詳見代碼!)
?
好處:?把類的接口和實現細節分離開,?易于更改某個函數的功能。
??????????把函數的聲明和定義分開,?提高代碼可讀性。
??????????把類的聲明和定義?與?要解決的問題分開,?提高,類的重用性!
?
?????????????????????????????????????????定義類(聲明類內的函數)
// 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 modesprivate:double x; // horizontal valuedouble y; // vertical valuedouble mag; // length of vector in degreesdouble ang;// direction of vector in degreesMode mode; // private methods for setting valuesvoid 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 valuedouble yval() const {return y; } // report y valuedouble magval() const {return mag; } // report magnitudedouble angval() const {return ang; } // report anglevoid polar_mode(); // set mode to POLvoid rect_mode(); // set mode to RECT// operator overloadingVector operator+(const Vector & b) const;Vector operator-(const Vector & b) const;Vector operator-()const;Vector operator*(double n) const;// friendsfriend Vector operator*(double n, const Vector & a);friend std::ostream & operator<<(std::ostream & os, const Vector & v);}; } // end namespace VECTOR #endif
?上面代碼涵蓋了許多關于類的基礎知識:??名稱空間與作用域?, ?實現類內部常量的方法, ?構造函數,?析構函數,?運算符重載,?友元函數,?關于多種實現方法等。
?
PS:?實現類內部常量的方法:?(1)枚舉類型。?(2)?static?const?int??常量
????????運算符重載:?注意操作數的順序。
???????????????????????????????????????????
?????????????????????????????????????????????????????????? 函數定義
// vect.cpp -- methods for the Vector class #include <cmath> #include "vect.h" // include <iostream> using std::sqrt; using std::sin; using std::cos; using std::atan; using std::atan2; using std::cout;namespace VECTOR {// compute degree in one radianconst double Rad_to_deg = 45.0/atan(1.0);// should be about 57.2957795130823// private methods// calculate magnitude from x and yvoid 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 coordinatevoid Vector::set_x(){x = mag*cos(ang);}//set y from polar coodinatevoid Vector::set_y(){y = mag * sin(ang);}// public methodsVector::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 pVector::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.0";mode = RECT;}}// reset vector from rectangular coodinates if form is// RECT (the default) or else form polar coordinates if// form is POLvoid 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.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 VectorsVector 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 VectorVector Vector::operator-() const{return Vector(-x, -y);}// multiply vector by nVector Vector::operator*(double n) const{return Vector(n*x, n*y);}// friend methods// multiply n by Vector aVector operator*(double n, const Vector & a){return a * n;}//display rectangular coordinates if mode is RECT// else display polar coordinates if mode is POLstd::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 << ")"; }elseos << "Vector object mode is invalid";return os;} } // end namespace VECTOR
?
?
???????????????????????????????????????????????????????????具體解決的問題
// randwalk.cpp -- using the Vector class // compile with the vect.cpp file #include <iostream> #include <cstdlib> // rand(), srand() pototypes #include <ctime> // time() pototype #include "vect.h"int main() {using namespace std;using VECTOR::Vector;srand(time(0)); // seed random-number generatordouble direction;Vector step;Vector result(0.0, 0.0);unsigned long steps = 0;double target;double dstep;cout << "Enter target distance (q to quit): ";while(cin >> target){cout << "Enter step length: ";if(!(cin>>dstep))break;while(result.magval() < target){direction = rand()%360;step.reset(dstep, direction, Vector::POL);result = result + step;steps++;}cout << "After " << steps <<" steps, the subject ""has the following location:\n";cout << result <<endl;result.polar_mode();cout << "or\n" << result << endl;cout << "Average outward distance per step = "<< result.magval()/steps << endl;steps = 0;result.reset(0.0, 0.0);cout << "Enter target distance (q to quit): ";}cout << "Bye!\n";cin.clear();while(cin.get() != '\n')continue;return 0; }
?
二。一個簡易的string類。
鍛煉內容:
??????????? 拷貝構造函數(深拷貝與淺拷貝)
??????????? 重載賦值運算符(深賦值)
??????????? 許多的細節與技巧!
?
類的聲明


//sting1.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 ot stringint len; // length of stringstatic int num_strings; // number of objectsstatic const int CINLIM = 80; // cin input limit public:// construction and other methodsString(const char * s); // constructorString(); // default constructorString(const String &); // copy constructor~String(); // destructor int length() const { return len; }// overloaded operator methodsString & operator=(const String &);String & operator=(const char *);char & operator[](int i);const char & operator[](int i)const;// overloaded operator friendsfriend 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 functionstatic int HowMany(); }; #endif
?
類方法的實現。


// string1.cpp -- String class methods #include <cstring> // string.h for some #include "string1.h" // includes <iostream> using std::cin; using std::cout;// initializing static class member int String::num_strings = 0;// static method int String::HowMany() {return num_strings; }// class methods String::String(const char * s) // construct String from C string {len = std::strlen(s); // set sizestr = new char[len + 1]; // allot storagestd::strcpy(str, s); // initialize pointernum_strings++; // set object count }String::String() // default constructor {len = 4;str = new char[1];str[0] = '\0'; // default stringnum_strings++; }String::String(const String & st) {num_strings++; // handle static member updatelen = st.len; // same lengthstr = new char [len + 1]; // allot space std::strcpy(str, st.str); // copy string to new location }String::~String() // necesserary destructor {--num_strings; // requireddelete [] str; // required }// overloaded operator methods// assign a String to a String String & String::operator=(const String & st) {if(this == &st)return *this;delete [] str;len = st.len;str = new char[len + 1];std::strcpy(str, st.str);return *this; }// assign a C string to a String String & String::operator=(const char * s) {delete [] str;len = std::strlen(s);str = new char[len + 1];std::strcpy(str, s);return *this; }// read-write char access for non-const String char & String::operator[](int i) {return str[i]; }// read-only char access for const string const char & String::operator[](int i) const {return str[i]; }// averloaded operator friendsbool operator<(const String &st1, const String &st2) {return (std::strcmp(st1.str, st2.str) < 0); }bool operator>(const String &st1, const String &st2) {return st2 < st1; }bool operator==(const String &st1, const String &st2) {return (std::strcmp(st1.str, st2.str)==0); }// simple String output ostream & operator<<(ostream & os, const String & st) {os << st.str;return os; }// quick and dirty String input istream & operator>>(istream & is, String & st) {char temp[String::CINLIM];is.get(temp, String::CINLIM);if(is)st = temp;while (is && is.get() != '\n')continue;return is; }
?
main(), 測試String類。


// saying1.cpp -- using expanded String class // complile with string1.cpp #include <iostream> #include "string1.h" const int MaxLen = 81; int main() {using std::cout;using std::cin;using std::endl;String name;cout << "Hi, what's your name?\n>> ";cin >> name;cout << name << ", please enter up to " << ArSize<< " short sayings <empty line to quit>:\n";String sayings[ArSize]; // array of objectschar temp[MaxLen] // temporary string storageint i;for (i = 0; i < ArSize; i++){cout << i + 1 << ": ";cin.get(temp, MaxLen);while(cin && cin.get()!='\n')continue;if(!cin||temp[0] == '\0') // empty line?break; // i not increamentedelsesayings[i] = temp; // overloaded assignment }int total = i; // total # of lines readif( total > 0){cout << "Here are your sayings:\n";for (i = 0; i < total; i++)cout << sayings[i][0] << ": " << sayings[i] << endl;int shortest = 0;int first = 0;for(i = 1; i < total; i++){if(sayings[i].length() < sayings[shortest].length())shortest = i;if(sayings[i] < sayings[first])first = i;}cout << "Shortest saying:\n" << sayings[shortest] << endl;cout << "First alphabetically:\n" << sayings[first] << endl;cout << "This program used " << String::HowMany()<< " String objects. Bye.\n"}elsecout << "No input! Bye.\n";return 0; }
?
?
代碼源自: C++ Primer Plus? 。?小恪親自敲寫!
感悟:? 如果一部書經久不衰, 一定是有它的理由的! 正如這部書,? 內容細致而深刻, 全面而嚴謹。獲益良多!此書有點兒厚,與諸君共勉。