Lecture Object oriented programming - Lecture no 09

In this chapter you will learn about the following: Design as a series of design decisions; various approaches and types of design, including top-down design starting with the architecture, and bottom-up design starting with utilities; design principles that lead to maintainable software, such as ‘divide and conquer’, striving for high cohesion and low coupling, as well as using good abstractions to hide details, thus simplifying the system;. | CSC241: Object Oriented Programming Lecture No 09 Previous Lecture Operator overloading Overloading unary operator ++ - - Argument of overloaded function Post increment Counter operator ++ () { return Counter(++count); } Counter operator ++ (int) { return Counter(count++); } Today’s Lecture Example : Distance class overloading ++ and – ++ add 1 in feet and 1 in inches, -- subtract 1 from feet and 1 from inches Prefix and postfix Overloading binary operator Example – Distance class Overload pre-increment and pre-decrement unary operator for distance class such that Increment: add 1 in feet and 1 in inches Decrement: subtract 1 from feet and 1 from inches Pre increment – prefix Post increment – post fix Write to program Overloading Binary Operators Binary operator can be overloaded as easy as unary operator by using a non static fucntion Distance objects could be added using a member function add_dist(): (dist1, dist2); By overloading the + operator we can reduce this . | CSC241: Object Oriented Programming Lecture No 09 Previous Lecture Operator overloading Overloading unary operator ++ - - Argument of overloaded function Post increment Counter operator ++ () { return Counter(++count); } Counter operator ++ (int) { return Counter(count++); } Today’s Lecture Example : Distance class overloading ++ and – ++ add 1 in feet and 1 in inches, -- subtract 1 from feet and 1 from inches Prefix and postfix Overloading binary operator Example – Distance class Overload pre-increment and pre-decrement unary operator for distance class such that Increment: add 1 in feet and 1 in inches Decrement: subtract 1 from feet and 1 from inches Pre increment – prefix Post increment – post fix Write to program Overloading Binary Operators Binary operator can be overloaded as easy as unary operator by using a non static fucntion Distance objects could be added using a member function add_dist(): (dist1, dist2); By overloading the + operator we can reduce this dense-looking expression to dist3 = dist1 + dist2; class Distance { private: int feet; float inches; public: Distance() : feet(0), inches() { } Distance(int ft, float in) : feet(ft), inches(in) { } void showdist() const { cout = ) { i -= ; f++; } return Distance(f,i); } }; Distance dist1(3, ); Distance dist2(11, ); Distance dist3; dist3 = dist1 + dist2; dist1 feet inches 3 dist2 feet inches 2 dist3 feet inches Distance class d2 feet inches 2 feet inches 5 5 Distance dist4; dist4 =dist3 + dist1 + dist2; dist4 feet inches 10 Cont. dist3 = dist1 + dist2 The argument on the left side of the operator (dist1 in this case) is the object of which the operator is a member The object on the right side of the operator (dist2) is as an argument to the operator + function The operator returns a object by value, which is .

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
241    102    3    17-06-2024
154    89    1    17-06-2024
Đã 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.