【发布时间】:2018-02-13 10:45:43
【问题描述】:
我已经为 getLength、getWidth 和 getArea 设计了函数,我需要在我的 main 函数中将它们分配给 int 值,但我不知道如何。
我在下面发布了我的头文件、实现和 main.cpp 文件。
请注意。
泰。
头文件
#ifndef RECTANGLE_H
#define RECTANGLE_H
class Rectangle
{
Rectangle();
public:
int getLength;
int getWidth();
int getArea(int x, int y)
};
#endif
实施文件
#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;
}
这是我开始遇到问题的地方。这些函数返回一个整数,但我不知道如何将返回的整数分配给一个 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;
}
【问题讨论】:
标签: c++