Professional Documents
Culture Documents
Array of Objects
Array of Objects
#include<iostream>
using namespace std;
class A
{
int x;
public:
void getdata(int i)
{
cout<<"\n Enter value for obj["<<i<<"].x = ";
cin>>x;
}
void showdata()
{
cout<<"\n Entered value is:"<<x<<endl;
}
};
int main()
{
A obj[5];
=====
#include<iostream>
using namespace std;
class A
{
int x;
public:
void getdata(int i)
{
cout<<"\n Enter value for obj["<<i<<"].x = ";
cin>>x;
}
void showdata()
{
cout<<"\n Entered value is:"<<x<<endl;
}
};
int main()
{
A obj[5];
A *p;
p=obj;
=====
#include<iostream>
using namespace std;
class A
{
int x;
public:
void getdata(int i, int j)
{
cout<<"\n Enter value for obj["<<i<<"]["<<j<<"].x = ";
cin>>x;
}
void showdata(int i, int j)
{
cout<<"\n Entered value of obj["<<i<<"]["<<j<<"].x = "<<x<<endl;
}
};
int main()
{
A obj[2][3];
A (*p)[3];
p=obj;
for (int r=0; r<2; r++)
{
for (int c=0; c<3; c++)
{
p[r][c].getdata(r,c);
}
}