-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathprotected_friend_class_function.cpp
executable file
·71 lines (57 loc) · 1.6 KB
/
protected_friend_class_function.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
#include <iostream>
#include <memory>
/*
You can only access protected members in instances of your type (or derived from
your type). You cannot access protected members of an instance of a parent or
cousin type.
*/
class base {
friend class frindClass;
friend void friendFunction(int y);
private:
int x;
void privateMethod() { std::cout << "privateMethod from base" << std::endl; }
protected:
void info() { std::cout << "info from base" << std::endl; }
public:
base(int x) : x(x){};
void print() { std::cout << "base" << std::endl; }
};
class derived : public base {
public:
derived(int x) : base(x){};
void print() { std::cout << "derived" << std::endl; }
void infoCaller() { info(); }
};
void friendFunction(int y) {
base baseObject(5);
std::cout << baseObject.x + y << std::endl;
}
class frindClass {
std::shared_ptr<base> b;
public:
frindClass(int x) { b.reset(new base(x)); }
void getinfo() {
b->info();
std::cout << b->x << std::endl;
}
};
int main(int argc, char *argv[]) {
base baseObject(5);
/*This will give you an error*/
// baseObject.info();
derived derivedObject(10);
/*This will give you an error*/
// derivedObject.info();
std::cout << "The correct way to call a protectd method from base class in "
"derived class"
<< std::endl;
derivedObject.infoCaller();
std::cout << "Accessing private member in a friend function" << std::endl;
int y = 3;
friendFunction(y);
std::cout << "Accessing private member in a friend class" << std::endl;
frindClass frindClassObject(y);
frindClassObject.getinfo();
return 0;
}