//========================
//Program in C++ to access array of pointer to object, arrofptr2obj.cpp
//========================
//Array of pointer to object of class person
//pros: Dont need to know the number of objects to be created
//cons: Need to give a fixed size of array// solution is linked list
#include<iostream>
using namespace std;
class person
{
protected:
char name[50];
public:
void setname()
{
cout<<"\n Enter Name of the :";
cin >> name;
}
void showname()
{
cout<<"\n Name of the person is: ";
cout<<name;
}
};
int main()
{
person* ptrpers[100];//array of pointer to object of class person
int n=0;
char option;
do{
ptrpers[n]= new person;//creating 1 object run time
ptrpers[n]->setname();
n++;
cout<<"Enter 1 more person's name (y/n)"<<"\n";
cin>>option;
}while(option=='y');
//displaying the names
for(int j=0;j<n;j++)
{
cout<<"\n Person Number:"<<j+1;
ptrpers[j]->showname();
}
cout<<endl;
return 0;
}
//Program in C++ to access array of pointer to object, arrofptr2obj.cpp
//========================
//Array of pointer to object of class person
//pros: Dont need to know the number of objects to be created
//cons: Need to give a fixed size of array// solution is linked list
#include<iostream>
using namespace std;
class person
{
protected:
char name[50];
public:
void setname()
{
cout<<"\n Enter Name of the :";
cin >> name;
}
void showname()
{
cout<<"\n Name of the person is: ";
cout<<name;
}
};
int main()
{
person* ptrpers[100];//array of pointer to object of class person
int n=0;
char option;
do{
ptrpers[n]= new person;//creating 1 object run time
ptrpers[n]->setname();
n++;
cout<<"Enter 1 more person's name (y/n)"<<"\n";
cin>>option;
}while(option=='y');
//displaying the names
for(int j=0;j<n;j++)
{
cout<<"\n Person Number:"<<j+1;
ptrpers[j]->showname();
}
cout<<endl;
return 0;
}
No comments:
Post a Comment