0% found this document useful (0 votes)
14 views

OOP Lab 06

Uploaded by

Asmara Minhas
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

OOP Lab 06

Uploaded by

Asmara Minhas
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

UNIVERSITY OF CHAKWAL

DEPARTMENT OF COMPUTER SCIENCE


(Object Oriented Programming)

Lab 06

Destructor, Constant and Static


Data Members,This pointer

Lab Instructor: Engr. Samina Bilquees


Objectives
In this lab, you will learn:
 Destructor
 Constant Data Members, Constant Member Functions, and Constant Objects
 Static Data Members and its initialization and accessing
 Getter and setter method

 “This” Pointer
1. Destructure
A destructor is also a special member function as a constructor. Destructor destroys the class objects
created by the constructor. Destructor has the same name as their class name preceded by a tilde (~)
symbol. It is not possible to define more than one destructor. The destructor is only one way to destroy the
object created by the constructor. Hence destructor can-not be overloaded. Destructor neither requires any
argument nor returns any value. It is automatically called when the object goes out of scope. Destructors
release memory space occupied by the objects created by the constructor. In destructor, objects are
destroyed in the reverse of object creation.

The syntax for defining the destructor within the ~<class-name> () { }

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.

 Destructor cannot be declared as static and const;

 Destructor should be declared in the public section of the program.

 Destructor is called in the reverse order of its constructor invocation

2. Constant Data Members:


Concepts
Data members of a class may be declared as const. Such a data member must be initialized by
the constructor using an initialization list. Once initialized, a const data member may never be
modified, not even in the constructor or destructor. Data members that are both static
and const have their own rules for initialization. Furthermore, a const data member cannot be
initialized at the time of declaration or within the member function definition. To initialize
a const data member of a class, follow given syntax:

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.

5.1 Accessing and Initialization


The following example illustrates the initialization and accessing process of static data
members:

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:

5. Access Private Members


To access a private attribute, use public "get" and "set" methods:
Explanation
 The salary attribute is private, which have restricted access.
 The public setSalary() method takes a parameter (s) and assigns it to the salary attribute (salary =
s).
 The public getSalary() method returns the value of the private salary attribute.
 Inside main(), we create an object of the Employee class.
 Now we can use the setSalary() method to set the value of the private attribute to 50000.
 Then we call the getSalary() method on the object to return the value.

You might also like