Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

C++ 請各位大大幫個忙

假設我們已有了下面的CRectangle類別:

class CRectangle

{

int width;

int height;

}

(a) 試為CRectangle類別設計一個建構元CRectangle(w,h),當此建構元呼叫時

, 便會自動設定width=w, hieght=h

(b) 請接續(a)的部分,請再設計一個建構元CRectangle(),使得當此建構元呼

叫時,便會自動設定width=10, height=10

1 Answer

Rating
  • MrAYD
    Lv 5
    1 decade ago
    Favorite Answer

    #include "stdafx.h"

    #include <iostream>

    using namespace std;

    class CRectangle

    {

    private:

    int width;

    int height;

    public:

    CRectangle(int width,int height)

    {

    this->width=width;

    this->height=height;

    }

    CRectangle()

    {

    this->width=10;

    this->height=10;

    }

    friend ostream& operator<<(ostream& os,CRectangle p)

    {

    os<<"("<<p.width<<","<<p.height<<")";

    return os;

    }

    };

    int _tmain(int argc, _TCHAR* argv[])

    {

    cout<<CRectangle()<<endl;

    cout<<CRectangle(2,5);

    cin.ignore(2);

    return 0;

    }

    Source(s): ME
Still have questions? Get your answers by asking now.