Using Arrays within a class
Arrays can be used within a class similar to that as used
in the main() function
The syntax and the declaration of Array inside the class
is given as follows:
Class class_name
{
private:
data_type array_name[size];
public:
Member functions;
};
Let us consider an example to make clear of
the concept of using arrays inside the class
Example
#include<iostream.h>
#include<conio.h>
class stud
{
private:
int r_no;
char name[25];
public:
void getdata(int r, char *sting)
{
r_no=r;
strcpy(name, string);
}
void putdata()
{
cout<<“ Roll no of the student is”;
cout<<r_no;
cout<<“\nThe name is”;
cout<<name;
Cout<<“\n”;
}
};
void main()
{
stud s1,s2;
s1.getdata(1,raja);
s2.getdata (2,rahul);
clrscr();
s1.putdata();
s2.putdata();
}
The output of the above program is given as follows:
Output:
Roll no of the student is 1
The Name is raja
Roll no of the student is 2
The Name is rahul
Thus the above program gives a clear idea of the
concept of using arrays inside the class
You can also try several other examples also