Loading....
Coupon Accepted Successfully!

 

Arrays Of Classes

As we know, an array is a collection of similar data types. In the same way, we can also define an array of classes. In such a type of an array, every element is of class type. An array of class objects can be declared as follows.

class stud

{

public:

char name[12]; // class declaration

int rollno;

char grade[2];

};

class stud st[3]; // declaration of array of class objects


In the above example, 
st[3] is an array of three elements containing three objects of class stud. Each element of st[3] has its own set class of member variables; that is, char name[12], int rollno, and char grade[2]. A program is explained as given below.
 

12.15 Write a program to display names, roll numbers, and grades of 3 students who have appeared for the examination. Declare the class of name, roll numbers, and grade. Create an array of class objects. Read and display the contents of the array.

#include<stdio.h>

#include<conio.h>

#include<iostream.h>

void main()

{

int k=0;

class stud

{

public:
char name[12];
int rollno;

char grade[2];

};
class stud st[3];
while (k<3)
{ clrscr();

gotoxy(2,4);
cout<<“Name:”;
gotoxy(17,4);
cin>>st[k].name;
gotoxy(2,5);
cout<<“Roll No.:”;
gotoxy(17,5);
cin>>st[k].rollno;
gotoxy(2,6);
cout<<“Grade:”;
gotoxy(17,6);
cin>>st[k].grade;
st[k].grade[1]=‘\0’;
puts (“press any key..”);
getch();
k++;

}

k=0;
clrscr();
cout<<“\nName\tRollno Grade\n”;
while (k<3)
{
cout<<st[k].name<<“\t”<<st[k].rollno<<“ \t”<<st[k].grade<<“\n”;
k++;

}
}

OUTPUT

Name   Rollno  Grade

Balaji   50         A

Manoj  51         B

Sanjay 55         C


Explanation:
In the above program, class stud is declared. Its members are char name[12], int rollno, and char grade[2], and all are public. The array st[3] of class stud is declared. The first while loop and cin() statements within it are used for repetitive data reading. The second while loop and printf() statements within it display the contents of the array. In the above program, all the member variables are public. If the members are private, the above program will not work. Next, we need to declare member functions to access the data. The program given below explains the reading of private data using member functions.
 

12.16 Write a program to display names, roll numbers, and grades of 3 students who have appeared for the examination. Declare the class of name, roll numbers, and grade private. Create an array of class objects. Read and display the contents of the array using member functions.

#include<stdio.h>

#include<conio.h>

#include<iostream.h>

void put()

{

cout<<name<<“\t”<<rollno<<“\t”<<grade<<“\n”;

}

};

class stud

{

private :

char name[12];

int rollno;

char grade[2];

public :

void get()

{ clrscr();

gotoxy(2,4);

cout<<“Name : ”;

gotoxy(17,4);

cin>>name;

gotoxy(2,5);

cout<<“Roll No. : ”;

gotoxy(17,5);

cin>>rollno;

gotoxy(2,6);

cout<<“Grade :”;

gotoxy(17,6);

cin>>grade;

grade[1]=‘\0’;
puts (“press any key..”);
getch();

}
int main()
{
int m=0;
class stud st[3];
while (m<3)
{

st[m].get();
m++;
}
cout<<“\nName\tRollno Grade\n”;
m=0;
while (m<3)
{
st[m].put();
m++;
}

return 0;
}

OUTPUT

Name   Rollno  Grade

Balaji   50        A

Manoj  51        B

Sanjay 55        C


Explanation: The above program is similar to the previous one. Here, the class members are private. It is not possible to access the private members directly. Using member functions get() and put(), data are read and displayed.




Test Your Skills Now!
Take a Quiz now
Reviewer Name