类引入
到眼下为止我们所写的自己定义类型都是keywordstruct,从如今起我们将採用class方式定义类,这样的方式对于学习过其它高级语言包含脚本(Such as Python)的人来说再熟悉只是了.
可是在写之前我们还是须要比較一下用struct和class之间有什么差别.
首先对于struct,在C兼容性方面非常重要,虽然C++是有别于C的还有一门语言,但很多程序还是必须与C交互,C++有两个重要功能,能够方便的与C交互.当中之中的一个的就是POD,即是Plain Old Data(简单旧式数据)的缩写.
POD类型就是没有其它功能仅用于存储数据的类型.如内置类型就是POD类型,该类型没有其它功能.详细的int类型.一个既无构造函数也没有重载的赋值操作函数而仅有共同拥有的POD类型作为数据成员的类也是一个POD类型.
POD类型的重要性在于,那些在C++库/第三方库的/操作系统接口中遗留的C函数须要POD类型.
在编写类之前,我们能够直接看一下class的样例,即是rational的最新版本号,部分代码例如以下:
/** @file rational_class.cpp */ /** The Latest Rewrite of the rational Class */ #include <cassert> #include <cstdlib> #include <istream> #include <ostream> #include <sstream> using namespace std; /// Compute the greatest common divisor of two integers, using Euclid’s algorithm. int gcd(int n, int m) { n = abs(n); while (m != 0) { int tmp(n % m); n = m; m = tmp; } return n; } /// Represent a rational number (fraction) as a numerator and denominator. class rational { public: rational(): numerator_(0), denominator_(1) {} rational(int num): numerator_(num), denominator_(1) {} rational(int num, int den) : numerator_(num), denominator_(den) { reduce(); } rational(double r) : numerator_(static_cast<int>(r * 10000)), denominator_(10000) { reduce(); } int numerator() const { return numerator_; } int denominator() const { return denominator_; } float as_float() const { return static_cast<float>(numerator()) / denominator(); } double as_double() const { return static_cast<double>(numerator()) / denominator(); } long double as_long_double() const { return static_cast<long double>(numerator()) / denominator(); } /// Assign a numerator and a denominator, then reduce to normal form. void assign(int num, int den) { numerator_ = num; denominator_ = den; reduce(); } private: /// Reduce the numerator and denominator by their GCD. void reduce() { assert(denominator() != 0); if (denominator() < 0) { denominator_ = -denominator(); numerator_ = -numerator(); } int div(gcd(numerator(), denominator())); numerator_ = numerator() / div; denominator_ = denominator() / div; } int numerator_; int denominator_; }; /// Absolute value of a rational number. rational abs(rational const& r) { return rational(abs(r.numerator()), r.denominator()); } /// Unary negation of a rational number. rational operator-(rational const& r) { return rational(-r.numerator(), r.denominator()); } /// Add rational numbers. rational operator+(rational const& lhs, rational const& rhs) { return rational( lhs.numerator() * rhs.denominator() + rhs.numerator() * lhs.denominator(), lhs.denominator() * rhs.denominator()); } /// Subtraction of rational numbers. rational operator-(rational const& lhs, rational const& rhs) { return rational( lhs.numerator() * rhs.denominator() - rhs.numerator() * lhs.denominator(), lhs.denominator() * rhs.denominator()); } /// Multiplication of rational numbers. rational operator*(rational const& lhs, rational const& rhs) { return rational(lhs.numerator() * rhs.numerator(), lhs.denominator() * rhs.denominator()); } /// Division of rational numbers. /// TODO: check for division-by-zero rational operator/(rational const& lhs, rational const& rhs) { return rational(lhs.numerator() * rhs.denominator(), lhs.denominator() * rhs.numerator()); } /// Compare two rational numbers for equality. bool operator==(rational const& a, rational const& b) { return a.numerator() == b.numerator() and a.denominator() == b.denominator(); } /// Compare two rational numbers for inequality. inline bool operator!=(rational const& a, rational const& b) { return not (a == b); } /// Compare two rational numbers for less-than. bool operator<(rational const& a, rational const& b) { return a.numerator() * b.denominator() < b.numerator() * a.denominator(); } /// Compare two rational numbers for less-than-or-equal. inline bool operator<=(rational const& a, rational const& b) { return not (b < a); } /// Compare two rational numbers for greater-than. inline bool operator>(rational const& a, rational const& b) { return b < a; } /// Compare two rational numbers for greater-than-or-equal. inline bool operator>=(rational const& a, rational const& b) { return not (b > a); } /// Read a rational number. /// Format is @em integer @c / @em integer. istream& operator>>(istream& in, rational& rat) { int n(0), d(0); char sep('