Lecture Object oriented programming - Lecture no 18

After you have read and studied this chapter, you should be able to: Manipulate a collection of data values, using an array; declare and use an array of primitive data types in writing a program; declare and use an array of objects in writing a program; define a method that accepts an array as its parameter and a method that returns an array; describe how a two-dimensional array is implemented as an array of arrays; manipulate a collection of objects, using lists and maps. | CSC241: Object Oriented Programming Lecture No 18 Previous lecture Example program – inheritance Relationship between class Association Aggregation Composition ID 6 name ali e c *emp name ali house 0 name Length Name Width Length Name Width 1 rooms[2] Today’s Lecture Default copy constructor Memory management String class using new operator Pointer to object Array of pointers to objects Polymorphism Default Copy constructor Two ways to initialize objects. no-argument constructor Distance d1; multi-argument constructor Distance d2(4, 5,76); An object can be initialize with another object of the same type Distance d1 = d2; A default copy constructor will be called One argument constructor whose argument is an object of same class Cont. If no copy constructor define in class then complier adds default copy constructor When default copy constructor is invoked When object is pass as an argument to a function Distance d1, d2, d3; (d1, d2); When an object is return by value . | CSC241: Object Oriented Programming Lecture No 18 Previous lecture Example program – inheritance Relationship between class Association Aggregation Composition ID 6 name ali e c *emp name ali house 0 name Length Name Width Length Name Width 1 rooms[2] Today’s Lecture Default copy constructor Memory management String class using new operator Pointer to object Array of pointers to objects Polymorphism Default Copy constructor Two ways to initialize objects. no-argument constructor Distance d1; multi-argument constructor Distance d2(4, 5,76); An object can be initialize with another object of the same type Distance d1 = d2; A default copy constructor will be called One argument constructor whose argument is an object of same class Cont. If no copy constructor define in class then complier adds default copy constructor When default copy constructor is invoked When object is pass as an argument to a function Distance d1, d2, d3; (d1, d2); When an object is return by value Prototype : Distance add_dist(Distance d); Function call: d3 = (d1); Copy constructor—Function Arguments The copy constructor is invoked when an object is passed by value to a function. It creates the copy that the function operates on. Thus if the function void func(alpha a); called by the statement func(a1); then the copy constructor would be invoked to create a copy of the a1 object for use by func(). Copy constructor is not invoked if the argument is pass by reference or by pointer. In these case no copy is created Copy constructor—Function Return Values The copy constructor also creates a temporary object when a value (an object) is returned from a function. Suppose there was a function alpha func(); and this function was called by the statement a2 = func(); The copy constructor would be invoked to create a copy of the value returned by func(), and this value would be assigned to a2 Memory management It is an act of managing computer memory It provides ways to dynamically .

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
Đã 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.