tailieunhanh - Lecture Object oriented programming - Lecture no 21
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() { } Distance (float fltfeet) { feet = fltfeet; inches = 12*(fltfeet-feet); } Distance(int ft, float in) : feet(ft), inches(in) { } void showdist() const { cout = ) { i -= ; f++; } return Distance(f,i); } When such constructor exists, following statements are allowed d3 = ; d3 = d1 + ; . | 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() { } Distance (float fltfeet) { feet = fltfeet; inches = 12*(fltfeet-feet); } Distance(int ft, float in) : feet(ft), inches(in) { } void showdist() const { cout = ) { i -= ; f++; } return Distance(f,i); } When such constructor exists, following statements are allowed d3 = ; d3 = d1 + ; Cont. d3 = ; is passed to one argument constructor, is converted into Distance object d3 = nameless object of Distance; d3 = d1 + ; is converted into Distance object by calling one argument constructor Then operator + function is invoked by d1 object d3 = + d1; d3 = Distance() + d1; Note When a float value pass as argument to function Definition: void function (Distance d1) { } Call : function (); One argument constructor is called Distance(float) Convert float value . into Distance object Call : function (Distance ()) function (Nameless Distance object) d3 = + d1; Neither float value 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 = + ; float i = + ; if(i >= ) i -= ; f .
đang nạp các trang xem trước