OOP Lab 06
OOP Lab 06
Lab 06
The syntax for defining the destructor outside the <class-name> : : ~ <class-name>{}
Characteristics of a destructor:-
Destructor is invoked automatically by the compiler when its corresponding constructor goes out
of scope and releases the memory space that is no longer required by the program.
Destructor neither requires any argument nor returns any value therefore it cannot be overloaded.
The following example illustrates the initialization and declaration of constant data members:
Output
3. Constant Member Functions:
The const member functions are the functions which are declared as constant in the program. The
object called by these functions cannot be modified. It is recommended to use const keyword so
that accidental changes to object are avoided. A const member function can be called by any
type of object. Non-const functions can be called by non-const objects only. The following is
an example that illustrates the syntax of constant member functions:
Output:
4. Constant Objects:
Like member functions and data members, the objects of a class can also be declared as const.
An object declared as const cannot be modified and hence, can invoke only const member
functions as these functions ensure not to modify the object.
A const object can be created by prefixing the const keyword to the object declaration. Any
attempt to change the data member of const objects results in a compile-time error. The
following example illustrates the declaration and usage of constant object along with constant
member function:
Output:
5. Static Data Members:
When a member variable is declared with the keyword static, there will be only one copy of the
member variable in memory, regardless of the number of instances of the class that might
exist. A single copy of a class’s static member variable is shared by all instances of the class.
Output:
5.2 Lifetime
Even though static member variables are declared in a class, they are actually defined outside the
class declaration. The lifetime of a class’s static member variable is the lifetime of the
program. This means that a class’s static member variables come into existence before any
instances of the class are created.
6. “this” Pointer:
Every object has access to its own address through an important pointer called “this” pointer.
The “this” pointer is an implicit parameter to all member functions. Therefore, inside a member
function, this may be used to refer to the invoking object. To summarize, the “this” pointer
holds the address of current object, in simple words you can say that this pointer points to the
current object of the class.
Let’s take an example to understand this concept. Here, you can see that we have two data
members num and ch. In member function setMyValues() we have two local variables having
same name as data members name. In such case if you want to assign the local variable value to
the data members then you won’t be able to do until unless you use this pointer, because the
compiler won’t know that you are referring to object’s data members unless you use
“this” pointer. This is one of the example where you must use this pointer.
Output: