Array Of Object In C++
Like array of other user-defined datatype's, an array of type class can also
be created. The array of type class contains the objects of the class as its individual elements. Thus, an array of a
class type is also known as an array of objects. An array of objects is
declared in the same way as an array of any built-in data type.
Fig: Array Of Object In C++ |
Synatx:
Class
name object name[size];
Example:
BCA
a[2];
Program:
#include<iostream.h>
#include<conio.h>
class
BCA
{
char
name[30];
int
phone no;
public:
void
inputdata();
void
outputdata();
};
void
BCA::inputdata()
{
cout<<”input
name “<<endl;
cin>>name;
cout<<”input
phone no”<<endl;
cin>>phone
no;
}
void
BCA::outputdata()
{
cout<<name<<endl;
cout<<phone
no<<endl;
}
void
main()
{
BCA
a[3];
for(int
i=0;i<=2;i++)
a[i].inputdata();
for(int
i=0;i<=2;i++)
a[i].outputdata();
getch();
}
Output:
Input
name Pramod
Input
phone 1234567889
Input
name Vansh
Input
phone 8934259812
Input
name Sparsh
Input
phone 8381833613
No comments:
Post a Comment