C ++类矩形赋值

时间:2017-07-24 01:38:59

标签: c++ pointers floating-point return-value

我正在为这个赋值编写一个类矩形创建一个具有属性长度和宽度的类Rectangle,每个默认为1.提供计算矩形周长和面积的成员函数。另外,为length和width属性提供set和get函数。 set函数应验证长度和宽度是否都是大于0.0且小于20.0的浮点数。

我不断收到错误建议setLengthandWidth,setLength和setWidth必须返回一个值。我不确定我哪里出错了。有什么建议?这是我的3个文件。

标头文件

#ifndef Rectangle_H
#define Rectangle_H
class Rectangle
{
public:
    Rectangle();
    Rectangle(float length);
    Rectangle(float length, float width);
    ~Rectangle();
    float setLengthAndWidth(float, float);
    float setLength(float Length);
    float setWidth(float Width);
    float calculatePerimeter();
    float calculateArea();
    void printInfo();
    float getLength();
    float getWidth();
private:
    float length;
    float width;
    float area;
    float perimeter;
};
#endif#pragma once

main.cpp文件

#include <iostream>
#include <iomanip>
#include <cmath>
#include "Rectangle.h"
using namespace std;
int main()
{
    Rectangle objectOne;
    Rectangle objectTwo(7.1, 3.2);
    Rectangle objectThree(6.3);
    Rectangle objectFour(200, 300);
    Rectangle objectFive = objectTwo;
    cout << "The first objects information is\n  ";
    objectOne.printInfo();
    cout << "The second objects information is\n  ";
    objectTwo.printInfo();
    cout << "The third objects information is\n  ";
    objectThree.printInfo();
    cout << "The fourth objects information is\n  ";
    objectFour.printInfo();
    cout << "The fifth objects information is\n  ";
    objectFive.printInfo();
}

成员.cpp文件

#include <iostream>
#include <iomanip>
#include <cmath>
#include "Rectangle.h"
using namespace std;
Rectangle::Rectangle()
{
    length = width = 1.0;
}
Rectangle::Rectangle(float length)
{
    setLengthAndWidth(length, 1.0);
}
Rectangle::Rectangle(float length, float width)
{
    setLengthAndWidth(length, width);
}
float Rectangle::setLengthAndWidth(float Len, float Wid)
{
    setLength(Len);
    setWidth(Wid);
}
float Rectangle::setLength(float length)
{
    if (length >= 0 || length <= 20.0)
        length = length;
    else
        length = 1.0;
}
float Rectangle::setWidth(float width)
{
    if (width >= 0 || width <= 20.0)
        width = width;
    else
        width = 1.0;
}
float Rectangle::calculatePerimeter()
{
    perimeter = (length * 2) + (width * 2) ;
    return perimeter;
}
float Rectangle::calculateArea()
{
    area = length * width;
    return area;
}
float Rectangle::getLength()
{
    cout << "Please enter length" << endl;
    cin >> length;
        return length;
}
float Rectangle::getWidth()
{
    cout << "Please enter width" << endl;
    cin >> width;
    return width;
}

void Rectangle::printInfo()
{
    cout << "the length is " << length << endl << "the width is " << width << endl;
    cout << "the perimeter is " << perimeter << endl << "the area is " << area << endl;

}
Rectangle::~Rectangle()
{
    cout << "the object has gone out of scope. ";
}

2 个答案:

答案 0 :(得分:1)

setLengthandWidthsetLengthsetWidth方法的声明和定义中,您指定它们返回float数据类型:

//In header
float setLengthAndWidth(float, float);
float setLength(float Length);
float setWidth(float Width);

//In CPP file
float Rectangle::setLengthAndWidth(float Len, float Wid)
{
    setLength(Len);
    setWidth(Wid);
}
float Rectangle::setLength(float length)
{
    if (length >= 0 || length <= 20.0)
        length = length;
    else
        length = 1.0;
}
float Rectangle::setWidth(float width)
{
    if (width >= 0 || width <= 20.0)
        width = width;
    else
        width = 1.0;
}

如果您不想收到该错误,请在方法声明和定义中将数据类型从float更改为void,如下所示:

//In header
void setLengthAndWidth(float, float);
void setLength(float Length);
void setWidth(float Width);

//In CPP file
void Rectangle::setLengthAndWidth(float Len, float Wid)
{
    setLength(Len);
    setWidth(Wid);
}
void Rectangle::setLength(float length)
{
    if (length >= 0 || length <= 20.0)
        length = length;
    else
        length = 1.0;
}
void Rectangle::setWidth(float width)
{
    if (width >= 0 || width <= 20.0)
        width = width;
    else
        width = 1.0;
}

答案 1 :(得分:0)

为什么您的getLengthgetWidth方法会修改值?通常,访问器方法只能返回适当的字段。

尽管HatsuPointerKun是正确的。

方法的返回类型为float,但无法返回任何值。 要么让它们返回一个浮点数(最有可能是你设置它们),要么将返回类型更改为void。