1、 实验 1 类和对象 一、实验目的和要求 1掌握声明类的方法,类和类的成员概念以及定义对象的方法; 2掌握成员函数的实现与调用方法。 ; 3掌握引检查和调试基于对象的程序的方法; 4. 深刻领会类与对象的区别,类实现数据隐藏与封装的原理等 二、实验内容 任务 1:程序调试与程序设计 调试下列程序,程序描述了一个圆柱的类,成员中有私有数据半径 r 及高 h,公有的成 员函数有构造函数与输出圆柱参数的函数,在 main 函数中,输入两个参数,定义并初始化 此类的一个对象。 (1)请测试。 #include class column private: double r,h; public: colu
2、mn(double ri,double hi) r=ri; h=hi; void prin() cout class Student public: / void clacTuition() /计算学费 cout class Base public: virtual void fn(int x) cout class Shape public: virtual double Area() const return 0.0; / 纯虚函数,在派生类中重载 virtual void PrintShapeName() const = 0; virtual void Print() const = 0
3、; ; #endif / point.h 文件 定义类 Point #ifndef POINT_H #define POINT_H #include “shape.h“ class Point : public Shape int x, y; /点的 x 和 y 坐标 public: Point( int = 0, int = 0 ); / 构造函数 void SetPoint( int a, int b ); / 设置坐标 int GetX() const / 取 x 坐标 return x; int GetY() const / 取 y 坐标 return y; virtual void
4、PrintShapeName() const cout #include “point.h“ Point:Point( int a, int b ) : x( a ), y( b ) void Point:SetPoint( int a, int b ) x = a; y = b; void Point:Print() const cout #include “point.h“ class Circle : public Point double radius; public: Circle(int x = 0, int y = 0, double r = 0.0); void SetRadi
5、us( double r ); /设置半径 double GetRadius() const; /取半径 virtual double Area() const; /计算面积 a virtual void Print() const; /输出圆心坐标和半径 virtual void PrintShapeName() const cout = 0 ? r : 0 ); double Circle:GetRadius() const return radius; double Circle:Area() const return 3.14159 * radius * radius; void Ci
6、rcle:Print() const cout #include “shape.h“ #include “point.h“ #include “circle.h“ void virtualViaPointer( const Shape * ); void virtualViaReference( const Shape int main() /创建 point、circle 对象 Point point(30,50); Circle circle(120,80,10.0); /输出 point、circle 、rectangle 对象信息 point.PrintShapeName(); poi
7、nt.Print(); cout PrintShapeName(); baseClassPtr-Print(); cout Area() class RMB /人民币类 public: RMB(double d) yuan=d; jf=(d-yuan)/100; RMB interest(double rate); /计算利息 RMB add(RMB d); /人民币加 void display() cout class RMB public: RMB(unsigned int d, unsigned int c); friend RMB operator +(RMB friend RMB v
8、oid display() cout =100 ) /以使构造时,确保角分值小于 100 yuan +; jf -= 100; RMB operator+(RMB unsigned int yuan = s1.yuan + s2.yuan; RMB result( yuan, jf ); return result; RMB if(s.jf = 100) s.jf -= 100; s.yuan+; return s; void main() RMB d1(1, 60); RMB d2(2, 50); RMB d3(0, 0); d3 = d1 + d2; +d3; d3.display();
9、(3) 上机分析下面程序,掌握运算符重载的方法。 #include class Complex friend Complex operator+( const double friend Complex operator-( const double double m_fReal, m_fImag; public: Complex( const double double GetImag() const return m_fImag; Complex m_fReal=c.m_fReal; m_fImag=c.m_fImag; return *this; Complex operator+( c
10、onst Complex Complex operator-( const Complex Complex operator+( const double Complex operator-( const double ; Complex operator+( const double Complex operator-( const double int main() Complex c1( 3.3, 4.4 ); Complex c2( 5.5, 2.2 ); Complex c3; c3 = c1 + c2; cout #include using namespace std; clas
11、s MatrixException : public logic_error public: MatrixException(const string class Singular: public MatrixException public: Singular(const string class InvalidIndex: public MatrixException public: InvalidIndex(const string class IncompatibleDimension: public MatrixException public: IncompatibleDimens
12、ion(const string /头文件 Matrix.h #include “MatrixException.h“ class Matrix double* elems; / 存放矩阵中各元素,按行存放 int row, col; / 矩阵的行与列 protected: double rowTimesCol(int i, const Matrix /矩阵的第 i 行矩阵 b 的 第 j 列相乘 public: Matrix(int r, int c); Matrix(double* m, int r, int c); Matrix( const Matrix Matrix(); /重载运算
13、符“ =“,实现矩阵赋值,若进行运算的矩阵维数不同,抛出 IncompatibleDimension 异常 Matrix /重载运算符“( )“ ,用来返回某一个矩阵元素值,若所取矩阵下标非法,抛出 InvalidIndex 异常 double const double /给矩阵元素赋值,若所设置矩阵元素下标非法,抛出 InvalidIndex 异常 void SetElem(int i, int j, double val) throw(InvalidIndex); /重载运算符“*“,实现矩阵相乘, /若前一个矩阵的列数不等于后一个矩阵的行数,抛出 IncompatibleDimensio
14、n 异常 Matrix operator *(const Matrix /重载运算符“ +“,实现矩阵相加,若进行运算的矩阵维数不同,抛出 IncompatibleDimension 异常 Matrix operator +(const Matrix /重载运算符“ -“,实现矩阵相减/若进行运算的矩阵维数不同,抛出 IncompatibleDimension 异常 Matrix operator -(const Matrix void Print() const; /按行显示输出矩阵中各元素 ; /成员函数的定义文件 Matrix.cpp #include #include #include
15、 #include “Matrix.h“ using namespace std; string int2String(int i) /将整型数转换为 String 类型 char buf64; ostrstream mystr(buf, 64); mystr 0 col = c; elems = new doubler * c; /为矩阵动态分配存储 else elems = NULL; row=col=0; Matrix:Matrix( double* m, int r, int c ) if ( r 0 col = c; elems = new doubler * c; else ele
16、ms = NULL; row=col=0; if ( elems != NULL ) for (int i=0; i =row | c=col ) throw( InvalidIndex(string(“Get Element(“) + int2String(r) + “,“ + int2String(c) + “)“ + “ from (“ + int2String(row) + “ x “ + int2String(col) + “)“ + “ matrix.“ ) ); return elemsr*col+c; const double return elemsr*col+c; void
17、 Matrix:SetElem(int r, int c, double val) throw(InvalidIndex) if ( r=row | c=col ) throw(InvalidIndex(string(“Set Element(“) + int2String(r) + “,“ + int2String(c) + “)“ + “ for (“ + int2String(row) + “ x “ + int2String(col) + “)“ + “ matrix.“ ) ); elemsr*col+c = val; Matrix Matrix:operator*(const Ma
18、trix i #include “Matrix.h“ using namespace std; int main() try /创建矩阵对象 double a20= 1.0, 3.0, -2.0, 0.0, 4.0, -2.0, -1.0, 5.0, -7.0, 2.0, 0.0, 8.0, 4.0, 1.0, -5.0, 3.0, -3.0, 2.0, -4.0, 1.0; double b15= 4.0, 5.0, -1.0, 2.0, -2.0, 6.0, 7.0, 8.0, 1.0, 0.0, 3.0, -5.0, 9.0, 8.0, -6.0; Matrix x1(a, 4, 5);
19、 cout“the matrix of x1 is:“endl; x1.Print(); Matrix y1(b, 5, 3); cout“the matrix of y1 is:“endl; y1.Print(); Matrix z1 = x1*y1; /两个矩阵相乘 cout“the matrix of z1=x1*y1 is:“endl; z1.Print(); Matrix x2(2, 2); x2.SetElem(0, 0, 1.0); /为矩阵对象添加元素 x2.SetElem(0, 1, 2.0); x2.SetElem(1, 0, 3.0); x2.SetElem(1, 1, 4.0); cout“the matrix of x2 is:“endl; x2.Print(); cout“x1*x2 is:“endl; x1*x2; /两个维数不匹配矩阵相乘,产生异常 cout“x1-x2 is:“endl; x1-x2; /两个维数不匹配矩阵相减,产生异常 cout“Set a new element for x1:“endl; x1.SetElem (7, 8, 30); /设置矩阵元素超界,产生异常 cout“Get a element from x1:“endl;