Any object whether built in or user defined, can be stored in an array. When you declare the array, you tell the compiler the type of object to store and the number of objects for which to allocate room.
The compiler know how much room is needed for each object based on the class declaration.
Consider the following class definition :
class vehicle
{
int vcode;
char vname[30];
public:
void getinfo( );
void disinfo( );
};
The identifier vehicle is a user defined data type and can be used to create objects that relate to a different categories of vehicles.
Example:
vehicle v1[3]; vehicle v2[4];
The array contains three objects namely , v1[0] , v1[1], v1[2] of type vehicle class. Similarly, v2 contains 4 objects namely v2[0], v2[1], v[2], v[3].
Accessing member data in an array of objects is a two step process. You identify the member of the array by using the index operator([]) and then you add the member operator(.) to access the particular member variable.
For Example:
v[i] disinfo( );
will display the data of the ith element of the array v1. That is, this statement request the objects v[i] to invoke the member function disonfo( ). An array of object is stored inside the memory in same way as a multi-dimensional array.
The array v1 represented in figure below
Here only the space for data items of object is created. Member function are stored separately and will be used by all the objects.
Write a program to declare a class book having data members as a title and author. Accept the data for five books and display the accepted data.
#include<iostream.h>
#include<conio.h>
class book
{
char auth[20];
char title[50];
public:
void accept( );
void display( );
};
void book::accept( )
{
cout<<"\n\n\tEnter Title of the book";
cin>>title;
cout<<"\n\n\tEnter Author Name";
cin>>auth;
}
void book::display( )
{
cout<<"\n\n\tBook Name--->"<<title;
cout<<"\n\n\tAuthor is--->"<<auth;
}
void main( )
{
book b[5];
int i;
clrscr( );
for(i=0;i<5;i++)
{
b[i].accept( );
}
for(i=0;i<5;i++)
{
b[i].display( );
}
getch( );
}
Output:-
Enter Title of the book c_basic
Enter Author Name ABC
Enter Title of the book cpp
Enter Author Name DEF
Enter Title of the book java
Enter Author Name PQR
Enter Title of the book sql
Enter Author Name XYZ
Enter Title of the book php
Enter Author Name MNO
Book Name--->c_basic
Author is--->ABC
Book Name--->cpp
Author is--->DEF
Book Name--->java
Author is--->PQR
Book Name--->sql
Author is--->XYZ
Book Name--->php
Author is--->MNO
The compiler know how much room is needed for each object based on the class declaration.
Consider the following class definition :
class vehicle
{
int vcode;
char vname[30];
public:
void getinfo( );
void disinfo( );
};
The identifier vehicle is a user defined data type and can be used to create objects that relate to a different categories of vehicles.
Example:
vehicle v1[3]; vehicle v2[4];
The array contains three objects namely , v1[0] , v1[1], v1[2] of type vehicle class. Similarly, v2 contains 4 objects namely v2[0], v2[1], v[2], v[3].
Accessing member data in an array of objects is a two step process. You identify the member of the array by using the index operator([]) and then you add the member operator(.) to access the particular member variable.
For Example:
v[i] disinfo( );
will display the data of the ith element of the array v1. That is, this statement request the objects v[i] to invoke the member function disonfo( ). An array of object is stored inside the memory in same way as a multi-dimensional array.
The array v1 represented in figure below
Here only the space for data items of object is created. Member function are stored separately and will be used by all the objects.
Write a program to declare a class book having data members as a title and author. Accept the data for five books and display the accepted data.
#include<iostream.h>
#include<conio.h>
class book
{
char auth[20];
char title[50];
public:
void accept( );
void display( );
};
void book::accept( )
{
cout<<"\n\n\tEnter Title of the book";
cin>>title;
cout<<"\n\n\tEnter Author Name";
cin>>auth;
}
void book::display( )
{
cout<<"\n\n\tBook Name--->"<<title;
cout<<"\n\n\tAuthor is--->"<<auth;
}
void main( )
{
book b[5];
int i;
clrscr( );
for(i=0;i<5;i++)
{
b[i].accept( );
}
for(i=0;i<5;i++)
{
b[i].display( );
}
getch( );
}
Output:-
Enter Title of the book c_basic
Enter Author Name ABC
Enter Title of the book cpp
Enter Author Name DEF
Enter Title of the book java
Enter Author Name PQR
Enter Title of the book sql
Enter Author Name XYZ
Enter Title of the book php
Enter Author Name MNO
Book Name--->c_basic
Author is--->ABC
Book Name--->cpp
Author is--->DEF
Book Name--->java
Author is--->PQR
Book Name--->sql
Author is--->XYZ
Book Name--->php
Author is--->MNO
0 Comments:
Post a Comment