【发布时间】:2012-08-07 21:59:12
【问题描述】:
我需要一些帮助来解决以下问题,即使用 getArea() 方法计算出数组中每个圆的面积。如何访问一个数组,然后使用 Circle::getArea() 成员函数计算出圆的面积。
Main.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);
}
Circle.h 文件
#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
};
谢谢。 非常感谢您的帮助。
【问题讨论】:
-
您没有任何
Circles 可以调用getArea()。你想要哪个圈子的面积?我完全理解您是 C++ 新手并且正在寻求帮助,但我认为这是坐下来阅读有关 C++ 工作原理的一些信息的好时机。您需要了解什么是类以及如何调用方法,以便了解这里发生了什么。照原样,这个问题没有提供足够的信息来回答它。
标签: c++ arrays visual-studio-2010 geometry member-functions