TAILIEUCHUNG - Constructors and Destructors

Constructors and destructors perform special functions as members of classes. Constructors are used to initialize objects. Conversely, you can use destructors to delete an object when it is not required. This chapter introduces the concept of constructors and destructors. In addition, the chapter explains how to implement constructors and destructors. The chapter also explains how constructors and destructors help in identifying the life cycle of the object. | Constructors and Destructors Constructors and destructors perform special functions as members of classes. Constructors are used to initialize objects. Conversely you can use destructors to delete an object when it is not required. This chapter introduces the concept of constructors and destructors. In addition the chapter explains how to implement constructors and destructors. The chapter also explains how constructors and destructors help in identifying the life cycle of the object. Objectives In this chapter you will learn to fl Implement constructors fl Implement destructors fl Identify the life cycle of an object Chapter 6 Implementing Constructors A constructor is a special type of method that is invoked when you create a new instance of a class. A constructor is used to initialize the members of the class. The name of a constructor is the same as the name of the class that contains it. A constructor is complementary to a destructor. It is good programming practice to use both constructors and destructors. The Need of Constructors To initialize member variables a function can be invoked as soon as an object is created. The following program is an example of writing an initialization function using System namespace Calc class Calculator static int numberl number2 total public void Intialize number1 10 number2 20 public void AddNumber total number1 number2 public void DisplayNumber The Total is 0 total public static void Main string args Calculator c1 new Calculator In the preceding example the c1 object of the Calculator class has to call the Initialize member function to initialize the data members number1 and number2. However you need to ensure that the initialize function is invoked. NIIT Constructors and Destructors .

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