Đang chuẩn bị nút TẢI XUỐNG, xin hãy chờ
Tải xuống
After you have read and studied this chapter, you should be able to: Perform linear and binary search algorithms on small arrays, determine whether a linear or binary search is more effective for a given situation, perform selection and bubble sort algorithms, describe the heapsort algorithm and show how its performance is superior to the other two algorithms, apply basic sorting algorithms to sort an array of objects. | CSC241: Object Oriented Programming Lecture No 21 Previous Lecture Polymorphism Example program person class Virtual destructor base class destructor is virtual, derived class destructor also become virtual Friend functions can access private member of a class Today’s Lecture Friend function example program: Distance class Friend function for functional notation Friend classes static functions Distance Example class Distance { private: int feet; float inches; public: Distance() : feet(0), inches(0.0) { } Distance (float fltfeet) { feet = fltfeet; inches = 12*(fltfeet-feet); } Distance(int ft, float in) : feet(ft), inches(in) { } void showdist() const { cout = 12.0) { i -= 12.0; f++; } return Distance(f,i); } When such constructor exists, following statements are allowed d3 = 15.5; d3 = d1 + 10.0; . | CSC241: Object Oriented Programming Lecture No 21 Previous Lecture Polymorphism Example program person class Virtual destructor base class destructor is virtual, derived class destructor also become virtual Friend functions can access private member of a class Today’s Lecture Friend function example program: Distance class Friend function for functional notation Friend classes static functions Distance Example class Distance { private: int feet; float inches; public: Distance() : feet(0), inches(0.0) { } Distance (float fltfeet) { feet = fltfeet; inches = 12*(fltfeet-feet); } Distance(int ft, float in) : feet(ft), inches(in) { } void showdist() const { cout = 12.0) { i -= 12.0; f++; } return Distance(f,i); } When such constructor exists, following statements are allowed d3 = 15.5; d3 = d1 + 10.0; Cont. d3 = 10.5; 10.5 is passed to one argument constructor, 10.5 is converted into Distance object d3 = nameless object of Distance; d3 = d1 + 9.75; 9.75 is converted into Distance object by calling one argument constructor Then operator + function is invoked by d1 object d3 = 6.5 + d1; d3 = Distance(6.5) + d1; Note When a float value pass as argument to function Definition: void function (Distance d1) { } Call : function (10.75); One argument constructor is called Distance(float) Convert float value i.e. 10.75 into Distance object Call : function (Distance (10.75)) function (Nameless Distance object) d3 = 10.75 + d1; Neither float value 10.75 invoke operator + function Nor operator knows to convert float to Distance Distance Example – Friend function class Distance { . . . friend Distance operator + ( Distance, Distance ) const; }; Distance Distance::operator + (Distance d1, Distance d2) const { int f = d1.feet + d2.feet; float i = d1.inches + d2.inches; if(i >= 12.0) i -= 12.0; f .