-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path03_oops.cpp
More file actions
51 lines (40 loc) · 1.2 KB
/
03_oops.cpp
File metadata and controls
51 lines (40 loc) · 1.2 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 shop{
int itemId[100];
int itemPrice[100]; //private
int counter;
public:
void initCounter(void) { counter = 0;}
void setPrice(void);
void displayPrice(void);
};
void shop:: setPrice(void) {
cout<<"Enter id of your item no "<<counter + 1 <<endl;
cin>>itemId[counter];
cout<<"Enter price of your item"<<endl;
cin>>itemPrice[counter];
counter ++;
}
void shop::displayPrice(void){
for(int i=0; i< counter;i++)
cout<<"the price of item with Id"<<itemId[i]<<"is"<<itemPrice[i]<<endl;
}
int main() {
shop dukan; //make object
dukan.initCounter();
dukan.setPrice();
dukan.setPrice(); //3 bar id or price k set krne bolta h qki tha 3 times dukan.setprice likhe h
dukan.setPrice();
dukan.displayPrice();
return 0;
}
/*
* Why are we using counter inside square brackets like itemId[counter]?
Because:
counter indicates which position in the array we should store the new item.
Every time you add a new item, you store it at the next index.
* Why not fixed index like itemId[0]?
Because then every time you enter a new item, old value gets replaced.
Using counter automatically moves to next empty place in array.
*/