-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path11_Inheritance.cpp
148 lines (129 loc) · 4.49 KB
/
11_Inheritance.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/*
Topic 11:
https://cplusplus.com/doc/tutorial/inheritance/
*/
/*
Friends are functions or classes declared with the 'friend' keyword.
A friend can access class members through that class objects.
1) Friend function:
A non-member function (declared a friend of a class) can access the private and protected
members of that class. That is done by including a declaration of this external function
within the class, and preceding it with the keyword 'friend'. (see examples)
2) Friend class:
A friend class is a class whose members have access to the private or protected members of another class.
3) Inheritance between classes:
Classes in C++ can be extended, creating new classes which retain characteristics of the base class.
This process, known as inheritance, involves a base class and a derived class:
The derived class inherits the members of the base class, on top of which it can add its own members.
-) Derived classes definitions use the following syntax:
class derived_class_name: public base_class_name
{ ... definition ... };
-) Multiple inheritance: A class may inherit from more than one class with syntax:
class derived_class_name: public base_class1_name, public base_class2_name, public base3_class_name
{ ... definition ... };
Note: The 'public' access specifier may be replaced by any one of the other access specifiers
(protected or private). This access specifier limits the most accessible level for the members
inherited from the base class such that:
-) The members with a more accessible level are inherited with this level instead.
-) The members with an equal or more restrictive access level keep their level in the derived class.
_____________________________________________________________________
Access | public | protected | private |
=====================================================================
members of the same class | yes | yes | yes |
members of derived class | yes | yes | no |
not members (objects) | yes | no | no |
---------------------------------------------------------------------
A publicly derived class inherits access to every member of a base class except:
- its constructors and its destructor
- its assignment operator members (operator=)
- its friends
- its private members
Note: Access to the constructors and destructor of the base class is not inherited, but their
defaults are automatically called by the constructors and destructor of the derived class.
For base class constructors with parameters, they must be called explicitly after the derived
class constructors with syntax:
derived_constructor_name (parameters) : base_constructor_name (parameters) {...}
*/
#include <iostream>
using namespace std;
class Square;
class Rectangle {
int width{}, height{};
public:
Rectangle() {}
Rectangle(int x, int y) : width(x), height(y) {}
int area() { return width * height; }
friend Rectangle duplicate(const Rectangle&); // friend function
void convert(Square a);
};
Rectangle duplicate(const Rectangle& param)
{
Rectangle duplic;
duplic.width = param.width * 2;
duplic.height = param.height * 2;
return duplic;
}
class Square {
friend class Rectangle; // friend class
private:
int side;
public:
Square(int a) : side(a) {}
};
void Rectangle::convert(Square a) {
width = a.side;
height = a.side;
}
// Inheritance
class Mother {
public:
Mother()
{
cout << "Mother: no parameters\n";
}
Mother(int a)
{
cout << "Mother: int parameter\n";
}
};
class Daughter : public Mother {
public:
Daughter()
{
cout << "Daughter: no parameter\n\n";
}
Daughter(int a)
{
cout << "Daughter: int parameter\n\n";
}
};
class Son : public Mother {
public:
Son()
{
cout << "Son: no parameter\n\n";
}
Son(int a) : Mother()
{
cout << "Son: int parameter\n\n";
}
};
int main() {
// friend function
Rectangle foo;
Rectangle bar(2, 3);
foo = duplicate(bar);
cout << foo.area() << '\n';
cout << bar.area() << '\n';
// friend class
Rectangle rect;
Square sqr(4);
rect.convert(sqr);
cout << rect.area() << "\n\n";
// constructors and derived classes
Daughter Aliya(10);
Son Ibrahim(10);
Daughter Aysha;
Son Ali;
return 0;
}