tailieunhanh - Lecture Object oriented programming - Lecture no 22
After you have read and studied this chapter, you should be able to: Include a JFileChooser object in your program to let the user specify a file; write bytes to a file and read them back from the file, using FileOutputStream and FileInputStream; write values of primitive data types to a file and read them back from the file, using DataOutputStream and DataInputStream;. | CSC241: Object Oriented Programming Lecture No 22 Previous Lecture Friend function example program: Distance class Friend function for functional notation Friend classes static functions Assignment operator and copy initialization Today’s Lecture Example program Copy initialization Assignment operator overloaded Memory efficient string class Dynamic Type Information The Copy Constructor Distance d2 = d1; It is similar to what assignment operator (d2 = d1) does The difference is that copy initialization also creates a new object 7 feet inches d1 feet inches d2 7 Example program – assignment and copy constructor class alpha { private: int data; public: alpha() { } alpha(int d) { data = d; } alpha(alpha& a) { data = ; cout << “\nCopy constructor invoked”; } void display() { cout << data; } void operator = (alpha& a) { data = ; cout << “\nAssignment operator invoked”; } }; a2 = a1; alpha a3(a1); Program Output Assignment operator invoked Copy constructor invoked When . | CSC241: Object Oriented Programming Lecture No 22 Previous Lecture Friend function example program: Distance class Friend function for functional notation Friend classes static functions Assignment operator and copy initialization Today’s Lecture Example program Copy initialization Assignment operator overloaded Memory efficient string class Dynamic Type Information The Copy Constructor Distance d2 = d1; It is similar to what assignment operator (d2 = d1) does The difference is that copy initialization also creates a new object 7 feet inches d1 feet inches d2 7 Example program – assignment and copy constructor class alpha { private: int data; public: alpha() { } alpha(int d) { data = d; } alpha(alpha& a) { data = ; cout << “\nCopy constructor invoked”; } void display() { cout << data; } void operator = (alpha& a) { data = ; cout << “\nAssignment operator invoked”; } }; a2 = a1; alpha a3(a1); Program Output Assignment operator invoked Copy constructor invoked When Copy constructor is invoked Copy constructor may be invoked when an object is defined . alpha a3 = a2; or alpha a3(a2); when arguments are passed by value to functions. void func(alpha a) { . . . } called by the statement func(a1); when values are returned from functions alpha func(); a2 = func(); Alpha a = a1; Why Not alpha(alpha a) Constructor? Do we need to use a reference in the argument to the copy constructor? alpha(alpha &a) Could we pass by value instead? alpha(alpha a) No, the compiler complains that it is out of memory if we try to compile Why? Because when an argument is passed by value, a copy of it is constructed What makes the copy? The copy constructor. But this is the copy constructor, so it calls itself It calls itself over and over until the compiler runs out of memory. A Memory-Efficient String Class Defects with the String Class String object contains an array of char s2 = s1; problem with this is that the same string now exists in two (or more) places in memory .
đang nạp các trang xem trước