100% satisfaction guarantee Immediately available after payment Both online and in PDF No strings attached 4.2 TrustPilot
logo-home
Class notes

Basic knowledge for computer programming

Rating
-
Sold
-
Pages
19
Uploaded on
25-04-2024
Written in
2023/2024

Basic details for computer programming beginners

Institution
Course










Whoops! We can’t load your doc right now. Try again or contact support.

Written for

Course

Document information

Uploaded on
April 25, 2024
Number of pages
19
Written in
2023/2024
Type
Class notes
Professor(s)
Mutharasan
Contains
All classes

Subjects

Content preview

Constructors and
Destructors


LEARNING 0BJ.ECTIVES

.:.fter going through this chapter, you will be able to
□ Describe the concepts of constructors and destructors
t::J Explain parameterized constructors
□ Interpret the usage of multiple constructors in a class
□ Discuss dynamic initialization of objects
□ Explain copy and dynamic constructors




6.1 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
z . getdata(l00 ,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 appropriate objects that have
already been created. These functions cannot be used to initialize the member variables at the time of
creation of their objects.
Providing the initial values as described above does not conform with the philosophy of c++
language. We stated earlier that one of the aims of C++ is to create user-defined data types such as class ,
that behave very similar to the built-in types. This means that we should be able to initialize a class type
variable (object) when it is declared , much the same way as initialization of an ordinary variable. For example,

int m = 20;
float x. = 5.7:i;
are valid initialization statements for basic data types .

, Object-Oriented Programming with C++

Similarly, when a variable of built-in type goes out of scope, the compiler automatically destroys th
variable. But it has not happened with the objects we have so far studied. It is therefore clear that some rnore
features of classes need to be explored that would enable us to initialize the objects when they are creat~~
and destroy them when their presence is no longe~ necessary. _
C++ provides a special member function called the constructor which enables an object to initialize its~Jf
when it is created. This is known as automatic initialization of objects. It also provides another mernbFJr
function called the destructor that destroys the objects when they are no longer required.


6.2 CONST RUCTO RS
A constructor is a 'special' member function whose task is to initializ~ the objects of its class. It is special
because its name is the same as the class name. The constructor is invoked whenever an object of its asso-
ciated class is created. It is called constructor because i\construct ~ the values of data members of the class.

A constructo r is declared and defined as follows: .
II class with a construc tor ·
class integer
{
. ,'.
int m, n;
public: ·
integer (void ) ; /.I construc tor declared

};
integer .. integer (void l I ( constr.u ctor defined .
{
t . :
m = o; n = ·o;
}
When a class contains a constructor like the one defined above, it is guaranteed that an object created by
the class will be initialized automatically. Fo_r exarnple, the declaration . :·," .
i nteger intl; II object intl c;eated
not only creates the object int1 of type integer but also initializes its data members m and n to zero. There
is ~o need to write any statement to invoke the constructor function (as we do with the normal member
functions). If a 'normal' member function is defined for zero initialization, we would need to invoke this fu nction
for each of the objects separately. This wol:,lld be very inconvenient, if there are a large number of objects.
A constructor that accepts no parameters is called the default constructor. The default constructor for class
A is A:;A(). If no such constructor is defined, then the compiler supplies a default constructor. Therefore a '
statement such as
A a;
' '
invokes the default constructor of the compiler to create the object a.
The constructo r functions have some special characteristics, These are:
• They should be declared in the public section.
• They are invoked automatically when the objects are created.

, Constructors and Destructors

• They do not have return types, not even void and therefore , and they cannot return values.
• They cannot be inherited , though a derived class can call the base class constructor.
• Like other C++ functions, they can have default arguments.
• constructors cannot be virtual. (Meaning of virtual will be discussed later in Chapter 9.)
• we cannot refer to their addresses.
• An object with a constructor (or destructor) cannot be used as a member of a union .
• They make 'implicit calls' to the operators new and delete when memory allocation is required.

Remember, when a constructor is declared for a class, initialization of the class objects becomes
mandatory.


6.3 PARAMETERIZED CONSTRUCTORS
The constructor integer(), defined above, initializes the data members of all the objects to zero. However, in
practice it may be necessary to initialize the various data elements of different objects with different values
when they are created. C++ permits us to achieve this objective by passing arguments to the constructor
function when the objects are created. The constructors that can take arguments
. . .
are called parameterized
"' ' ' . •. .
constructors.
The constructor integer() may be modified to ·take arguments as shown below:
class integer

int m, n;
public:
integer ( i nt x, i nt y ) ; II parameterized constructor


};
integer .. integer(int x, int . y)
{
m = x; n = y;


When a constructor has been parameterized, the object declaration statement such as

i nteger intl ;
may not work. We must pass the initial values as arguments to the constructor function when an object is
declared. This can be done in two ways:

• By calling the constructor explicitly.
• By calling the constructor implicitly.
The following declaration illustrates the first meth0 d:
integer intl = intege r (0,100) i
II exp.licit C,iII

. This statement creates an integer object int1 and passes the values O and 100 to it. The second is
implemented as follows:

integer intl(0,100); I I i llljll iv i t C.i ll




r~
$9.49
Get access to the full document:

100% satisfaction guarantee
Immediately available after payment
Both online and in PDF
No strings attached

Get to know the seller
Seller avatar
கர்ணன்mк1

Get to know the seller

Seller avatar
கர்ணன்mк1 Online
Follow You need to be logged in order to follow users or courses
Sold
0
Member since
1 year
Number of followers
0
Documents
1
Last sold
-

0.0

0 reviews

5
0
4
0
3
0
2
0
1
0

Recently viewed by you

Why students choose Stuvia

Created by fellow students, verified by reviews

Quality you can trust: written by students who passed their tests and reviewed by others who've used these notes.

Didn't get what you expected? Choose another document

No worries! You can instantly pick a different document that better fits what you're looking for.

Pay as you like, start learning right away

No subscription, no commitments. Pay the way you're used to via credit card and download your PDF document instantly.

Student with book image

“Bought, downloaded, and aced it. It really can be that simple.”

Alisha Student

Frequently asked questions