-
Notifications
You must be signed in to change notification settings - Fork 7
/
inheritance.cpp
143 lines (116 loc) · 4.56 KB
/
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
#include<bits/stdc++.h>
using namespace std;
class A {
private:
int x;
protected:
void setX(int x) {
this->x = x;
}
int getX() {
return x;
}
};
class B: public A {
public:
void setData(int x) {
setX(x);
}
int getData() {
return getX();
}
};
int main() {
/*
What is inheritance?
--> It is a process of inheriting properties and behaviours of existing class(es) into new class(es)
--> A more generalized class is always the parent and the specific one is always the child
Example:
Class Fruit will be a parent class
Class Apple will be a child class
Need of inheritance?
--> Let us understand this via and example. Suppose you have a class Car. You also wish to have a class
SportsCar which has all the properties of Car + some exclusive properties for it being a SportsCar
--> What we can do for this?
--> We can have 2 seperate classes, Car (With original properties)
and SportsCar (With the new properties + properties of car). Issue in this is that we will have to write
same code twice for Car and SportsCar for the common features
--> We can have 2 seperate classes, Car (With original properties) and SportsCar (With the new properties)
and then whenever we need to add a new SportsCar we make 2 objects for it. One for new features of a Sports
Car and general features of a Car. Issue with this is that it does not comply with the idea of Encapsulation
which states that for any entity all of it's variables and functions should be part of the same class
So to solve these problems, the concept of inheritance comes into picture.
We can reuse the properties of a previously declared class and use them in our new class with some
additional functionalities
Syntax:
class BaseClassName{
};
class DerivedClassName : VisibilityMode/AccessSpecifier BaseClassName{
};
Types of Inheritance:
1. Single Inheritance
--> 1 parent class and 1 child class
Example:
Class A{
};
class B:public A{
};
2. Multilevel Inheritance
--> More than one level of single inheritance
Example:
Class A{
};
Class B: public A{
};
class C: public B{
};
3. Multiple Inheritance
--> More than one parent class
Example:
Class A{
};
Class B{
};
Class C: public A, public B{
};
4. Hierarchical Inheritance
--> One parent having more than one children
Example:
Class A{
};
Class B: public A{
};
Class C: public A{
};
5. Hybrid Inheritance
--> Mixture of more than one type of inheritance from 1 to 4
Visibility Modes
--> Private
--> Protected
--> Public
Types of users for a class
--> Users who create an object of the class (say u1)
--> Users who use derived class to access members of base class (say u2)
Difference between availability & accessibility?
--> During inheritance everything is inherited may it be Private or Protected or Public.
They are avaiable in the derived class and availalibility does not depend on the visibility mode
of inheritance.
When we say they are available, it does not mean that everyone can access it. They might not
be accessible depending on the visibility mode
Access specifier in base class | Access specifier when inherited publicly
Public | Public
Protected | Protected
Private | Inaccessible
Access specifier in base class | Access specifier when inherited protectedly
Public | Protected
Protected | Protected
Private | Inaccessible
Access specifier in base class | Access specifier when inherited privately
Public | Private
Protected | Private
Private | Inaccessible
*/
B b;
b.setData(4);
cout << b.getData() << endl;
}