-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathptrtoclss.cpp
More file actions
69 lines (57 loc) · 1.91 KB
/
Copy pathptrtoclss.cpp
File metadata and controls
69 lines (57 loc) · 1.91 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
#include <iostream>
using namespace std;
class Rectangle {
private:
int length;
int breadth;
public:
Rectangle(int l, int b) {
length=l;
breadth=b;
}
int getArea() {
return 2*length*breadth;
}
};
int main() {
//// Pointer to Class
// First approach: Create an object first, then assign to a pointer
// creating an object of Rectangle
Rectangle var1(5,2); // parameterized constrcutor
/* creating a pointer of Rectangle type &
assigning address of var1 to this pointer */
Rectangle* ptr = &var1;
// Second approach: directly use the new operator to assign a class/datatype to a pointer
Rectangle* ptr = new Rectangle(5,2);//&var1;
/* calculating area of rectangle by using pointer
ptr to call the objects getArea() function
*/
int area = ptr->getArea();
cout<<"Area of rectangle is: "<< area << endl;
// Second approach: free the allocated memory when using the new operator
delete ptr;
// Pointer to an array
int *ptr;
int arr[5]={0,1,2,3,4};
ptr = arr; //&arr[0]; // both ways are ok, both point to the first element
for(int i=0;i<5;i++){
// ptr+i is equivalent to &arr[i], display the address stored in ptr+i
cout << "ptr + " << i << " = "<< ptr + i << endl;
// *(ptr+i) is equivalent to arr[i], display the value at the address stored in ptr+i
cout << "*(ptr + " << i << ") : " << *(ptr + i) << endl;
}
// Pointer to Class Array
// creating an object array of Rectangle, setting values of array elements
Rectangle var[2] = {Rectangle(5,2), Rectangle(3,2)};
/* creating a pointer of Rectangle type &
assigning address of var to this pointer */
Rectangle* ptr;
ptr = var;
/* calculating area of rectangles by using pointer
ptr to call the objects getArea() function
*/
for(int i=0;i<2;i++){
cout<<"Area of Rectangle"<<(i+1)<<" : "<<(ptr+i)->getArea()<<endl;
}
return 0;
}