classMatrix{ private: int row; int col; public: Matrix(int r, int c); Matrix(void); voidsetMatrix(int r, int c); pair<int,int> getMatrix(void); ~Matrix(); // 析构函数声明 };
// 成员函数定义 Matrix::Matrix(void){ cout << "Object is created by Constructor 1" << endl; } Matrix::Matrix(int r, int c) : row(r), col(c) { cout << "Object is created by Constuctor 2" << endl; } Matrix::~Matrix(void){ cout << "Object is being deleted" << endl; }
voidMatrix::setMatrix(int r, int c){ row = r, col = c; } pair<int,int> Matrix::getMatrix(void){ returnpair<int,int>(row, col); }
/* 该程序的输出结果为: =================== Object is created by Constructor 1 Object is created by Constuctor 2 p1 : (50, 0), p2 : (2, 3), p3 : (4, 5) Object is being deleted Object is being deleted =================== */
/* 该程序的输出结果为: =================== Object created Main begins. Object created Object created Object created Destructor called ------------ Object created copy func Destructor called Test copy Destructor called after Test. ------------ Object created Object created Destructor called Destructor called Main ends. Destructor called Destructor called Destructor called Destructor called =================== */