-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpointer_object.cpp
More file actions
76 lines (39 loc) · 1.45 KB
/
pointer_object.cpp
File metadata and controls
76 lines (39 loc) · 1.45 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
67
68
69
70
71
72
73
74
75
76
// pointer and ojects
#include<iostream>
using namespace std;
class shop{
public:
int product_v;
int products;
string name;
shop(int product_v,int products,string name){
this->product_v = product_v;
this->products = products; // use this in order to seperate the value of local var and class member .
this->name = name;
}
void print(){
cout<<product_v<<" "<<products<<" "<<name<<endl;
}
};
int main(){
shop s1(32,2,"baby garments");
shop*s2 = new shop(90,4,"bags"); // dynamically store the obj values in pointer
shop *p = &s1; // make a object pointer and store the adress of object (s1)
// cout<<(*p).name;
/*
now by using pointer of obj we can
change everything inside such calss
and structure of obj pinter is like
(*p).class memeber where p is var
*/
//-----------------USING (->) IS A POITNER----------------------------->
p->name = "lader coat";
p->products = 90; //by using -> we access the member and change whole value
p->product_v = 2;
cout<<endl;
//use the p only and only if it is pointer of object.othwise its not correct.
s1.print(); //access the s2 obj is a simple obj and print values. (orignal s1 obj)
s1.print(); //access the s2 obj is a simple obj and print values. (changed using pointer)
s2->print(); //access the s2 obj is a pointer obj and print values.
return 0;
}