Trong hướng dẫn này, chúng ta sẽ học cách sử dụng kế thừa công khai, được bảo vệ và riêng tư trong C ++ với sự trợ giúp của các ví dụ.
Trong kế thừa C ++ , chúng ta có thể dẫn xuất một lớp con từ lớp cơ sở trong các chế độ truy cập khác nhau. Ví dụ,
class Base {
.... ... ....
};
class Derived : public Base {
.... ... ....
};
Chú ý từ khóa public
trong mã
class Derived : public Base
Điều này có nghĩa là chúng ta đã tạo một lớp dẫn xuất từ lớp cơ sở ở chế độ công khai . Ngoài ra, chúng ta cũng có thể lấy các lớp trong các chế độ được bảo vệ hoặc riêng tư .
3 từ khóa ( public
, protected
và private
) này được gọi là chỉ định truy cập trong kế thừa C ++.
kế thừa công khai, được bảo vệ và riêng tư trong C++
thừa kế công khai , được bảo vệ và riêng tư có các tính năng sau:
- public inheritance makes
public
members of the base classpublic
in the derived class, and theprotected
members of the base class remainprotected
in the derived class. - protected inheritance makes the
public
andprotected
members of the base classprotected
in the derived class. - private inheritance makes the
public
andprotected
members of the base classprivate
in the derived class.
Lưu ý: private
các thành viên của lớp cơ sở không thể truy cập được vào lớp dẫn xuất.
class Base {
public:
int x;
protected:
int y;
private:
int z;
};
class PublicDerived: public Base {
// x is public
// y is protected
// z is not accessible from PublicDerived
};
class ProtectedDerived: protected Base {
// x is protected
// y is protected
// z is not accessible from ProtectedDerived
};
class PrivateDerived: private Base {
// x is private
// y is private
// z is not accessible from PrivateDerived
}
Ví dụ 1: Kế thừa công khai C++
// C++ program to demonstrate the working of public inheritance
#include <iostream>
using namespace std;
class Base {
private:
int pvt = 1;
protected:
int prot = 2;
public:
int pub = 3;
// function to access private member
int getPVT() {
return pvt;
}
};
class PublicDerived : public Base {
public:
// function to access protected member from Base
int getProt() {
return prot;
}
};
int main() {
PublicDerived object1;
cout << "Private = " << object1.getPVT() << endl;
cout << "Protected = " << object1.getProt() << endl;
cout << "Public = " << object1.pub << endl;
return 0;
}
Đầu ra
Private = 1 Protected = 2 Public = 3
Ở đây, chúng tôi đã bắt nguồn PublicDerived
từ Base
ở chế độ công khai .
Kết quả là, trong PublicDerived
:
- prot is inherited as protected.
- pub and
getPVT()
are inherited as public. - pvt is inaccessible since it is private in
Base
.
Vì các thành viên riêng tư và được bảo vệ không thể truy cập từ main()
, chúng tôi cần tạo các chức năng công khai getPVT()
và getProt()
để truy cập chúng:
// Error: member "Base::pvt" is inaccessible
cout << "Private = " << object1.pvt;
// Error: member "Base::prot" is inaccessible
cout << "Protected = " << object1.prot;
Chú ý rằng getPVT()
hàm đã được định nghĩa bên trong Base
. Nhưng getProt()
chức năng đã được xác định bên trong PublicDerived
.
Điều này là do pvt , ở chế độ riêng tưBase
, không thể truy cập được PublicDerived
.
Tuy nhiên, prot có thể truy cập được PublicDerived
do kế thừa công khai. Vì vậy, getProt()
có thể truy cập biến được bảo vệ từ bên trong PublicDerived
.
Khả năng tiếp cận trong Thừa kế công khai
Accessibility | private members | protected members | public members |
---|---|---|---|
Base Class | Yes | Yes | Yes |
Derived Class | No | Yes | Yes |
Ví dụ 2: Kế thừa được bảo vệ trong C++
// C++ program to demonstrate the working of protected inheritance
#include <iostream>
using namespace std;
class Base {
private:
int pvt = 1;
protected:
int prot = 2;
public:
int pub = 3;
// function to access private member
int getPVT() {
return pvt;
}
};
class ProtectedDerived : protected Base {
public:
// function to access protected member from Base
int getProt() {
return prot;
}
// function to access public member from Base
int getPub() {
return pub;
}
};
int main() {
ProtectedDerived object1;
cout << "Private cannot be accessed." << endl;
cout << "Protected = " << object1.getProt() << endl;
cout << "Public = " << object1.getPub() << endl;
return 0;
}
Đầu ra
Private cannot be accessed. Protected = 2 Public = 3
Ở đây, chúng tôi đã bắt nguồn ProtectedDerived
từ Base
trong chế độ được bảo vệ .
Kết quả là, trong ProtectedDerived
:
- prot, pub and
getPVT()
are inherited as protected. pvt
is inaccessible since it is private inBase
.
Như chúng ta đã biết, các thành viên được bảo vệ không thể được truy cập trực tiếp từ bên ngoài lớp. Do đó, chúng tôi không thể sử dụng getPVT()
from ProtectedDerived
.
Đó cũng là lý do tại sao chúng ta cần tạo getPub()
hàm ProtectedDerived
để truy cập biến pub .
// Error: member "Base::getPVT()" is inaccessible
cout << "Private = " << object1.getPVT();
// Error: member "Base::pub" is inaccessible
cout << "Public = " << object1.pub;
Khả năng tiếp cận trong Tài sản thừa kế được bảo vệ
Accessibility | private members | protected members | public members |
---|---|---|---|
Base Class | Yes | Yes | Yes |
Derived Class | No | Yes | Yes (inherited as protected variables) |
Ví dụ 3: Kế thừa riêng trong C++
// C++ program to demonstrate the working of private inheritance
#include <iostream>
using namespace std;
class Base {
private:
int pvt = 1;
protected:
int prot = 2;
public:
int pub = 3;
// function to access private member
int getPVT() {
return pvt;
}
};
class PrivateDerived : private Base {
public:
// function to access protected member from Base
int getProt() {
return prot;
}
// function to access private member
int getPub() {
return pub;
}
};
int main() {
PrivateDerived object1;
cout << "Private cannot be accessed." << endl;
cout << "Protected = " << object1.getProt() << endl;
cout << "Public = " << object1.getPub() << endl;
return 0;
}
Đầu ra
Private cannot be accessed. Protected = 2 Public = 3
Ở đây, chúng tôi đã bắt nguồn PrivateDerived
từ Base
ở chế độ riêng tư .
Kết quả là, trong PrivateDerived
:
- prot,
pub
andgetPVT()
are inherited as private. - pvt is inaccessible since it is private in
Base
.
Như chúng ta đã biết, các thành viên riêng tư không thể được truy cập trực tiếp từ bên ngoài lớp. Do đó, chúng tôi không thể sử dụng getPVT()
from PrivateDerived
.
Đó cũng là lý do tại sao chúng ta cần tạo getPub()
hàm PrivateDerived
để truy cập biến pub .
// Error: member "Base::getPVT()" is inaccessible
cout << "Private = " << object1.getPVT();
// Error: member "Base::pub" is inaccessible
cout << "Public = " << object1.pub;
Khả năng tiếp cận trong Thừa kế riêng
Accessibility | private members | protected members | public members |
---|---|---|---|
Base Class | Yes | Yes | Yes |
Derived Class | No | Yes (inherited as private variables) | Yes (inherited as private variables) |