gpt4 book ai didi

C++ 圆形对象/getArea()

转载 作者:太空宇宙 更新时间:2023-11-04 16:26:09 26 4
gpt4 key购买 nike

我想写一个程序

• 定义一个包含五个圆对象的数组,其中使用参数化构造函数的随机类提供的随机数设置半径

• 定义第二个包含五个圆对象的数组,其中使用 random 类使用 setRadius 方法提供的随机数设置半径

• 使用 getArea()

显示每个数组中每个圆的面积

我需要帮助来使用 getArea() 方法显示每个数组中每个圆的面积,我需要访问每个数组中具有五个圆的半径值的数组,然后计算出面积 3.14 *半径 * 半径。然后将该区域显示到屏幕上。

另外,在做了一些研究之后,我是否需要实例化 Circle 对象这是我在 Circle myCircle; 中输入的。然而它出现了一个错误说

1>main.obj : error LNK2019: unresolved external symbol "public: __thiscall Circle::Circle(void)" (??0Circle@@QAE@XZ) referenced in function _main

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

随机.h文件

#pragma once
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

class Random
{
public:
static void initialiseSeed();
// random number initialised
// random number has been initialised to the current time.

static int random(int lower, int upper);
// this function will return a positive random number within a specific lower and
// upper boundary.
};

main.cpp 文件

#include <iostream>
#include <cstdlib>
#include <ctime>
#include <cmath>
#include <iomanip>
using namespace std;

// Include header files
#include "Circle.h"
#include "Random.h"

int main()
{
// Instantiate myCircle object
//Circle myCircle;

// Array 1
// an array of five circles objects where radius is set using the random number
// provided by the random class utilising the parameterised constructor

int CircleArrayOne [5]; // store the numbers
const int NUM = 5; // Display 5 random numbers

srand(time(NULL)); // seed the generator

// populate the array by calling the random function from the random class
// within the lower and upper bounds

for(int x = 0; x < NUM; ++x)
{
CircleArrayOne[x] = Random::random(1, 40); // assumed 0 and 40 as the bounds
}

// Below is the code I use to output the array to the screen to make sure
// that is output the correct number of values and it generate number between 1 and 40
// I am doing this to test the code above that populate the array within lower and upper
// bounds

cout << "Checking the radius in the Array 1." << endl;
for(int i = 0; i < NUM; ++i)
{
cout << CircleArrayOne[i] << endl;
}

// Array 2
// a second array of five circles objects where radius is set using the random number
// provided by the random class utilising the setRadius method

float CircleArrayTwo [5]; // store the numbers
const int Number = 5; // Display 5 random numbers

srand(time(NULL)); // seed the generator

// populate the array with random numbers

for(int i = 0; i < Number; ++i)
{
CircleArrayTwo[i] = rand()%100;
}

// Below is the code I use to output the array to the screen to make sure
// that is output the correct number of values and it generate random values

cout << "Checking the radius in the Array 2." << endl;
for(int i = 0; i < Number; ++i)
{
cout << CircleArrayTwo[i] << endl;
}

// Display the area of each Circle within each array using getArea()

cout << "\nThe area of each circle within array 1: " << endl;

cout << "\nThe area of each circle within array 2: " << endl;

// Display a message that indicates which set of circle has the largest
// combined area

cout << "\nArray: " << "had the largest combined area." << endl;

// Display a message that indicates which set contains the circle
// with the largest area

cout << "\nArray: " << "contain the circle with the largest area.\n" << endl;

system("PAUSE");
return 0;
}

// Calling Circle(float r) from Circle Class
Circle::Circle(float r)
{
if (r<=0)
{
cout << "An invalid radius has been detected." << endl;
cout << "The radius has been set to 1.0" << endl;
Radius = 1.0;
}
else
Radius = r;
}

// Calling getArea() from Circle Class
float Circle::getArea()
{
// Area = pi * radius * radius
return 3.14 * Radius * Radius;
}

// Calling setRadius(float r) from Circle Class
void Circle::setRadius(float r)
{
rand()%100;
Radius = r;
}

// Calling getRadius() from Circle Class
float Circle::getRadius()
{
return Radius;
}

// Calling random(int lower, int upper) from Random Class
int Random::random(int lower, int upper)
{
int range = upper - lower + 1;
return (rand() % range + lower);
}

// Calling initialiseSeed() from Random Class
void Random::initialiseSeed()
{
srand((unsigned int)time(0));
rand()%100;
}

如何使用 getArea() 函数获取每个数组中每个圆的面积

最佳答案

您还没有为Circle 的默认构造函数提供定义,添加

Circle::Circle() : Radius(0) {}

到 main.cpp。

关于C++ 圆形对象/getArea(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11862749/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com