简体   繁体   English

如何将类中的 Int 函数设置为等于 Main 中的 Int

[英]How to Set Int Function from Class equal to Int in Main

I have designed functions for getLength, getWidth, and getArea and I need to assign them to int values in my main function but I do not know how.我为 getLength、getWidth 和 getArea 设计了函数,我需要在主函数中将它们分配给 int 值,但我不知道如何分配。

I have posted my header, implementation, and main.cpp files below.我已经在下面发布了我的标题、实现和 main.cpp 文件。

Please adivse.请指教。

Ty.泰。

Header File头文件

#ifndef RECTANGLE_H
#define RECTANGLE_H

class Rectangle
{
    Rectangle();
public:
    int getLength;
    int getWidth();
    int getArea(int x, int y)
};
#endif

Implementation File实施文件

#include "Rectangle.h"
#include <iostream>
using namespace std;

int Rectangle::getLength()
{
    int x = 0;
    cout << "Enter Length: ";
    cin >> x;

    return x;
}

int Rectangle::getWidth()
{
    int x = 0;
    cout << "Enter Width: ";
    cin >> x;

    return x;

}

int Rectangle::getArea(int x, int y)
{
    int area = x * y;
    return area;
}

This is where I begin running into issues.这是我开始遇到问题的地方。 The functions return an integer, but I do not know how to be able to assign the return integer to an int value.这些函数返回一个整数,但我不知道如何将返回整数分配给一个 int 值。

#include "Rectangle.h"
#include "Rectangle.cpp"
#include <iostream>
#include <vector>
using namespace std;

int main() {
    int area, length, width = 0;

    vector <int> myVector;
    cout << "Lets Make Some Rectangles: " << endl;

    for (int i = 0; i < 5; i++) {
        length = Rectangle::getLength();
        width = Rectangle::getWidth();

        area = Rectangle::getArea(length,width);

        myVector.push_back(area);
    }

    int largest = 0;

    for (int i = 0; i < 5; i++) {
        if (myVector[i] >= myVector[i + 1]) {
            largest = myVector[i];
        }
    }
    cout << "The Largest Rectangle Has an Area of: " << largest;
    system("pause");
    return 0;
}

One can call the member function in this way Rectangle::getLength();可以通过这种方式调用member function Rectangle::getLength(); only when getLength();仅当getLength(); is declared static inside class Rectangle .在类Rectangle声明为静态。

If member functions are not declared static then they can be accessed via object of the class.如果member functions未声明为静态,则可以通过类的对象访问它们。

Rectangle obj;
length = obj.getLength();

Also, you are crossing the valid bounds of vector myVector .此外,您正在越过向量myVector的有效边界。 You have inserted 5 elements in vector (from index 0 to 4) and trying to access 6th element myVector[5] via myVector[i] >= myVector[i + 1] when i = 4 .您已在向量中插入 5 个元素(从索引 0 到 4),并在i = 4时尝试通过myVector[i] >= myVector[i + 1]访问第 6 个元素myVector[5]

Correct way to find largest:找到最大的正确方法:

int largest = myVector[0];

for (int i = 1; i < myVector.size(); i++) {
    if (largest > myVector[i]) {
        largest = myVector[i];
    }
}

As far as my knowledge with C++ goes, you need to create member variables which will hold the data(width, length), if you need create constructor which will take parameters for those memeber variables:就我对 C++ 的了解,如果您需要创建构造函数来获取这些成员变量的参数,则需要创建成员变量来保存数据(宽度、长度):

Rectangle(int width, int length){
    m_width = width;
    m_length = length;
}

notice member variables m_width and m_length注意成员变量m_widthm_length

and create object, which will hold the data using constructor.并创建对象,它将使用构造函数保存数据。 ie IE

Rectangle myRectangle = Rectangle(5,5)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM