C++ for Mathematicians An Introduction for Students and Professionals phần 5

cho đến khi máy tính chạy ra khỏi bộ nhớ để vinh danh các yêu cầu. Tình trạng này được biết đến như một rò rỉ bộ nhớ. Mặt khác, nếu một cố gắng để thực hiện một xóa [] trên cùng một khối của bộ nhớ nhiều hơn một lần, các cửa của thế giới ngầm sẽ mở ra và quỷ sẽ thống trị trái đất. Hoặc chương trình của bạn có thể sụp đổ. Các chương trình sàng | The Projective Plane 185 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 int main Base B 1 2 Child C 3 4 GrandChild D 5 6 7 cout endl cout endl Illegal sum is protected cout C. print cout endl D. print cout cout endl return 0 In this program we define three classes Base Child and Grandchild each is used to derive the next. The Base class has two data members a private integer a and a protected integer b. The class also includes a protected method named sum a public constructor and a public method named print. Class Child has no additional data members. It has a public method increase_b that increases the data member b by one. Note that it would not be possible for Child to have a similar method for increasing a. The constructor for Child passes its arguments on to its parent Base but then takes no further action hence the curly braces on line 17 do not enclose any statements . The print method for Child uses Base s print method and sum method. Class GrandChild adds an extra private data element c. The constructor for GrandChild passes its first two arguments to its parent s constructor and then uses the third argument to set the value of c. The print method for GrandChild uses its grandparent s print method. Although Base print invokes the sum method the GrandChild methods cannot directly call sum because it is protected in Base hence implicitly private in Child and hence inaccessible in GrandChild. A main to illustrate all these ideas begins on line 30. The output of the program follows. ----------------------------------------------------------------------------------- 1 2 3 4 7 3 5 8 5 6 7 -- 5 7 7 _. 186 C for Mathematicians Class and file organization for PPoint and PLine Because of the duality between points and lines in the projective plane most of the data and code we use to represent these concepts in C are the

Không thể tạo bản xem trước, hãy bấm tải xuống
TỪ KHÓA LIÊN QUAN
TÀI LIỆU MỚI ĐĂNG
68    98    5    08-05-2024
172    77    4    08-05-2024
Đã phát hiện trình chặn quảng cáo AdBlock
Trang web này phụ thuộc vào doanh thu từ số lần hiển thị quảng cáo để tồn tại. Vui lòng tắt trình chặn quảng cáo của bạn hoặc tạm dừng tính năng chặn quảng cáo cho trang web này.