Lecture Object oriented programming - Lecture no 04

In this chapter you will learn about the following: How to properly use the most essential features of UML class diagrams: classes, associations, generalizations and interfaces; the basics of Object Constraint Language (OCL); typical problems you will encounter when modeling with class diagrams; a step-by-step process for systematically developing class diagrams; basic techniques for implementing class diagrams in Java. | CSC241: Object Oriented Programming Lecture No 04 Previous Lecture Constructor – example program Placing class in separate file Destructor – example program Constructor with arguments – example program Today’s Lecture Overloaded function Constructor const (constant) object member function data member object as function argument friend function this pointer Objects as Function Arguments class Distance { //Distance class private: int feet; float inches; public: Distance() : feet(0), inches() { } Distance(int ft, float in) : feet(ft), inches(in) { } void getdist(){ cout > feet; cout > inches; } void showdist(){ cout = ) { inches -= ; feet++; } feet += + ; } main() { Distance dist1, dist3; Distance dist2(11, ); (); . | CSC241: Object Oriented Programming Lecture No 04 Previous Lecture Constructor – example program Placing class in separate file Destructor – example program Constructor with arguments – example program Today’s Lecture Overloaded function Constructor const (constant) object member function data member object as function argument friend function this pointer Objects as Function Arguments class Distance { //Distance class private: int feet; float inches; public: Distance() : feet(0), inches() { } Distance(int ft, float in) : feet(ft), inches(in) { } void getdist(){ cout > feet; cout > inches; } void showdist(){ cout = ) { inches -= ; feet++; } feet += + ; } main() { Distance dist1, dist3; Distance dist2(11, ); (); (dist1, dist2); cout << “\ndist1 = “; (); cout << “\ndist2 = “; (); cout << “\ndist3 = “; (); } Overloaded Constructors It’s convenient to be able to give variables of type Distance a value when they are first created Distance dist2(11, ); which defines an object, and initializes it to a value of 11 for feet and for inches. Distance dist1, dist2; then No-argument constructor is called/invoked (the default constructor) Since there are now two constructors with the same name, Distance(), we say the constructor is overloaded Member Functions Defined Outside the Class Such functions, needs to have a prototype/declaration within the class The function name, add_dist(), is preceded by the class name, Distance, and a new symbol—the double colon (::). This symbol is called the scope resolution operator. It is a way of specifying what class something is associated with In this situation, Distance::add_dist() means “the add_dist() .

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
41    158    18    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.