c ++程序返回默认值0

时间:2012-11-04 23:23:30

标签: c++ class object

我写完这个初学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. ";  

}

1 个答案:

答案 0 :(得分: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;
}