C++ 学习之 - 基本语言 - C++ 类
C++类的关键特征是接口与实现的分离。
接口(Public)是一些“用户可以应用到类对象上的操作”的集合,它由三部分组成:操作的名字,返回值,参数表
私有实现(Private) 支持Public 所必需的算法和数据。
理想情况下,即使类的接口增长了,它也不用变得与以前的版本不相兼容;另一方面在类的生命周期内其实现可以自由演化!
class Matrix{
public:
Matrix(int=defaultRowSize,int=defaultColumnSize);
Matrix(const Matrix&);
~Matrix();
Matrix &operator=(const Matrix&);
bool operator==(const Matrix&) const;
bool operator!=(const Matrix&) const;
int &operator*()(int,int);
int operator()(int,int) const;
int getRowSize() const;
int getColumnSize() const;
friend ostream &operator<<(ostream &,const Matrix &);
friend istream &operator>>(istream &,Matrix &);
private:
int **im;
int rows;
int cols;
static const int defaultRowSize=5;
static const int defaultColumnSize=5;
};

浙公网安备 33010602011771号