Đang chuẩn bị nút TẢI XUỐNG, xin hãy chờ
Tải xuống
Điều này cung cấp đủ thông tin cho các trình biên dịch để làm công việc của mình, tuy nhiên, nó tốt hơn thực hành, sử dụng một số tên có ý nghĩa trong một mẫu thử nghiệm bởi vì nó hỗ trợ khả năng đọc, và trong một số trường hợp, làm cho tất cả các sự khác biệt giữa các mã và mã rõ ràng gây nhầm lẫn. | Chapter 9 Class Inheritance and Virtual Functions How It Works In this example you calculate the volumes of the two CCandyBox objects by invoking the Volume function that is a member of the derived class. This function accesses the inherited members m_Length m_Width and m_Height to produce the result. The members are declared as protected in the base class and remain protected in the derived class. The program produces the output shown as follows CBox constructor called CCandyBox constructor1 called CBox constructor called CCandyBox constructor2 called myCandyBox volume is 1 myToffeeBox volume is 24 CCandyBox destructor called CBox destructor called CCandyBox destructor called CBox destructor called The output shows that the volume is being calculated properly for both CCandyBox objects. The first object has the default dimensions produced by calling the default CBox constructor so the volume is 1 and the second object has the dimensions defined as initial values in its declaration. The output also shows the sequence of constructor and destructor calls and you can see how each derived class object is destroyed in two steps. Destructors for a derived class object are called in the reverse order to the constructors for the object. This is a general rule that always applies. Constructors are invoked starting with the base class constructor and then the derived class constructor whereas the destructor for the derived class is called first when an object is destroyed followed by the base class destructor. You can demonstrate that the protected members of the base class remain protected in the derived class by uncommenting the statement preceding the return statement in the function main . If you do this you get the following error message from the compiler error C2248 m_Length cannot access protected member declared in class CBox which indicates quite clearly that the member m_Length is inaccessible. The Access Level of Inherited Class Members You know that if you have