tailieunhanh - Chapter 7 Constructors and Other Tools

Chapter and Other Objectives.♦ Constructors , Definitions , Calling . More Tools const parameter modifier , Inline functions Static member data ,Vectors Introduction to vector class Copyright | Chapter 7 Constructors and Other Tools Learning Objectives Constructors Definitions Calling More Tools const parameter modifier Inline functions Static member data Vectors Introduction to vector class Constructors Initialization of objects Initialize some or all member variables Other actions possible as well A special kind of member function Automatically called when object declared Very useful tool Key principle of OOP Constructor Definitions Constructors defined like any member function Except: Must have same name as class Cannot return a value; not even void! Constructor Definition Example Class definition with constructor: class DayOfYear { public: DayOfYear(int monthValue, int dayValue); //Constructor initializes month & day void input(); void output(); private: int month; int day; } Constructor Notes Notice name of constructor: DayOfYear Same name as class itself! Constructor declaration has no return-type Not even void! Constructor in public section It’s called when | Chapter 7 Constructors and Other Tools Learning Objectives Constructors Definitions Calling More Tools const parameter modifier Inline functions Static member data Vectors Introduction to vector class Constructors Initialization of objects Initialize some or all member variables Other actions possible as well A special kind of member function Automatically called when object declared Very useful tool Key principle of OOP Constructor Definitions Constructors defined like any member function Except: Must have same name as class Cannot return a value; not even void! Constructor Definition Example Class definition with constructor: class DayOfYear { public: DayOfYear(int monthValue, int dayValue); //Constructor initializes month & day void input(); void output(); private: int month; int day; } Constructor Notes Notice name of constructor: DayOfYear Same name as class itself! Constructor declaration has no return-type Not even void! Constructor in public section It’s called when objects are declared If private, could never declare objects! Calling Constructors Declare objects: DayOfYear date1(7, 4), date2(5, 5); Objects are created here Constructor is called Values in parens passed as arguments to constructor Member variables month, day initialized: 7 5 4 5 Constructor Equivalency Consider: DayOfYear date1, date2 (7, 4); // ILLEGAL! (5, 5); // ILLEGAL! Seemingly OK CANNOT call constructors like other member functions! Constructor Code Constructor definition is like all other member functions: DayOfYear::DayOfYear(int monthValue, int dayValue) { month = monthValue; day = dayValue; } Note same name around :: Clearly identifies a constructor Note no return type Just as in class definition Alternative Definition Previous definition equivalent to: DayOfYear::DayOfYear( int monthValue, int dayValue) : month(monthValue), day(dayValue) { } Third line called "Initialization .

TỪ KHÓA LIÊN QUAN
crossorigin="anonymous">
Đã 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.