<< Chapter < Page Chapter >> Page >

Có bốn cách để trộn và đối sánh các con trỏ lớp cơ sở và các con trỏ lớp dẫn xuất với các đối tượng lớp cơ sở và các đối tượng lớp dẫn xuất:

 Tham chiếu tới một đối tượng lớp cơ sở với một con trỏ lớp cơ sở thì không phức tạp.

 Tham chiếu tới một đối tượng lớp dẫn xuất với một con trỏ lớp dẫn xuất thì không phức tạp.

 Tham chiếu tới đối tượng lớp dẫn xuất với một con trỏ lớp cơ sở thì an toàn bởi vì đối tượng lớp dẫn xuất cũng là một đối tượng lớp cơ sở của nó. Như vậy đoạn mã chỉ có thể tham chiếu tới các thành viên lớp cơ sở. Nếu đoạn mã tham chiếu tới các thành viên lớp dẫn xuất thông qua con trỏ lớp cơ sở, trình biên dịch sẽ báo một lỗi về cú pháp.

 Tham chiếu tới một đối tượng lớp cơ sở với một con trỏ lớp dẫn xuất thì có lỗi cú pháp. Đầu tiên con trỏ lớp dẫn xuất phải được ép sang con trỏ lớp cơ sở.

Đa kế thừa (multiple inheritance)

Một lớp có thể được dẫn xuất từ nhiều lớp cơ sở, sự dẫn xuất như vậy được gọi là đa kế thừa. Đa kế thừa có nghĩa là một lớp dẫn xuất kế thừa các thành viên của các lớp cơ sở khác nhau. Khả năng mạnh này khuyến khích các dạng quan trọng của việc sử dụng lại phần mềm, nhưng có thể sinh ra các vấn đề nhập nhằng.

Ví dụ 5.7: Lớp Circle và project có tên là CT5_8.PRJ (gồm các file DIRIVED.CPP, CT5_8.CPP).

File BASE1.H

1: //BASE1.H

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

3: #ifndef BASE1_H

4: #define BASE1_H

5:

6: class Base1

7: {

8: protected:

9: int Value;

10: public:

11: Base1(int X)

12: {

13: Value = X;

14: }

15: int GetData() const

16: {

17: return Value;

18: }

19: };

20:

21: #endif

File BASE2.H

1: //BASE2.H

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

3: #ifndef BASE2_H

4: #define BASE2_H

5:

6: class Base2

7: {

8: protected:

9: char Letter;

10: public:

11: Base2(char C)

12: {

13: Letter = C;

14: }

15: char GetData() const

16: {

17: return Letter;

18: }

19: };

20:

21: #endif

File DERIVED.H

1: //DERIVED.H

2: //Định nghĩa lớp Derived mà kế thừa từ nhiều lớp cơ sở (Base1&Base2)

3: #ifndef DERIVED_H

4: #define DERIVED_H

5:

6: #include "base1.h"

7: #include "base2.h"

8:

9: class Derived : public Base1, public Base2

10: {

11: private:

12: float Real;

13: public:

14: Derived(int, char, float);

15: float GetReal() const;

16: friend ostream&operator<<(ostream&Output, const Derived&D);

17: };

18:

19: #endif

File DERIVED.CPP

1: //DERIVED.H

2: //Định nghĩa lớp Derived kế thừa từ nhiều lớp cơ sở (Base1&Base2)

3: #ifndef DERIVED_H

4: #define DERIVED_H

5:

6: #include "base1.h"

7: #include "base2.h"

8:

9: class Derived : public Base1, public Base2

10: {

11: private:

12: float Real;

13: public:

14: Derived(int, char, float);

15: float GetReal() const;

16: friend ostream&operator<<(ostream&Output, const Derived&D);

17: };

18:

19: #endif

File CT5_8.CPP

1: //CT5_8.CPP

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

3: #include<iostream.h>

4: #include "base1.h"

5: #include "base2.h"

6: #include "derived.h"

7:

8: int main()

9: {

10: Base1 B1(10), *Base1Ptr;

11: Base2 B2('Z'), *Base2Ptr;

12: Derived D(7, 'A', 3.5);

13: cout<<"Object B1 contains integer "

14:     <<B1.GetData()<<endl

15:     <<"Object B2 contains character "

16:     <<B2.GetData()<<endl

17:     <<"Object D contains:"<<endl<<D<<endl<<endl;

Questions & Answers

how do you get the 2/50
Abba Reply
number of sport play by 50 student construct discrete data
Aminu Reply
width of the frangebany leaves on how to write a introduction
Theresa Reply
Solve the mean of variance
Veronica Reply
Step 1: Find the mean. To find the mean, add up all the scores, then divide them by the number of scores. ... Step 2: Find each score's deviation from the mean. ... Step 3: Square each deviation from the mean. ... Step 4: Find the sum of squares. ... Step 5: Divide the sum of squares by n – 1 or N.
kenneth
what is error
Yakuba Reply
Is mistake done to something
Vutshila
Hy
anas
hy
What is the life teble
anas
hy
Jibrin
statistics is the analyzing of data
Tajudeen Reply
what is statics?
Zelalem Reply
how do you calculate mean
Gloria Reply
diveving the sum if all values
Shaynaynay
let A1,A2 and A3 events be independent,show that (A1)^c, (A2)^c and (A3)^c are independent?
Fisaye Reply
what is statistics
Akhisani Reply
data collected all over the world
Shaynaynay
construct a less than and more than table
Imad Reply
The sample of 16 students is taken. The average age in the sample was 22 years with astandard deviation of 6 years. Construct a 95% confidence interval for the age of the population.
Aschalew Reply
Bhartdarshan' is an internet-based travel agency wherein customer can see videos of the cities they plant to visit. The number of hits daily is a normally distributed random variable with a mean of 10,000 and a standard deviation of 2,400 a. what is the probability of getting more than 12,000 hits? b. what is the probability of getting fewer than 9,000 hits?
Akshay Reply
Bhartdarshan'is an internet-based travel agency wherein customer can see videos of the cities they plan to visit. The number of hits daily is a normally distributed random variable with a mean of 10,000 and a standard deviation of 2,400. a. What is the probability of getting more than 12,000 hits
Akshay
1
Bright
Sorry i want to learn more about this question
Bright
Someone help
Bright
a= 0.20233 b=0.3384
Sufiyan
a
Shaynaynay
How do I interpret level of significance?
Mohd Reply
It depends on your business problem or in Machine Learning you could use ROC- AUC cruve to decide the threshold value
Shivam
how skewness and kurtosis are used in statistics
Owen Reply
yes what is it
Taneeya
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