gpt4 book ai didi

C++ 程序返回默认值 0

转载 作者:太空宇宙 更新时间:2023-11-04 16:24:44 25 4
gpt4 key购买 nike

我写完了这个基本上只是一个矩形类的初学者 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. ";

}

最佳答案

您的 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;
}

关于C++ 程序返回默认值 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13224027/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com