[英]How to initialize an array in a constructor c++
I need help with this code.我需要有关此代码的帮助。 What I want is to make a parametric constructor and initialise/set the value of array in it.
我想要的是制作一个参数构造函数并在其中初始化/设置数组的值。
Question: Make a class with arrays of integers and initialise it in a constructor.问题:用整数的 arrays 创建一个 class 并在构造函数中初始化它。 Then find the smallest and largest numbers using functions.
然后使用函数找到最小和最大的数字。
But I am stuck at how to initialise the array in the constructor.但是我被困在如何在构造函数中初始化数组。 I want to take data input in both ways
我想以两种方式输入数据
(1) By user, using cin (1) 按用户,使用 cin
(2) By giving my own values (2) 通过给出自己的价值观
class Numbers
{
int Arr[3];
public:
Numbers() //default constructor
{
for (int i=0 ; i<=2 ; i++)
{
Arr[i]=0;
}
}
Numbers(int arr[]) //parameteric constructor
{
for (int i=0;i<=2;i++)
{
Arr[i]=arr[i];
}
}
};
int main()
{
int aro[3] = {0,10,5};
Numbers obj (aro);
return ;
}
I suggest to use std::vector<int>
or std::array<int>
.我建议使用
std::vector<int>
或std::array<int>
。 If you want initialize with custom values you can do std::vector<int> m_vec {0, 1, 2};
如果您想使用自定义值进行初始化,您可以执行
std::vector<int> m_vec {0, 1, 2};
The solution is pretty simple.解决方案非常简单。 I've made a new program from start again (for sake of understanding).
我从头开始制作了一个新程序(为了理解起见)。 According to your requirement, you wants to get input of array elements from the user dynamically and assign them to a constructor and use a method to print the highest value.
根据您的要求,您希望动态地从用户那里获取数组元素的输入并将它们分配给构造函数并使用一种方法来打印最高值。
Consider the following code:考虑以下代码:
#include <iostream>
using namespace std;
const int N = 100;
class Numbers
{
int largest = 0;
public:
Numbers(int, int[]);
void showHighest(void)
{
cout << largest << endl;
}
};
Numbers::Numbers(int size, int arr[])
{
for (int i = 0; i < size; i++)
{
if (arr[i] > largest)
{
largest = arr[i];
}
}
}
int main(void)
{
int arrays[N], total;
cout << "How many elements? (starts from zero) ";
cin >> total;
for (int i = 0; i < total; i++)
{
cout << "Element " << i << ": ";
cin >> arrays[i];
}
Numbers n(total, arrays);
n.showHighest();
return 0;
}
Output
Output
How many elements? (starts from zero) 3
Element 0: 12
Element 1: 16
Element 2: 11
16
Note: I've initialized a constant number of maximum elements, you can modify it.
注意:我已经初始化了一个恒定数量的最大元素,你可以修改它。 No vectors, etc. required to achieve so.
不需要向量等来实现这一点。 You can either use your own values by removing the
total
and its followed statements and use onlyint arrays[<num>] = {...}
instead.您可以通过删除
total
及其后面的语句来使用自己的值,而只使用int arrays[<num>] = {...}
代替。 You're done!你完成了!
Enjoy coding!享受编码!
Thank you so much for your help.非常感谢你的帮助。 I was basically confused about how to use arrays in a constructor and use setters/getters for arrays in a class.
我基本上对如何在构造函数中使用 arrays 并在 class 中为 arrays 使用 setter/getter 感到困惑。 But you all helped a lot.
但是你们都帮了很多忙。 Thanks again.
再次感谢。
Numbers(int arr[])
{
for (int i=0;i<=9;i++)
{
Arr[i]=arr[i];
}
Largest=Arr[0];
Smallest=Arr[0];
}
void Largest_Number()
{
header_top("Largest Number");
Largest=Arr[0]; //Using this so we make largest value as index zero
for (int i=0 ; i<=9 ; i++)
{
if(Arr[i]>Largest)
{
setLargest( Arr[i] );
}
}
cout<<"Largest Number: "<<getLargest()<<endl;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.