重载构造函数,一个带有两个参数,一个带有 none

overloaded constructors one with two parameters and one with none

设计并创建一个具有重载构造函数的矩形 class。第一个构造函数不需要 参数。第二个有两个参数,一个用于长度,一个用于宽度。成员变量存储 矩形的长度和宽度,以及成员方法分配和检索长度和宽度以及 return 长方形的面积和周长。通过编写适当的客户端代码测试 class。

这是给我的任务,我不太明白如何开始这样的事情,它只是五部分之一,我希望能得到一些帮助。

//Rectangle class
class Rectangle{
  private int length;
  private int width;

  Rectangle(){
    this.length=1; // assuming default length=1
    this.width=1; // assuming default width=1
  }

  Rectangle(int length, int width){
    this.length=length; 
    this.width=width; 
  }

int area(){
   return length*width;
}
int perimeter(){
  return 2*(length+width);
}
}


// test class

public class TestRectangle{
    public static void main(String args[]){
        Rectangle r1= new Rectangle();
        System.out.println("Area of r1: "+ r1.area());
        Rectangle r2= new Rectangle(2,3);
        System.out.println("Perimetr of r2: "+ r2.perimeter());
    }
}
// http://www.cplusplus.com/doc/tutorial/classes/
//       Classes (I)
//         Overloading constructors

#include <cstdlib>  // contains EXIT_SUCCESS, EXIT_FAILURE
#include <iostream> // cout

// define class
class Rectangle
{
  int width, height;

  public:
    Rectangle ( );           // default constructor
    Rectangle ( int, int );  // overload constructor
    int area ( void ) { return ( width * height ); }
};

// default constructor
Rectangle::Rectangle ( )
{
  width  = 2;
  height = 3;
}

// overloaded constructor
Rectangle::Rectangle ( int a, int b )
{
  width  = a;
  height = b;
}

int main ( )
{
  // Two objects of class Rectangle are constructed: rectd and recto.
  // The object rectd is constructed using the default constructor
  // The object recto is constructed with two arguments (overloaded constructor)
  Rectangle recto ( 4, 5 );
  Rectangle rectd;  // empty parentheses cannot be used to call the default constructor

    std::cout << "rectangle area, default constructor:    " << rectd.area() << std::endl;
    std::cout << "expected answer:                        6 = 2 x 3\n" << std::endl;

    std::cout << "rectangle area, overloaded constructor: " << recto.area() << std::endl;
    std::cout << "expected answer:                        20 = 4 x 5"  << std::endl;

  return EXIT_SUCCESS;
}

代码用g++编译;不需要 C++11 或更高版本。

为了好玩,我决定用 C# 来做这个

    static void Main(string[] args)
    {

        Rectangle rect = new Rectangle();//length 2, width 1
        Rectangle yourRect = new Rectangle(12.34, 25.022);
        Console.WriteLine(rect.area() + " area of puny rect\n" + yourRect.area() + " area of bigger and better rect");
        Console.WriteLine(rect.perimeter() + " perimeter of puny rect\n" + yourRect.perimeter() + " perimeter of bigger and better!");
        //change things
        rect.length = 23.33;
        rect.width = 122222.333;
        Console.WriteLine(rect.area() + " WHOA look at that!\n" + rect.perimeter() + " WHHHAAATTT that is much bigger!");
        Console.ReadKey();//this is here to look at output without it disapearing.
    }

}
class Rectangle
{
    //simple rectangle class using C#
    /**
     * Using double will allow you to use decimals which 
     * is much more accurate than int
     * */
    public double width { get; set; }
    public double length { get; set; }

    public Rectangle()
    {
        this.length = 2.0;
        this.width = 1.0;
    }

    public Rectangle(double length, double width)
    {
        this.length = length;
        this.width = width;
    }
    public double area()
    {
        return length * width;
    }
    public double perimeter()
    {
        return 2 * (length + width);
    }
}

代码中有用的小注释,欣赏。顺便说一句,我从上面的 Farooque Java 设计中借用了基本设计。

//C++ CODE
#include<iostream>
using namespace std;
class Rectangle{
private:
double length;
double breadth;
public:
Rectangle(){
    length=0;
    breadth=0;
}
Rectangle(double x,double y){
    length=x;
    breadth=y;
}
Rectangle(double lb){
    length=lb;
    breadth=lb;
}
void display(){
  cout<<"Area of rec. is "<<length*breadth<<endl;
}
};
int main(){
Rectangle r1;
Rectangle r2{ 10 };
Rectangle r3{ 15.6,45 };
r1.display();
r2.display();
r3.display();
return 0;
}