狠狠干影院/欧美午夜电影在线观看/高黄文/国产精品一区二区在线观看完整版

運(yùn)算符重載實(shí)驗(yàn)報(bào)告

| 瀏覽次數(shù):

 實(shí)

  驗(yàn)

  報(bào)

  告

 課程名稱

  程序設(shè)計(jì)語(yǔ)言 C/C++

  實(shí)驗(yàn)項(xiàng)目

  運(yùn)算符重載

 一、 實(shí)驗(yàn)?zāi)康?/p>

 1、 理解重載運(yùn)算符的意義;

  2、 掌握成員函數(shù)、友元函數(shù)重載運(yùn)算符的定義及使用方法;

  3、 掌握動(dòng)態(tài)聯(lián)編、虛函數(shù)、純虛函數(shù)和抽象類的概念; 4、 掌握虛函數(shù)、純虛函數(shù)和抽象類的定義及使用方法。

 二、 實(shí)驗(yàn)內(nèi)容

 1.上機(jī)分析下面程序,掌握運(yùn)算符重載的方法。

 要求:

 (1)給出實(shí)驗(yàn)結(jié)果; (2)掌握重載“+、-、—、=”運(yùn)算符重載的方法。

 #include <iostream> using namespace std;

 class COMPLEX

 {

 public:

 COMPLEX(double r = 0, double i = 0);

 //構(gòu)造函數(shù)

  COMPLEX(const COMPLEX& other);

 //拷貝構(gòu)造函數(shù)

  void print();

  //打印復(fù)數(shù)

  COMPLEX operator +(const COMPLEX& other);

  //重載加法運(yùn)算符(二元)

  COMPLEX operator -(const COMPLEX& other);

  //重載減法運(yùn)算符(二元)

  COMPLEX operator -();

  //重載求負(fù)運(yùn)算符(一元)

  COMPLEX operator =(const COMPLEX& other);

  //重載賦值運(yùn)算符(二元)

 protected:

  double real, image;

  // 復(fù)數(shù)的實(shí)部與虛部

 };

  COMPLEX::COMPLEX(double r,double i) {

 real=r;

  image=i; }

 COMPLEX::COMPLEX(const COMPLEX& other)

 {

  real=other.real;

 image=other.image; }

 void COMPLEX::print() {

 cout<<real;

 if(image>0)

  cout<<"+"<<image<<"i";

 else if(image<0)

 cout<<image<<"i";

 cout<<endl; }

 COMPLEX

 COMPLEX::operator +(const COMPLEX& other) {

  COMPLEX temp;

  temp.real=real+other.real;

 //real=real+other.real;

 temp.image=image+other.image;

 //image=image+other.image;

 return temp;

 //*this; }

 COMPLEX COMPLEX::operator -(const COMPLEX& other) {

 COMPLEX temp;

 temp.real=real-other.real;

 temp.image=image-other.image;

 return temp;

 }

 COMPLEX COMPLEX::operator -() {

  COMPLEX temp;

 temp.real=-real;

 temp.image=-image;

 return temp; }

 COMPLEX COMPLEX::operator =(const COMPLEX& other) {

  real=other.real;

 image=other.image;

 return *this;

 //返回對(duì)象值 }

  int main()

 {

  COMPLEX c1(1, 2);

  // 定義一個(gè)值為 1 + 2i 的復(fù)數(shù) c1

  COMPLEX c2(2);

 // 定義一個(gè)值為 2 的復(fù)數(shù) c2

  COMPLEX c3(c1);

  // 用 COMPLEX(const COMPLEX& other)創(chuàng)建一個(gè)值同 c1 的新復(fù)數(shù)

  c3.print();

  // 打印 c3 原來(lái)的值

  c1=c1+c2+c3;

 // 將 c1 加上 c2 再加上 c3 賦值給 c1

  c2=-c3;

  // c2 等于 c3 求負(fù)

  c3=c2-c1;

  // c3 等于 c2 減去 c1

  c3.print();

  // 再打印運(yùn)算后 c3 的值

  cout<<sizeof(c1)<<endl;

  return 0;

 } 2. 上機(jī)分析下面程序,給出輸出結(jié)果。

 要求:

 (1)給出實(shí)驗(yàn)結(jié)果;

 (2)掌握友元運(yùn)算符重載的基本方法:通過(guò)重載 cout 語(yǔ)句,可使用 cout 輸 出對(duì)象的數(shù)據(jù)成員值的方法。

 #include<iostream.h> class T {

  int x,y;

  public:

 T(int a,int b)

 {x=a;y=b;}

 friend ostream & operator<<(ostream &os, T &a); };

 ostream & operator<<(ostream &os,T &a) { os<<"x="<<a.x<<"

 y="<<a.y;

  return os; }

  void main() {

  T a(1,2);

 cout<<a<<endl; }

  3、上機(jī)分析下面程序,掌握抽象類、純虛函數(shù)以及動(dòng)態(tài)綁定的定義和使用。

 要求:

 (1)給出實(shí)驗(yàn)結(jié)果;

 (2)掌握抽象類、純虛函數(shù)以及動(dòng)態(tài)綁定的方法。

 // shape.h 文件 定義抽象基類 Shape

 #ifndef SHAPE_H #define SHAPE_H class Shape {

  public:

 virtual double Area() const

 {

  return 0.0;

  }

 // 純虛函數(shù),在派生類中重載

 virtual void PrintShapeName() const = 0;

 virtual void Print() const = 0; }; #endif

  // point.h 文件 定義類 Point #ifndef POINT_H #define POINT_H #include <iostream> using namespace std; class Point : public Shape {

 int x, y;

  //點(diǎn)的 x 和 y 坐標(biāo)

  public:

 Point( int = 0, int = 0 );

 // 構(gòu)造函數(shù)

 void SetPoint( int a, int b );

 // 設(shè)置坐標(biāo)

 int GetX() const // 取 x 坐標(biāo)

 {

  return x;

 }

 int GetY() const // 取 y 坐標(biāo)

 {

  return y;

 }

 virtual void PrintShapeName() const

 {

  cout << "Point: ";

 }

 virtual void Print() const; //輸出點(diǎn)的坐標(biāo) }; #endif

 // Point.cpp 文件

 Point 類的成員函數(shù)定義

 #include <iostream> using namespace std;

 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 << "[" << x << ", " << y << "]"; }

  // circle.h 定義類 Circle

 #ifndef CIRCLE_H

 #define CIRCLE_H

 #include <iostream> using namespace std;

 class Circle : public Point

 {

 double radius;

 public:

 Circle(int x = 0, int y = 0, double r = 0.0);

  void SetRadius( double r );

 //設(shè)置半徑

 double GetRadius() const;

 //取半徑

 virtual double Area() const;

 //計(jì)算面積 a

 virtual void Print() const;

  //輸出圓心坐標(biāo)和半徑

 virtual void PrintShapeName() const

 {

  cout << "Circle: ";

  }

 };

 #endif

 // circle.cpp 文件

 circle 類的成員函數(shù)定義

 Circle::Circle(int a,int b,double r): Point(a,b), radius( r )

 { }

 void Circle::SetRadius( double r )

 {

 radius = ( r >= 0 ? r : 0 );

  }

 double Circle::GetRadius() const

  {

  return radius;

  }

 double Circle::Area() const

 {

  return 3.14159 * radius * radius; }

 void Circle::Print() const

 {

 cout << "Center = ";

 Point::Print();

 cout << "; Radius = " << radius << endl;

 }

 //main.cpp 文件 演示圖形類

 #include <iostream>

 using namespace std;

 void virtualViaPointer( const Shape * );

 void virtualViaReference( const Shape & );

 int main()

 {

  Point point(30,50);

  //創(chuàng)建 point、circle 對(duì)象

 Circle circle(120,80,10.0);

 point.PrintShapeName();

  //輸出 point、circle、rectangle 對(duì)象信息

 point.Print();

  cout << endl;

  circle.PrintShapeName();

  circle.Print();

  Shape *arrayOfShapes[2];

  //定義基類對(duì)象指針

 arrayOfShapes[ 0 ] = &point;

  arrayOfShapes[ 1 ] = &circle;

  cout << "Virtual function calls made off " << "base-class pointers\n";

 //通過(guò)基類對(duì)象指針訪問(wèn)派生類對(duì)象

  for ( int i = 0; i < 2; i++ )

  {

 virtualViaPointer( arrayOfShapes[ i ] );

  }

 cout << "Virtual function calls made off " << "base-class references\n";

 for ( int j = 0; j < 2; j++ )

 {

 virtualViaReference( *arrayOfShapes[ j ] );

  }

 return 0; } void virtualViaPointer( const Shape *baseClassPtr )

 //通過(guò)基類對(duì)象指針訪問(wèn)虛函數(shù)實(shí)現(xiàn)動(dòng)態(tài)綁定

 {

  baseClassPtr->PrintShapeName();

 baseClassPtr->Print();

  cout << "Area = " << baseClassPtr->Area() << endl;

 } //通過(guò)基類對(duì)象引用訪問(wèn)虛函數(shù)實(shí)現(xiàn)動(dòng)態(tài)綁定

 void virtualViaReference( const Shape &baseClassRef ) {

  baseClassRef.PrintShapeName();

  baseClassRef.Print();

 cout << "Area = " << baseClassRef.Area() << endl; } 4、上機(jī)完成下面實(shí)驗(yàn)。

 聲明一個(gè) Shape(形狀)基類,它有兩個(gè)派生類:Circle(圓)和 Square(正方形),要求利用多態(tài)性的概念,分別以虛函數(shù)的形式完成對(duì)圓和正方形的周長(zhǎng)及面積的計(jì)算。

 要求:Shape 類的數(shù)據(jù)成員包括中心點(diǎn)的坐標(biāo),Circle 類和 Square 類初始值分別給出:

 圓的圓心和半徑;正方形的中心和一個(gè)頂點(diǎn)。

 【實(shí)例編程】(參考)

 實(shí)數(shù)矩陣類 //頭文件 MatrixException.h

 #include <stdexcept>

 #include <string>

 using namespace std;

 class MatrixException : public logic_error

 {

 public:

 MatrixException(const string& s) : logic_error(s)

 {}

 };

 class Singular: public MatrixException

 {

  public:

 Singular(const string& s) : MatrixException("Singular: "+s)

 {}

 }; class InvalidIndex: public MatrixException

 {

  public:

 InvalidIndex(const string& s) : MatrixException("Invalid index: "+s)

 {}

 };

 class IncompatibleDimension: public MatrixException

 {

 public:

 IncompatibleDimension(const string& s) : MatrixException("Incompatible Dimensions: "+s)

 {} };

 //頭文件 Matrix.h //#include "MatrixException.h"

 class Matrix

 {

  private:

 double* elems;

 // 存放矩陣中各元素,按行存放

 int row, col;

  // 矩陣的行與列

  protected:

 double rowTimesCol(int i, const Matrix &b, int j ) const;

 //矩陣的第 i 行矩陣 b 的第 j 列相乘

 public:

 Matrix(int r, int c);

 Matrix(double* m, int r, int c);

 Matrix( const Matrix &m );

  ~Matrix();

 //重載運(yùn)算符" =",實(shí)現(xiàn)矩陣賦值,若進(jìn)行運(yùn)算的矩陣維數(shù)不同,拋出 IncompatibleDimension 異常

 Matrix& operator =(const Matrix& b) throw(IncompatibleDimension);

  //重載運(yùn)算符"( )",用來(lái)返回某一個(gè)矩陣元素值,若所取矩陣下標(biāo)非法,拋出 InvalidIndex 異常

 double& operator () (int i, int j) throw(InvalidIndex);

  const double& operator () (int i, int j) const throw(InvalidIndex);

  //給矩陣元素賦值,若所設(shè)置矩陣元素下標(biāo)非法,拋出 InvalidIndex 異常

 void SetElem(int i, int j, double val) throw(InvalidIndex);

 //重載運(yùn)算符"*",實(shí)現(xiàn)矩陣相乘, 若前一個(gè)矩陣的列數(shù)不等于后一個(gè)矩陣的行數(shù),拋出 IncompatibleDimension 異常

  Matrix operator *(const Matrix& b) const throw(IncompatibleDimension);

  //重載運(yùn)算符" +",實(shí)現(xiàn)矩陣相加,若進(jìn)行運(yùn)算的矩陣維數(shù)不同,拋出 IncompatibleDimension 異常

  Matrix operator +(const Matrix& b) const throw(IncompatibleDimension);

  //重載運(yùn)算符" -",實(shí)現(xiàn)矩陣相減//若進(jìn)行運(yùn)算的矩陣維數(shù)不同,拋出 IncompatibleDimension 異常

  Matrix operator -(const Matrix& b) const throw(IncompatibleDimension);

 void Print() const;

 //按行顯示輸出矩陣中各元素

 };

 //成員函數(shù)的定義文件 Matrix.cpp

 #include <iostream>

 #include <strstream>

 #include <string>

 //#include "Matrix.h"

 using namespace std;

 string int2String(int i)

 //將整型數(shù)轉(zhuǎn)換為 String 類型

 {

 char buf[64];

 ostrstream mystr(buf, 64);

 mystr << i << "\0";

 return string(buf);

 }

 Matrix::Matrix(int r, int c)

 {

 if ( r > 0 && c > 0 )

 {

  row = r;

 col = c;

  elems = new double[r * c]; //為矩陣動(dòng)態(tài)分配存儲(chǔ)

 }

  else

 {

 elems = NULL;

 row=col=0;

  }

 } Matrix::Matrix( double* m, int r, int c )

  {

 if ( r > 0 && c > 0 )

  {

  row = r;

 col = c;

 elems = new double[r * c];

 }

  else

 {

  elems = NULL;

  row=col=0;

  }

  if ( elems != NULL )

  {

 for (int i=0; i < r*c; i++)

 {

 elems[i] = m[i];

  }

 }

 }

 Matrix::Matrix( const Matrix &m ) : row( m.row ), col( m.col ) {

 if ( NULL == m.elems )

 {

  elems = NULL;

 }

 else

 {

 int total = row*col;

  elems = new double[total];

  for( int i = 0; i < total; ++i )

 {

 elems[i] = m.elems[i];

  }

  }

 }

 Matrix::~Matrix()

 {

  delete []elems; //釋放矩陣所占的存儲(chǔ)

 } Matrix& Matrix::operator =(const Matrix& b)

 throw(IncompatibleDimension)

 {

 if( this == &b )

 {

  return *this;

 }

 if ( col != b.col || row !=b.row

 )

 {

  throw( IncompatibleDimension(" Matrix "+int2String(row)

 + " x " + int2String(col) + " equails matrix "

 + int2String(b.row) + " x " + int2String(b.col)+".") );

 }

  row = b.row;

 col = b.col;

 delete []elems;

 if ( NULL == b.elems )

  {

 elems = NULL;

 }

 else

  {

 int total = row*col;

  elems = new double[total];

  for( int i = 0; i < total; ++i )

  {

 elems[i] = b.elems[i];

  }

 }

  return *this;

 } //重載運(yùn)算符"( )"可以由給出的矩陣行列得到相應(yīng)的矩陣元素值。之所以重載"( )"而不是"[]",是因?yàn)楸苊鈹?shù)組 elems 所造成的二義性

 double& Matrix::operator () (int r, int c) throw(InvalidIndex)

 {

 if ( r<0 || r>=row || c<0 || c>=col )

 {

 throw( InvalidIndex(string("Get Element(")

  + int2String(r) + "," + int2String(c) + ")"

  + " from ("

 + int2String(row) + " x "

  + int2String(col) + ")" + " matrix." ) );

 }

 return elems[r*col+c];

 }

 const double& Matrix::operator () (int r, int c) const throw(InvalidIndex)

 {

 if ( r<0 || r>=row || c<0 || c>=col )

 {

 throw( InvalidIndex(string("Get Element(")

  + int2String(r) + "," + int2String(c) + ")"

  + " from ("

 + int2String(row) + " x "

 + int2String(col) + ")" + " matrix." ) );

 }

 return elems[r*col+c];

 }

 void Matrix::SetElem(int r, int c, double val) throw(InvalidIndex)

 {

 if ( r<0 || r>=row || c<0 || c>=col )

 {

  throw(InvalidIndex(string("Set Element(")

  + int2String(r) + "," + int2String(c) + ")"

  + " for ("

 + int2String(row) + " x "

 + int2String(col) + ")" + " matrix." ) );

 }

 elems[r*col+c] = val;

 }

 Matrix Matrix::operator*(const Matrix& b) const throw(IncompatibleDimension)

 {

 if ( col != b.row )

  // incompatible dimensions

 {

  throw(IncompatibleDimension(" Matrix "+int2String(row)

 + " x " + int2String(col) + " times matrix "

 + int2String(b.row) + " x " + int2String(b.col)+"."));

 }

 Matrix ans(row, b.col);

 for (int r=0 ; r < row ; r++)

 {

 for (int c=0 ; c < b.col; c++)

  {

 ans.SetElem(r, c, rowTimesCol(r, b, c) );

 }

 }

 return ans; }

 Matrix Matrix::operator +(const Matrix& b) const throw(IncompatibleDimension)

 {

 if ( col != b.col || row !=b.row

 )

  {

 throw(IncompatibleDimension(" Matrix "+int2String(row)

 + " x " + int2String(col) + " adds matrix "

 + int2String(b.row) + " x " + int2String(b.col)+"."));

 }

 Matrix ans(row, col);

 for (int r=0 ; r < row ; r++)

 {

 for (int c=0 ; c < col; c++)

 {

  ans.SetElem(r, c, elems[r*col+c]+b.elems[r*col+c]);

 }

 }

 return ans;

 }

 Matrix Matrix::operator -(const Matrix& b) const throw(IncompatibleDimension)

 {

 if ( col != b.col || row !=b.row

 )

  {

 throw( IncompatibleDimension(" Matrix "+int2String(row)

 + " x " + int2String(col) + " minus matrix "

 + int2String(b.row) + " x " + int2String(b.col)+".") );

 }

 Matrix ans(row, col);

 for (int r=0 ; r < row ; r++)

 {

 for (int c=0 ; c < col; c++)

 {

  ans.SetElem(r, c, elems[r*col+c]-b.elems[r*col+c]);

  }

 }

 return ans; }

 void Matrix::Print() const

 {

  for (int i = 0; i < row; i++)

 {

 for (int j = 0; j < col-1; j++)

 {

 cout << elems[i*col+j] << "\t";

  }

 cout << elems[i*col+col-1];

  cout<<endl;

  }

 cout<<endl;

 }

 double Matrix::rowTimesCol( int i, const Matrix &b, int j ) const

 {

 double sum=0.0;

 for (int k = 0; k < col; k++)

 {

  sum += elems[i*col+k] * b.elems[k*b.col+j];

 }

 return sum; }

 //測(cè)試文件 MatrixMain.cpp

 #include <iostream>

 //#include "Matrix.h"

 using namespace std;

 int main()

 {

 try

 {

  //創(chuàng)建矩陣對(duì)象

  double a[20]= {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 b[15]= {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);

 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;

 //兩個(gè)矩陣相乘

 cout<<"the matrix of z1=x1*y1 is:"<<endl;

 z1.Print();

 Matrix x2(2, 2);

 x2.SetElem(0, 0, 1.0); //為矩陣對(duì)象添加元素

 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;

  //兩個(gè)維數(shù)不匹配矩陣相乘,產(chǎn)生異常

 cout<<"x1-x2 is:"<<endl;

 x1-x2;

  //兩個(gè)維數(shù)不匹配矩陣相減,產(chǎn)生異常

  cout<<"Set a new element for x1:"<<endl;

 x1.SetElem (7, 8, 30);

 //設(shè)置矩陣元素超界,產(chǎn)生異常

 cout<<"Get a element from x1:"<<endl;

 x1(4, 5);

  // 取不存在的矩陣元素,產(chǎn)生異常

 }

  catch(MatrixException& e)

  {

  cout << e.what() << endl; //獲取異常信息

 }

 return 0;

 } 三、實(shí)驗(yàn) 步驟 及結(jié)果分析

 1. 程序的類結(jié)構(gòu)圖為:

 COMPLEX

 #real:double #image:double +COMPLEX(double r=0,double i=0) +COMPLEX(const COMPLEX &other) +print():void

 +operator+(const COMPLEX &other): COMPLEX +operator-(const COMPLEX &other) : COMPLEX +operator-(): COMPLEX +operator=(const COMPLEX &other) : COMPLEX

 運(yùn)行結(jié)果

 2. 程序的類結(jié)構(gòu)圖為:

 T T

 x, y:int

 +T(int a,int b)

 +&operator<<(ost ream &os,T &a):friend ostream

 運(yùn)行結(jié)果

  3. 程序的類結(jié)構(gòu)圖為:

 Shape

 + +A A rea():virtual double

 const

 +PrintShapeName():virtual void

 const

 +Print():virtual void

 const

 Point x,y:int +Point(int=0,int=0)

 +SetPoint (in t a,int b):void

 +GetX():int const

 +G etY():int const

 +PointShapeName():virtual void const

 +Print():virtual void const

 Circle

 r r adius:double

 + + Circle(int x=0,int y=0,double r=0.0)

 + + SetRadius(double r):void

 + + GetRadius():double const

 + + Area():virtual dou ble con st

 + + Print():virtual void const

 + + PrintShapeName():virtual void const

  運(yùn)行結(jié)果

 4. 程序的類結(jié)構(gòu)圖為:

 Shape1 1

 #x_size,y_size:double

 +Shape1 1 (double x,double y)

 +area():virtual double

 +perimeter():virtual double e

 +print():virtual void

 相關(guān)代碼:

 #include<iostream>

 using namespace std; class Shape1 {

 protected:

 double x_size,y_size;

 public:

  Shape1(double x,double y);

 // 構(gòu)造函數(shù)

  virtual double area()

  // 純虛函數(shù),在派生類中重載

  {

 return 0.0; Circle1 1

 #radiu s:double

 +Ci rcle 1 (double e

 x=0.0,double y=0.0,double r=0.0)

 +set_radius(double r=0.0):void

 +get_radius():double

 +area():virtual double

 +perimeter():virtual double

 +print():virtual void

 Square1 1

 #i_size,j_size: double

 +Square1 1 (double x=0.0,double y=0.0,

 d d ouble

 i=0.0 ,double j=0.0)

 +set_i(double i):void

 +set_j(double_j):void

 +get_i():void

 + +g g et_j():void

 +area():virtual double

 +perimeter():virtual double

 +print():virtual void

 }

  virtual double perimeter()

  {

 return 0.0;

  }

  virtual void print()=0; };

 Shape1::Shape1(double a,double b) {

 x_size=a;

 y_size=b; }

 class Circle1:public Shape1 {

 protected:

 double radius;

 public:

  Circle1(double x=0.0,double y=0.0,double r=0.0); // 構(gòu)造函數(shù)

  void set_radius(double r=0.0);

  //設(shè)置半徑

  double get_radius();

  //輸出半徑

  virtual double area();

  virtual double perimeter();

  virtual void print();

  //輸出圓心坐標(biāo)和半徑 };

 Circle1::Circle1(double x,double y,double r):Shape1(x,y),radius(r)//構(gòu)造函數(shù) {} void Circle1::set_radius(double r) {

 radius=r; } double Circle1::get_radius() {

 return radius; } double Circle1::area() {

 return 3.14159*radius*radius; } double Circle1::perimeter() {

 return 3.14159*radius*2; } void Circle1::print()

  //輸出圓心坐標(biāo) {

  cout<<"["<<x_size<<","<<y_size<<"]"<<", "<<radius; }

 class Square1:public Shape1 {

  protected:

 double i_size,j_size;

 public:

  Square1(double x=0.0,double y=0.0,double i=0.0,double j=0.0); // 構(gòu)造函數(shù)

  void set_i(double i);

 //設(shè)置頂點(diǎn)橫坐標(biāo)

  void set_j(double j);

 //設(shè)置頂點(diǎn)縱坐標(biāo)

  double get_i();

 //輸出頂點(diǎn)橫坐標(biāo)

  double get_j();

 //輸出頂點(diǎn)縱坐標(biāo)

  virtual double area();

  virtual double perimeter();

  virtual void print();

 //輸出中心坐標(biāo)和頂點(diǎn)坐標(biāo)

 };

 Square1::Square1(double x,double y,double i,double j):Shape1(x,y),i_size(i),j_size(j) {}

 void Square1::set_i(double i) {

 i_size=i; } void Square1::set_j(double j) {

 j_size=j; } double Square1::get_i() {

 return i_size; } double Square1::get_j() {

 return j_size; } double Square1::area() {

 return 2*(i_size-x_size)*2*(j_size-y_size); } double Square1::perimeter() {

 return 4*2*(i_size-x_size); } void Square1::print()

 {

 cout<<"["<<x_size<<","<<y_size<<"]"<<", "<<"["<<i_size<<","<<j_size<<"]"; }

 int main()

 {

  Circle1 circle(0.0,0.0,3.0);

  circle.area();

  circle.perimeter();

  circle.print();

 cout<<"\n";

  Square1 square(0.0,0.0,3.0,3.0);

  square.area();

  square.perimeter();

  square.print();

 cout<<"\n";

  cout<<"圓的面積為:"<<circle.area()<<endl;

  cout<<"圓的周長(zhǎng)為:"<<circle.perimeter()<<endl;

  cout<<"圓的圓心坐標(biāo)和半徑為:";

 circle.print();

 cout<<"\n\n";

  cout<<"正方形的面積為:"<<square.area()<<endl;

  cout<<"正方形的周長(zhǎng)為:"<<square.perimeter()<<endl;

  cout<<"正方形的中心坐標(biāo)和一個(gè)頂點(diǎn)坐標(biāo)分別為:";

 square.print();

  cout<<"\n";

 return 0; } 運(yùn)行結(jié)果

 【實(shí)例編程】

 運(yùn)行結(jié)果

推薦訪問(wèn): 重載 運(yùn)算符 實(shí)驗(yàn)

【運(yùn)算符重載實(shí)驗(yàn)報(bào)告】相關(guān)推薦

工作總結(jié)最新推薦

NEW