-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path48arrayptr_class.cpp
More file actions
51 lines (46 loc) · 1.1 KB
/
Copy path48arrayptr_class.cpp
File metadata and controls
51 lines (46 loc) · 1.1 KB
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
44
45
46
47
48
49
50
51
#include<iostream>
using namespace std;
class retailshop
{
int id;
float price;
public:
void setData(int a, float b)
{
cout<<"Enter ID : ";
cin>>a;
id = a;
cout<<"Enter Price : ";
cin>>b;
price = b;
}
getData()
{
cout<<"Item ID [ "<<id<<" ] "<<endl
<<"Price : "<<price<<endl;
}
};
int main()
{
int size, a;
float b;
cout<<"Enter the number of items on the shelf : "<<endl;
cin>>size;
retailshop * ptr = new retailshop[size]; // dynamically allocated size or number of class pointers
for (int i = 0; i < size; i++)
{
(ptr + i)->setData(a, b);
}
cout<<endl;
for (int i = 0; i < size; i++)
{
(ptr + i)->getData();
/*
can use ptr++ as well but it will affect the next loop as after the last loop ends, the ptr will be pointing
to the last index of array, so before the first loop make a temporary pointer that is similar to the
original pointer like,
retailshop * temp_ptr = ptr;
*/
}
return 0;
}