-
Notifications
You must be signed in to change notification settings - Fork 3
/
access_mod.cpp
112 lines (85 loc) · 2.18 KB
/
access_mod.cpp
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#include<iostream>
using namespace std;
/* Demo of Public Class Access Modifier:
- All the Class members and functions of Public class can be accessed by everyone.
- Other classes can access them by creating object of the class.
*/
class CircleOne
{
public:
int radius;
double compute_area()
{
return 3.14*radius*radius;
}
};
/* Demo of Private Class Access Modifier:
- The class members declared as private can be accessed only by the functions inside the class.
- Only the member functions or the friend functions are allowed.
*/
class CircleTwo
{
private:
int radius;
public:
double compute_area(int r)
{
radius = r;
return 3.14 * radius * radius;
}
};
/* Demo of Protected Class Access Modifier:
- class member declared as Protected are inaccessible outside the class but they can be accessed by any subclass(derived class) of that class
*/
/* Note:
1. private and protected are same as they can be accessible through member functions of same class.
2. protected members can be accessible through derived classes.
*/
class CircleThree
{
protected:
int radius;
public:
double compute_area(int r)
{
radius = r;
return 3.14 * radius * radius;
}
};
class test_protected : public CircleThree
{
public:
void print_radius()
{
cout << "In Child Class, Radius from parent class is :"<< radius << endl;
}
public:
void update_radius(int r)
{
radius = r;
}
};
int main()
{
// Public Access modifier demo
CircleOne obj_one;
cout << "Enter Radius of CircleOne" << endl ;
cin >> obj_one.radius;
cout << "Area of circle is: " << obj_one.compute_area() << endl;
// Private Access modifier demo
CircleTwo obj_two;
int radius = 0;
cout << "Enter Radius of CircleTwo" << endl ;
cin >> radius;
cout << "Area of circle is: " << obj_two.compute_area(radius) << endl;
// Protected Access modifier demo
CircleThree obj_three;
int radius_three = 0;
cout << "Enter Radius of CircleThree" << endl ;
cin >> radius_three;
// Protected class Members access from Inheritence
test_protected obj;
obj.update_radius(4);
obj.print_radius();
cout << "Area of circle is: " << obj_three.compute_area(radius_three) << endl;
}