C++使用 getArea() 并访问数组来计算圆的面积
C++ using getArea() and accessing an array to work out area of circle
我需要一些帮助来解决以下问题,即使用 getArea() 方法计算数组中每个圆的面积。我如何访问数组,然后使用 Circle::getArea() 成员函数计算出圆的面积。
主.cpp文件
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
#include "Circle.h"
#include "Random.h"
int main()
{
// Array 1, below section is to populate the array with random
// radius number within lower and upper range
int CircleArrayOne [5];
const int NUM = 5;
srand(time(NULL));
for(int x = 0; x < NUM; ++x)
{
CircleArrayOne[x] = Random::random(1, 40); // assumed 0 and 40 as the bounds
}
// output the radius of each circle
cout << "Below is the radius each of the five circles in the second array. " << endl;
// below is to output the radius in the array
for(int i = 0; i < NUM; ++i)
{
cout << CircleArrayOne[i] << endl;
}
// Here I want to access the array to work out the area using
// float Circl::getArea()
system("PAUSE");
return 0;
}
float Circle::getArea()
{
double PI = 3.14;
return (PI * (Radius*Radius));
}
float Circle::getRadius()
{
return Radius;
}
int Random::random(int lower, int upper)
{
int range = upper - lower + 1;
return (rand() % range + lower);
}
圆圈文件
#pragma once
#include <string>
class Circle
{
private:
float Radius;
public:
Circle(); // initialised radius to 0
Circle(float r); // accepts an argument and assign its value to the radius attribute
void setRadius(float r); // sets radius to a value provided by its radius parameter (r)
float getRadius(); // returns the radius of a circle
float getArea(); // calculates and returns the areas of its circle
};
谢谢。非常感谢很多帮助。
到目前为止,
您只有圆的随机半径,但没有Circle
对象。因此,首先创建圆形对象。
Circle obj[5]; // Create 5 objects default constructed.
现在使用setRadius
设置每个对象半径,并在每个对象上调用区域。
相关文章:
- 如何计算数组中元素的位数?(不是数组的长度),并计算其数字的总和
- 通过指针算法计算数组长度
- 计算数组重复次数的组合的有效算法,加起来达到给定的总和
- 当我使用需要计算数组单元格地址的模板时,奇怪的C++行为
- 计算数组的特征值/向量,而不是使用特征 3 计算矩阵
- C++ 计算数组中的单词
- 计算数组行中的总和
- 计算数组中存在其总和的对数的算法
- 如何计算数组整数的总可能组合
- 使用 sizeof 计算数组中的元素数时的结果不同
- C :如何计算数组中唯一客户端的数量并输出其支出
- 以C++为单位计算数组中的重复数字
- 通过分而治算法计算数组的最大数量
- 我需要以尺寸方式计算数组的平均值
- 计算数组中特定线的平均值并存储在另一个数组中
- 如何在 c++ 中计算数组中的元素数量?
- 计算数组中"a"字母
- 避免在函数调用中计算数组元素
- 使用sizeof()计算数组大小
- 计算数组中唯一字符的函数