1. 程式人生 > >編寫一個類Rectangle,有長itsLength,寬itsWidth等資料成員,有過載的建構函式Rectangle()、Rectangle(int width,int length)。

編寫一個類Rectangle,有長itsLength,寬itsWidth等資料成員,有過載的建構函式Rectangle()、Rectangle(int width,int length)。

#include<iostream>
using namespace std;

class Rectangle
{
public:
   Rectangle() {itsLength=10,itsWidth=5;}
   Rectangle(int length ,int width) {itsLength=length;itsWidth=width;}
   ~Rectangle() {}
   int GetLength() {return itsLength;}
   int GetWidth() {return itsWidth;}
private:
   int itsLength,itsWidth;
};

int main()
{
  Rectangle rect1;
  cout<<"Length: "<<rect1.GetLength()<<endl;
  cout<<"Width: "<<rect1.GetWidth()<<endl;
     int length,width;
     cout<<"Enter the length:"<<endl;
     cin>>length;
     cout<<"Enter the width:"<<endl;
     cin>>width;
     Rectangle rect2(length,width);
     cout<<"The Length: "<<rect2.GetLength()<<endl;
     cout<<"The Width: "<<rect2.GetWidth()<<endl;
}