-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path52_Array_of_objects_using_pointers.cpp
More file actions
66 lines (44 loc) · 1.25 KB
/
52_Array_of_objects_using_pointers.cpp
File metadata and controls
66 lines (44 loc) · 1.25 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// Arrays of objects using pointers...!!
#include<iostream>
using namespace std;
class ShopItem{
int id;
float price;
public:
void setData(int a, float b){
id = a;
price = b;
}
void getData(){
cout << "Code of this item is: " << id << endl;
cout << "Price of this item is: " << price << endl;
}
};
int main()
{
int size = 3;
//#1 General Store item
//#2 Veggies item
//#3 Hardware item
ShopItem *ptr = new ShopItem[size]; // Array of objects
// Initialized "ptrTemp" just to run loop number 2
ShopItem *ptrTemp = ptr; // Points to first object
ptrTemp = ptr;
int p;
float q;
for (int i = 0; i < size; i++)
{
cout << "Enter the id and Price of item " << i+1 << ": ";
cin >> p >> q;
ptr -> setData(p, q);
ptr++;
}
// Humne ye ptrTemp banaya hai because jab pehla loop run hoga tab ptr last object ko point karega so we need to make that pointer to first object so that it prints the correct values
for (int i = 0; i < size; i++)
{
cout << "\nItem number " << i+1 << ": " <<endl;
ptrTemp -> getData();
ptrTemp++;
}
return 0;
}