简体   繁体   中英

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. 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

(2) By giving my own values

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> . If you want initialize with custom values you can do 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

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 only int arrays[<num>] = {...} instead. 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. 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;
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM