<< Chapter < Page Chapter >> Page >

Các constructor lớp cơ sở và các toán tử gán lớp cơ sở không được kế thừa bởi lớp dẫn xuất.Tuy nhiên, các constructor và các toán tử gán lớp dẫn xuất có thể gọi các constructor và các toán tử gán lớp cơ sở.

Một constructor lớp dẫn xuất luôn gọi constructor lớp cơ sở của nó đầu tiên để khởi tạo các thành viên lớp cơ sở của lớp dẫn xuất. Nếu constructor lớp dẫn bị bỏ qua, constructor mặc định lớp dẫn gọi constructor lớp cơ sở. Các destructor được gọi theo thứ tự ngược lại thứ tự gọi các constructor, vì thế destructor lớp dẫn xuất được gọi trước destructor lớp cơ sở của nó.

Ví dụ 5.4: Minh họa thứ tự các contructor và destructor lớp cơ sở và lớp dẫn xuất được gọi và project có tên là CT5_4.PRJ

File POINT.H

1: //POINT.H

2: //Định nghĩa lớp Point

3: #ifndef POINT_H

4: #define POINT_H

5:

6: class Point

7: {

8: public:

9: Point(float A= 0.0, float B= 0.0);

10: ~Point();

11: protected:

12: float X, Y;

13: };

14:

15: #endif

File POINT.CPP

1: //POINT.CPP

2: //Định nghĩa các hàm thành viên lớp Point

3: #include<iostream.h>

4: #include "point.h"

5:

6: Point::Point(float A, float B)

7: {

8: X = A;

9: Y = B;

10: cout<<"Point constructor: "

11:           <<'['<<X<<", "<<Y<<']'<<endl;

12: }

13:

14: Point::~Point()

15: {

16: cout<<"Point destructor: "

17:           <<'['<<X<<", "<<Y<<']'<<endl;

18: }

File CIRCLE.H

1: //CIRCLE.H

2: //Định nghĩa lớp Circle

3: #ifndef CIRCLE_H

4: #define CIRCLE_H

5:

6: #include "point.h"

7: #include<iomanip.h>

8:

9: class Circle : public Point

10: {

11: public:

12: Circle(float R = 0.0, float A = 0, float B = 0);

13: ~Circle();

14: private:

15: float Radius;

16: };

17:

18: #endif

File CIRCLE.CPP

1: //CIRCLE.CPP

2: //Định nghĩa các hàm thành viên lớp Circle

3: #include "circle.h"

4:

5: Circle::Circle(float R, float A, float B): Point(A, B)

6: {

7: Radius = R;

8: cout<<"Circle constructor: Radius is "

9:           <<Radius<<" ["<<A<<", "<<B<<']'<<endl;

10: }

11:

12: Circle::~Circle()

13: {

14: cout<<"Circle destructor: Radius is "

15:           <<Radius<<" ["<<X<<", "<<Y<<']'<<endl;

16: }

File CT5_4.CPP

1: //CT5_4.CPP

2: //Chương trình 5.4

3: #include<iostream.h>

4: #include "point.h"

5: #include "circle.h"

6: int main()

7: {

8: {

9: Point P(1.1, 2.2);

10: }

11: cout<<endl;

12: Circle C1(4.5, 7.2, 2.9);

13: cout<<endl;

14: Circle C2(10, 5, 5);

15: cout<<endl;

16: return 0;

17: }

Chúng ta chạy ví dụ 5.4 , kết quả ở hình 5.8

Hình 5.8: Kết quả của ví dụ 5.4

Chuyển đổi ngầm định đối tượng lớp dẫn xuất sang đối tượng lớp cơ sở

Mặc dù một đối tượng lớp dẫn xuất cũng là một đối tượng lớp cơ sở, kiểu lớp dẫn xuất và kiểu lớp cơ sở thì khác nhau. Các đối tượng lớp dẫn xuất có thể được xử lý như các đối tượng lớp cơ sở. Điều này có ý nghĩa bởi vì lớp dẫn xuất có các thành viên tương ứng với mỗi thành viên của lớp cơ sở. Phép gán theo chiều hướng ngược lại là không cho phép bởi vì gán một đối tượng lớp cơ sở cho đối tượng lớp dẫn xuất sẽ cho phép thêm các thành viên lớp dẫn xuất không xác định.

Một con trỏ trỏ tới một đối tượng lớp dẫn xuất có thể được chuyển đổi ngầm định thành một con trỏ trỏ tới một đối tượng lớp cơ sở bởi vì một đối tượng lớp dẫn xuất là một đối tượng lớp cơ sở.

Questions & Answers

Biology is a branch of Natural science which deals/About living Organism.
Ahmedin Reply
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
cell is the smallest unit of the humanity biologically
Abraham
what is biology
Victoria Reply
what is biology
Abraham
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
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