TAILIEUCHUNG - Object Oriented Programming (21)

CSC241: Object Oriented Programming. Lecture No 23. 1 Previous Lecture.•. Example program. – Copy initialization. – Assignment operator overloaded.•. Memory efficient string class. 2 Today’s Lecture.•. Efficient string class. – Resolve problem while deleting String objects.•. Dynamic Type Information. 3 Cont•. It’s more efficient to create a new class to store. the count.•. Each object of this class, which we call strCount,. contains a count and also a pointer to the string. 4 Contmain() { void operator. String(). String(char*. String(String& s) ={ (String&. S) { S) {. {if(psc->count==1). psc. psc == new. new strCount(“\0”); }. String s3 = “hello psc. delete. strCount(s); = psc;. }; ~strCount(). world.”;. cout count)++; { delete[]. int length = strlen(s);. String }str =(psc->count)--;. new char[length+1];. str; }. psc = ;. s1;. s1 = strcpy(str, s);. (psc->count)++; Go to. count=1;. }. s3;<< “\ns1=”; ();.cout } program. String s2(s3); S2. S3 << “\ns2=”; (); *psc:. *psc: *psc:.} : :. Output strcount. count: 1. 2. 3 strcount. count: hello world. addr=7550 *str: *str:.s1= hello world. addr= hello world. addr=7550 hello world \0 5 Note.•. Why must the assignment operator in String. worry about deletion?. S2 S1 S3. *psc:7550 8750. *psc:7550 *psc:7550. 8750. : :. 7550 8750. strcount. count: 1 strcount. count:10. 21. *str: 650 *str: 975 650 hello world 975 Good. morning. S1 =. S3 =. S2;. S1; 6 Cont•. String object on th

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.