forked from vectormars/CPP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPointer_021_object.cpp
43 lines (43 loc) · 1.13 KB
/
Pointer_021_object.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
// array of pointers to objects
#include <iostream>
using namespace std;
////////////////////////////////////////////////////////////////
class person //class of persons
{
protected:
char name[40]; //person's name
public:
void setName() //set the name
{
cout << "Enter name: ";
cin >> name;
}
void printName() //get the name
{
cout << "\n Name is: " << name;
}
};
////////////////////////////////////////////////////////////////
int main()
{
person* persPtr[100]; //array of pointers to persons
int n = 0; //number of persons in array
char choice;
do //put persons in array
{
persPtr[n] = new person; //make new object
persPtr[n]->setName(); //set person's name
n++; //count new person
cout << "Enter another (y/n)? "; //enter another
cin >> choice; //person?
}
while( choice=='y' ); //quit on ‘n'
for(int j=0; j<n; j++) //print names of
{
//all persons
cout << "\nPerson number " << j+1;
persPtr[j]->printName();
}
cout << endl;
return 0;
} //end main()