0

基本的に単なる長方形クラスであるこの初心者向けの C++ プログラムを書き終えました。プログラムは問題なくコンパイルされます。私が抱えている唯一の問題は、幅にゼロが返され、その理由がわかりません。誰かが私が間違っている場所を教えてくれ、助けてくれたら本当にありがたいです.

#pragma once
class theRectangle
{
private:
    int height;
    int width;

public:


    theRectangle();

    theRectangle(int _height, int _width);

    int calcArea() const;

    int calcPerimeter() const;

    bool is_Square()const;

    int Get_Height() const;

    void Set_Height(int _hight);

    int Get_Width() const;

    void Set_Width(int _width);

};



#include "theRectangle.h"
#include <iostream>
using namespace std;
//default constructor
theRectangle::theRectangle()
{
    height = 0;
    width = 0;
}

//parameterized constructor
theRectangle::theRectangle(int _height, int _width)
{

    height = _height;
    width = _width;

}
//get height function
int theRectangle::Get_Height() const
{

    return height;
}
//get width function
int theRectangle::Get_Width() const
{

    return width;
}

//calculate area function
int theRectangle::calcArea() const
{
    return (height*width);
}
//calculate perimiter function
int theRectangle::calcPerimeter() const
{
    return (height+height+width+width);
}
//is square function
bool theRectangle::is_Square()const
{
    if(height == width)
    {
        return true;
    }
    else
    {
        return false;
    }
}

//set height function
void theRectangle::Set_Height(int _height)
{
    height = _height;
}
//set width function
void theRectangle::Set_Width(int _width)
{
    height = _width;
}





#include "theRectangle.h"
#include <iostream>
#include <string>


using namespace std;

void printRectangle(const theRectangle& rec);

int main()
{



    theRectangle rectangleONE;
    int number;
    int numbertwo;


    cout << "\n Please type the height the 1st rectangle:  ";
    cin >> number;
    rectangleONE.Set_Height(number);
    cout << "\n Please type the width of the 1st rectangle: ";
    cin >> numbertwo;
    rectangleONE.Set_Width(numbertwo);


    theRectangle rectangleTWO;
    int numberthree;
    int numberfour;
    //ask user/save data
    cout << "\n Please type the height of the 2nd rectangle:  ";
    cin >> numberthree;
    rectangleTWO.Set_Height(numberthree);
    cout << "\n Please type the width of the 2nd rectangle: ";
    cin >> numberfour;
    rectangleTWO.Set_Width(numberfour);


    theRectangle rectangleTHREE;
    int numberfive;
    int numbersix;


    cout << "\n Please type the height of the 3rd rectangle:  ";
    cin >> numberfive;
    rectangleTHREE.Set_Height(numberfive);
    cout << "\n Please type the width of the 3rd rectangle:  ";
    cin >> numbersix;
    rectangleTHREE.Set_Width(numbersix);

    //print data
    cout << "\nFor rectangle one: ";
    printRectangle(rectangleONE);
    cout << "\nFor rectangle two: ";
    printRectangle(rectangleTWO);
    cout << "\nFor rectangle three: ";
    printRectangle(rectangleTHREE);

system("PAUSE");
return 0;
}


//print rectangle function
// Purpose: print info on rectangle
// Parameters:none
// Returns: none
void printRectangle(const theRectangle& rec)
{
    cout << "\nHeight is:  "    << rec.Get_Height();
    cout << "\nWidth is:  "     << rec.Get_Width();
    cout << "\nArea is:  "      << rec.calcArea();
    cout << "\nPerimeter is:  " << rec.calcPerimeter();
    if (rec.is_Square())
        cout << "\n This rectangle is a Square. ";  

}
4

1 に答える 1

5

関数setWidthは実際にheightメンバー変数を設定し、widthメンバーを0に設定したままにします:

//set width function
void theRectangle::Set_Width(int _width)
{
    height = _width;
}

そのはず:

//set width function
void theRectangle::Set_Width(int _width)
{
    width = _width;
}
于 2012-11-04T23:24:50.940 に答える