Notes of E-Comm SY 2021-22, JAVA Programming Constructor & Destructor - Study Material
Page 1 :
‘Constructing objects | Constructors | Constructor overloading | Default argument, constructor | Copy constructor | Constructing matrix objects | Automatic initialization, “Gemeysaeny | Default constructor | Dynamic initialization | Dynamic, , as Introduction, , ‘We have seen, so far, a few examples of classes being implemented. In all the cases, we have used, , member functions such as putdata() and setvalue() to provide initial values to the private member, variables. For example, the following statement, , A. input ()?, , invokes the member function input(), which assigns the initial values to the data items of object A, Similarly, the statement, , x. getdata (100,299.95);, , passes the initial values as arguments to the function getdata(), where these values are assigned to, the private variables of object x. All these ‘function call’ statements are used with the appropnate, objects that have already been created. These functions cannot be used to initialize the member, _ Variables at the time of creation of their objects., , int m= 20;, flost x = 5.753, , re valid initialization statements for basic data types.
Page 4 :
conented Programa with ** a Class, Multiple Constructors aid |, , | constructors: They are, j// No @ rgument s, , oO arguments, , jJues are passed by, a values and no va Y the, itself supplies the dat the appropriate values from main, , 436 Onject, , // Tw, , , , constructor "', , In the first case, the function call passes, , calling program. In the _— ai tors in the same class. For example, we could define g, bol situ, , Cee permits us 10 use, , class integer, , int ™, ne, public:, integer () (m=07 n=07}, integer(int a, int b), {m= az n = be}, integer(integer 6 i), {m = i.my n = i.ny), , // constructor 2, // constructor 2, , // constructor 3, , This — three constructors for an integer object. The first constructor receives no arguments, the second receives two integer arguments and the third i, rt ae = receives one integer object as an, , integer 11;, would automatically invoke, lly the first constructor and set both m and n of I1 to zero. The statement, integer I2(20,40);, , would call the second constr, respect ctor which will initi, ively, Finally, the statement will initialize the data members m and n of 12 to 20 and 40, , ‘nteger I3(12);, , , , ‘Us., ing Aamespace stq:
Page 5 :
0 . 18, , , , , 10000 3 0.18, 10000 3, , peposit 1, Principal Amount, return Valve, , peposit 2, cipal Amount, Return Value, , r, , , , Deposit 3, Principal Amount, Return Value, , amount, per!, , The output of Program 6.3 would be, , gnter amount, period,, , interest: catetin percent), , Antereat Fate (in dacinal iva, , enter amount and periog, , 103900, , 1#430.9, , 10000, , = 16430.3, , 10000, = 14049.3, The program uses three overloaded constructors. The parameter values to these constructors are ,, provided at run time. The user can provide input in one of the following forms:, , 1. Amount, period and interest in decimal form., 2. Amount, period and interest in percent form. e, 3. Amount and period., , , , | 67 ae, , We briefly mentioned about the copy constructor in Sec. 6.3. We used the copy constructor, , *s, , , , integer(integer 61); or q, i, in Sec. 6.4 as one of the overloaded constructors., , As stated earlier, a copy constructor is, , For example, the statement, integer I2(I1);, , Would define the object 12 and at the same time, , Statement is, , Used to declare and initialize an object from another object, , initialize it to the values of 11. Another form of this, , —— Errtrti“<itsS