Lecture Object oriented programming - Lecture no 03

In the previous two chapters, you learned about technologies that software engineers need to master before developing applications. Now, we can start thinking about the particular problem we wish to solve. We will first put effort into understanding the background of the problem, a process called domain analysis. Then we will look at the information you have to gather so that you can describe the problem and its proposed solution. Finally, we will discuss some techniques for gathering and analyzing that information. | CSC241: Object Oriented Programming Lecture No 03 Previous Lecture Simple C++ program using cin and cout Member function with parameters GradeBook class Book class Distance class Set functions and get functions A little about Constructor Today’s Lecture Constructor – example program Placing class in separate file Destructor – example program Constructor with arguments – example program Example program – Distance class Data members Feet Inches Member functions void setdist(int ft, float in); void getdist(); void initialize(); void showdist(); Go to program Distance class – data member initialization Distance class shows two ways to initialize the data items in an object void initialize(); void setdist(int ft, float in); Can an object be initialized whenever it is created, without requiring a separate call to a member function? Automatic initialization is carried out using a special member function called a constructor. A constructor is a member function that is executed automatically . | CSC241: Object Oriented Programming Lecture No 03 Previous Lecture Simple C++ program using cin and cout Member function with parameters GradeBook class Book class Distance class Set functions and get functions A little about Constructor Today’s Lecture Constructor – example program Placing class in separate file Destructor – example program Constructor with arguments – example program Example program – Distance class Data members Feet Inches Member functions void setdist(int ft, float in); void getdist(); void initialize(); void showdist(); Go to program Distance class – data member initialization Distance class shows two ways to initialize the data items in an object void initialize(); void setdist(int ft, float in); Can an object be initialized whenever it is created, without requiring a separate call to a member function? Automatic initialization is carried out using a special member function called a constructor. A constructor is a member function that is executed automatically whenever an object is created. Constructors C++ requires a construct call for each object it has created This ensure that object is initialized properly before it is used If there is no constructor, the compiler provides a default constructor that is, a constructor with no parameters Name of constructor function is same as name of class A counter example Data member Count Member function Constructor void inc_count() int get_count() Automatic initialization An object of type Counter is first created, we want its count to be initialized to 0 Options are set_count() function (call it with an argument of 0) zero_count() function, to set count to 0. Such functions would need to be executed every time we created a Counter object Cont. A programmer may forget to initialize the object after creating it It’s more reliable and convenient to cause each object to initialize implicitly when it is created In the Counter class, the constructor Counter() is called automatically whenever a new object of

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
Đã 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.