TAILIEUCHUNG - A Complete Guide to Programming in C++ part 56

A Complete Guide to Programming in C++ part 56. This book provides both novice and experienced programmers with a comprehensive resource manual for the C++ programming language. Readers gain experience in all aspects of programming, from elementary language concepts to professional software development, with in depth coverage of all the language elements en route. These elements are carefully ordered to help the reader create useful programs every step of the way. | chapter Type Conversion in Class Hierarchies This chapter describes implicit type conversion within class hierarchies which occurs in the context of assignments and function calls. In addition explicit type casting in class hierarchies is discussed in par ticular upcasting and downcasting. 529 530 CHAPTER 24 TYPE CONVERSION IN CLASS HIERARCHIES CONVERTING TO BASE CLASSES Example for implicit conversion include bool compare Car Car int main PassCar beetle New Beetle false 3421 VW miata Miata true 2512 Mazda bool res compare beetle miata . X X ok Implicit conversion to base class. Car a beetle Car b miata bool compare Car a Car b Here a is the base part of beetle b is the base part of miata. If this is inconvenient an explicit type cast to type PassCar has to be performed. CONVERTING TO BASE CLASSES 531 Implicit Conversion If a class is derived from another class by public inheritance the derived class assumes the characteristics and features of the base class. Objects of the derived class type then become special objects of the base class just like an automobile is a special type of vehicle. You can utilize the is relationship when handling objects. It is possible to assign an object of a derived class to an object of the base class. This causes an implicit type conversion to a base class type. The base class thus becomes a generic term for multiple special cases. Given that the classes PassCar and Truck were derived from the Car class objects of the PassCar or Truck type can always be managed like objects of Car type. Assignments Implicit type conversion in class hierarchies occurs in assignments to base class objects pointers or references to the base class. Function Calls Additionally a similar kind of implicit type conversion takes place for the arguments of function calls. Given the function compare with the following prototype Example bool compare Car Car and two objects of the derived PassCar class type beetle and miata the following statement is valid

TỪ KHÓA LIÊN QUAN
TAILIEUCHUNG - Chia sẻ tài liệu không giới hạn
Địa chỉ : 444 Hoang Hoa Tham, Hanoi, Viet Nam
Website : tailieuchung.com
Email : tailieuchung20@gmail.com
Tailieuchung.com là thư viện tài liệu trực tuyến, nơi chia sẽ trao đổi hàng triệu tài liệu như luận văn đồ án, sách, giáo trình, đề thi.
Chúng tôi không chịu trách nhiệm liên quan đến các vấn đề bản quyền nội dung tài liệu được thành viên tự nguyện đăng tải lên, nếu phát hiện thấy tài liệu xấu hoặc tài liệu có bản quyền xin hãy email cho chúng tôi.
Đã 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.