Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 3

// Array of objects (1D)

#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];

for (int i=0; i<5; i++)


{
obj[i].getdata(i);
}

for (int i=0; i<5; i++)


{
obj[i].showdata();
}
}

=====

// pointer to Array of objects (1D)

#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;

for (int i=0; i<5; i++)


{
    p[i].getdata(i);
}

for (int i=0; i<5; i++)


{
    (*(p+i)).showdata();
}
}

=====

// pointer to Array of objects (2D)

#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);
    }
}

for (int r=0; r<2; r++)


    {
    for (int c=0; c<3; c++)
    {
        ( *(*(p+r)+c) ).showdata(r,c);
    }
    }
}

You might also like