일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
- 최소제곱법
- 선형모델
- virtual function
- Effective C++
- Machine learning
- 비선형 함수
- member function
- 딥러닝
- call by reference
- inheritance
- struct
- Linear Regression
- Class
- regression
- 지도학습
- Overloading
- 회귀학습
- 회귀
- kubernetes
- Nonlinear Function
- call by value
- 맵리듀스
- local minimum
- Nonlinear Model
- overriding
- 머신러닝
- MapReduce
- least square
- 비선형모델
- c++
- Today
- Total
J's Study log
[C++] Inheritance 본문
Inheritance
The base idea of inheritance is for making a conflicated classes relation more simple.
No matter you add or delete a class.
It'll be much easier by using inheritance.
How to inherit
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
|
class A
{
private:
int a;
char b[100];
public:
A(int num, char* word) : a(num)
{
strcpy(b, word);
}
void AAA() const
{
cout<<a<<endl;
}
void BBB() const
{
cout<<b<<endl;
}
};
class B : public A // public inhertance of class A
{
private:
char c[100];
public:
B(int num, char* word1, char* word2) : A(num, word1)
{
strcpy(c, word2);
}
void Important const
{
AAA();
BBB();
cout<<c<<endl;
}
};
|
Let's see line 21.
You can see how to declare subclass of class A.
As you can see the constructor of class B resets class A's member variables too.
The constructor of derived class have to reset the member of base class.
(Also! The constructor of base class must be called explicitly to be reset,
otherwise the void constructor of the base class will be called.)
It's gets the value by parameter, and put it with A constructor's initializer.
And of course private members of base class is not able to reset directly.
(Because class B is declared by public inheritance)
All this process follows the very basic principles
"Every member of class must be reset by corresponding class's constructor"
Plus, destruction of each class's dynamic allocation should be proceeded in their own class's destructor.
Just for a reference
We call inheritance many ways.
class A | class B |
upper class | inferior class |
base class | derived class |
super class | subclass |
parent class | child class |
Three types of Inheritance
- Private Inheritance
If the member's access range is wider than 'private' it'll be changed into 'private'.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
class Base
{
private:
int num1;
protected:
int num2;
public:
int num3;
};
class Derived : private Base
{
//empty
};
|
If you look into 'class Derived' it is same as this.
1
2
3
4
5
6
7
8
9
|
class Derived : private Base
{
cannot access:
int num1;
private:
int num2;
private:
int num3;
};
|
In line3 there's no such keyword like cannot access, but I just wanted to show
that the private member of Base class does exist and it's impossible to access in this kind of subclass.
And if you inherit class Derived again, the result will be like this.
1
2
3
4
5
6
7
8
9
|
class ReDerived : public Derived
{
cannot access:
int num1;
cannot access:
int num2;
cannot access:
int num3;
};
|
- Protected Inheritance
If the member's access range is wider than 'protected' it'll be changed into 'protected'.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
class Base
{
private:
int num1;
protected:
int num2;
public:
int num3;
};
class Derived : protected Base
{
//empty
};
|
If you look into 'class Derived' it is same as this..
1
2
3
4
5
6
7
8
9
|
class Derived : protected Base
{
cannot access:
int num1;
protected:
int num2;
protected:
int num3;
};
|
- Public Inheritance
Inherit the class just the way it is declared exept 'private' members.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
class Base
{
private:
int num1;
protected:
int num2;
public:
int num3;
};
class Derived : public Base
{
//empty
};
|
If you look into 'class Derived' it is same as this...
1
2
3
4
5
6
7
8
9
|
class Derived : public Base
{
cannot access:
int num1;
protected:
int num2;
public:
int num3;
};
|
Remember that there's just 'public inheritance' in C++.
Because exept public, others are used in very special case.
Conditions for Inheritance
- Generization
The base condition of inheritance is 'IS-A' relation.
Following the base idea of inheritance, derived class basically have everything what base class have.
Plus, the derived has it's own specificity.
In real world, for example like a cell phone and a i-phone, an apple and a green apple, etc.
With these examples, you can say 'i-phone IS A cell phone' and 'Green apple IS AN apple'.
Following this rule, 'a derived class IS A base class' is mostly true.
Almost every inhertance is following 'IS A' relation rather than others.
It's because using other relations in inheritance becomes a restriction when you need to fix the code.
[참고자료]
- 윤성우의 열혈 C++ 프로그래밍
'Computer Language > C++' 카테고리의 다른 글
[C++] call by value & call by reference (0) | 2020.03.21 |
---|---|
Effective C++ (0) | 2020.03.10 |
[C++] Class / Struct (0) | 2020.03.08 |
[C++] new & delete (0) | 2020.02.22 |
[C++] Different length of String - not (0) | 2020.02.20 |