Using Array within the class

ARRAYS WITHIN A CLASS

In C++, we can use arrays as data member of a class. The member functions of the class can perform any operation on this array which is the data member of the class.

Example program:
#include<iostream.h> 
class array
{
    int a[6];
    public:
    void getdata()
    {
        cout<<”Enter 6 numbers :”<<endl;
        for(int i=0;i<6;i++)
        {
            cin>>a[i];
        }
    }
    void dispaly()
    {
        cout<<”\nArray elements are”<<endl;
        for(int i=0;i<6;i++)
        {
            cout<<a[i]<<endl;
        }
    }
};
void main()
{
    array obj;
    obj.getdata();
    obj.display();

Output
Enter 6 numbers in the array:
5
6
2
1
7
4
9
Array elements are
5
6
2
1
7
4
9

Comments