<< Chapter < Page Chapter >> Page >

Định nghĩa lại các thành viên lớp cơ sở trong một lớp dẫn xuất

Một lớp dẫn xuất có thể định nghĩa lại một hàm thành viên lớp cơ sở. Điều này được gọi là overriding. Khi hàm đó được đề cập bởi tên trong lớp dẫn xuất, phiên bản của lớp dẫn xuất được chọn một cách tự động. Toán tử định phạm vi có thể sử dụng để truy cập phiên bản của lớp cơ sở từ lớp dẫn xuất.

Các lớp cơ sở public, protected và private

Khi dẫn xuất một lớp từ một lớp cơ sở, lớp cơ sở có thể được kế thừa là public, protected và private.

class<drived_class_name>:<type_of_inheritance><base_class_name>

{………………..

};

Trong đó type_of_inheritance là public, protected hoặc private. Mặc định là private.

Khi dẫn xuất một lớp từ một lớp cơ sở public, các thành viên public của lớp cơ sở trở thành các thành viên public của lớp dẫn xuất, và các thành viên protected của lớp cơ sở trở thành các thành viên protected của lớp dẫn xuất. Các thành viên private của lớp cơ sở không bao giờ được truy cập trực tiếp từ một lớp dẫn xuất.

Khi dẫn xuất một lớp từ một lớp cơ sở protected, các thành viên public và protected của lớp cơ sở trở thành các thành viên protected của lớp dẫn xuất. Khi dẫn xuất một lớp từ một lớp cơ sở private, các thành viên public và protected của lớp cơ sở trở thành các thành viên private của lớp dẫn xuất.

Bảng sau (hình 5.6)tổng kết khả năng truy cập các thành viên lớp cơ sở trong một lớp dẫn xuất dựa trên thuộc tính xác định truy cập thành viên của các thành viên trong lớp cơ sở và kiểu kế thừa.

Kiểu kế thừa
Kế thừa public Kế thừa protected Kế thừa private
public public trong lớp dẫn xuất.Có thể truy cập trực tiếp bởi các hàm thành viên không tĩnh, các hàm friend và các hàm không thành viên. protected trong lớp dẫn xuất.Có thể truy cập trực tiếp bởi các hàm thành viên không tĩnh, các hàm friend. private trong lớp dẫn xuất.Có thể truy cập trực tiếp bởi các hàm thành viên không tĩnh, các hàm friend.
protected protected trong lớp dẫn xuất.Có thể truy cập trực tiếp bởi các hàm thành viên không tĩnh, các hàm friend. protected trong lớp dẫn xuất.Có thể truy cập trực tiếp bởi các hàm thành viên không tĩnh, các hàm friend. private trong lớp dẫn xuất.Có thể truy cập trực tiếp bởi các hàm thành viên không tĩnh, các hàm friend.
private Dấu trong lớp dẫn xuất.Có thể truy cập trực tiếp bởi các hàm thành viên không tĩnh, các hàm friend thông qua các hàm thành viên public và protected của lớp cơ sở. Dấu trong lớp dẫn xuất.Có thể truy cập trực tiếp bởi các hàm thành viên không tĩnh, các hàm friend thông qua các hàm thành viên public và protected của lớp cơ sở. Dấu trong lớp dẫn xuất.Có thể truy cập trực tiếp bởi các hàm thành viên không tĩnh, các hàm friend thông qua các hàm thành viên public và protected của lớp cơ sở.

Hình 5.7: Tổng kết khả năng truy cập thành viên lớp cơ sở trong lớp dẫn xuất.

Các contructor và destructor lớp dẫn xuất

Bởi vì một lớp dẫn xuất kết thừa các thành viên lớp cơ sở của nó (ngoại trừ constructor và destructor), khi một đối tượng của lớp dẫn xuất được khởi động, constructor lớp cơ sở phải được gọi để khởi động các thành viên lớp cơ sở của đối tượng lớp dẫn xuất. Một bộ khởi tạo lớp cơ sở (sử dụng cú pháp giống như bộ khởi tạo thành viên) có thể được cung cấp trong constructor lớp dẫn xuất để gọi tường minh constructor lớp cơ sở, mặt khác constructor lớp dẫn xuất sẽ gọi constructor mặc định lớp cơ sở.

Questions & Answers

what is phylogeny
Odigie Reply
evolutionary history and relationship of an organism or group of organisms
AI-Robot
ok
Deng
what is biology
Hajah Reply
the study of living organisms and their interactions with one another and their environments
AI-Robot
what is biology
Victoria Reply
HOW CAN MAN ORGAN FUNCTION
Alfred Reply
the diagram of the digestive system
Assiatu Reply
allimentary cannel
Ogenrwot
How does twins formed
William Reply
They formed in two ways first when one sperm and one egg are splited by mitosis or two sperm and two eggs join together
Oluwatobi
what is genetics
Josephine Reply
Genetics is the study of heredity
Misack
how does twins formed?
Misack
What is manual
Hassan Reply
discuss biological phenomenon and provide pieces of evidence to show that it was responsible for the formation of eukaryotic organelles
Joseph Reply
what is biology
Yousuf Reply
the study of living organisms and their interactions with one another and their environment.
Wine
discuss the biological phenomenon and provide pieces of evidence to show that it was responsible for the formation of eukaryotic organelles in an essay form
Joseph Reply
what is the blood cells
Shaker Reply
list any five characteristics of the blood cells
Shaker
lack electricity and its more savely than electronic microscope because its naturally by using of light
Abdullahi Reply
advantage of electronic microscope is easily and clearly while disadvantage is dangerous because its electronic. advantage of light microscope is savely and naturally by sun while disadvantage is not easily,means its not sharp and not clear
Abdullahi
cell theory state that every organisms composed of one or more cell,cell is the basic unit of life
Abdullahi
is like gone fail us
DENG
cells is the basic structure and functions of all living things
Ramadan
What is classification
ISCONT Reply
is organisms that are similar into groups called tara
Yamosa
in what situation (s) would be the use of a scanning electron microscope be ideal and why?
Kenna Reply
A scanning electron microscope (SEM) is ideal for situations requiring high-resolution imaging of surfaces. It is commonly used in materials science, biology, and geology to examine the topography and composition of samples at a nanoscale level. SEM is particularly useful for studying fine details,
Hilary
Got questions? Join the online conversation and get instant answers!
Jobilize.com Reply

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Lập trình hướng đối tượng. OpenStax CNX. Jul 29, 2009 Download for free at http://cnx.org/content/col10794/1.1
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Lập trình hướng đối tượng' conversation and receive update notifications?

Ask