2012-08-07 1 views
-1

getArea() 메서드를 사용하여 배열 내의 각 원의 영역을 계산하려면 다음 문제에 대한 도움이 필요합니다. Circle :: getArea() 멤버 함수를 사용하여 배열에 액세스 한 다음 원의 영역을 계산하는 방법은 어떻게됩니까?getArea()를 사용하고 배열 영역에 액세스하여 배열 영역을 액세스하는 C++

하여 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 
}; 

감사 파일. 많은 도움을 많이 받으실 수 있습니다.

+1

'getArea()'를 호출하는'Circle '이 없습니다. 어떤 서클에서 그 지역을 원하십니까? 나는 C++을 처음 접했을 때 도움이 필요하다는 것을 충분히 이해하고 있지만, C++이 어떻게 작동하는지에 대한 정보를 읽고 앉아 있으면 도움이 될 것이라고 생각합니다. 당신은 클래스가 무엇인지에 대해, 그리고 메소드를 호출하는 방법에 관해 배워야 만합니다. 그래서 당신은 여기서 무슨 일이 일어나고 있는지 이해할 수 있습니다. 마찬가지로,이 질문은 대답하는 방법에 대한 충분한 정보를 제공하지 않습니다. – KRyan

답변

1

지금까지 서클의 임의 반경 만 표시되었지만 Circle 개의 오브젝트가 없습니다. 먼저 원 객체를 만듭니다.

Circle obj[5]; // Create 5 objects default constructed. 

setRadius을 사용하여 각 개체 반경을 설정하고 각 개체의 호출 영역을 설정하십시오.