0% found this document useful (0 votes)
45 views17 pages

Constructors and Destructors

The document discusses constructors and destructors in C++. It defines constructors as class member functions that initialize object attributes and are automatically called when an object is created. Destructors destroy objects and are called when objects go out of scope. The document also covers parameterized constructors, copy constructors, calling constructors and destructors, operator overloading, and rules for operator overloading.

Uploaded by

Swapna D
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)
45 views17 pages

Constructors and Destructors

The document discusses constructors and destructors in C++. It defines constructors as class member functions that initialize object attributes and are automatically called when an object is created. Destructors destroy objects and are called when objects go out of scope. The document also covers parameterized constructors, copy constructors, calling constructors and destructors, operator overloading, and rules for operator overloading.

Uploaded by

Swapna D
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/ 17

Constructors and Destructors

October 19, 2022

Constructors and Destructors October 19, 2022 1 / 17


Outline
1 Charcteristics of Constructors and Destructors
2 Applications with Constructors
3 CONSTRUCTORS WITH ARGUMENTS (PARAMETERIZED
CONSTRUCTOR)
4 OVERLOADING CONSTRUCTORS (MULTIPLE CONSTRUCTORS)
5 Copy constructor
6 Calling Constructors and Destructors
7 Operator Overloading
8 The Keyword Operator
9 Binary operator overloading
10 Unary Opeartor Overloading
11 Rules for operator overloading
12 Operator Overloading using friend function
Constructors and Destructors October 19, 2022 2 / 17
Charcteristics of Constructors and Destructors

Constructor
Constructor is a class member function with same name as class
We cannot initialize class members directly
Constructor is a special function which initialises the data members of
a class
Constructor must be in public area of class.
Constructor is automatically called when an object is created
KeyPoints
We cannot create Virtual Constructor
We can overload Constructor
Characteristics of Constructor
Constructor name and class name must be same
Constructor doesn’t return value
Constructor is invoked automatically,when object is created.
Constructors and Destructors October 19, 2022 3 / 17
Charcteristics of Constructors and Destructors

Destructors

Destructors are opposite to the constructor.


The process of destroying the class objects created by constructors is
done in destructor.
The destructors have the same name as their class, preceded by a
tilde ( ).
A destructor is automatically executed when object goes out of scope.
Similar to constructors, the destructors do not have return type and
not even void
Constructors and destructors cannot be inherited, though a derived
class can call the constructors and destructors of the base class
Destructors can be virtual, but constructors cannot.
The destructor does not have any arguments
Destructors neither have default values nor can be overloaded
Constructors and Destructors October 19, 2022 4 / 17
Applications with Constructors

Applications with Constructors

The initialization of member variables of class is carried out using


constructors. The constructor also allocates required memory to the
object. example of a constructor
The programmer need not write any statement to call the constructor.
The compiler automatically calls the constructors.
If programmer writes a statement for calling a constructor, a
constructor is called again.
In case there is no constructor in the program, the compiler calls a
dummy constructor.
The constructor without argument is known as default constructor.
Write a program to show that for each object constructor is called
separately

Constructors and Destructors October 19, 2022 5 / 17


CONSTRUCTORS WITH ARGUMENTS (PARAMETERIZED
CONSTRUCTOR)

PARAMETERIZED CONSTRUCTOR

It is also possible to create constructor with arguments, and such


constructors are called parameterized constructors. For such constructors,
it is necessary to pass values to the constructor when an object is created.

Constructors and Destructors October 19, 2022 6 / 17


OVERLOADING CONSTRUCTORS (MULTIPLE
CONSTRUCTORS)

OVERLOADING CONSTRUCTORS (MULTIPLE


CONSTRUCTORS)

Similar to functions, it is also possible to overload constructors.


A class can contain more than one constructor. This is known as
constructor overloading.
All constructors are defined with the same name as the class they belong
to.
All the constructors contain different number of arguments.
Depending upon the number of arguments the compiler executes
appropriate constructor

Constructors and Destructors October 19, 2022 7 / 17


Copy constructor

Copy Constructor

These are special type of constructors which takes an object as


argument,and is used to copy values of data members of one object
into other object.
Initialisation of an object through another object is called copy
constructor
In other words copying the values of one object to another object.

Constructors and Destructors October 19, 2022 8 / 17


Copy constructor

Syntax

class classname
{
constructorname(classname &objname)
{}
};

Constructors and Destructors October 19, 2022 9 / 17


Calling Constructors and Destructors

Calling Constructors and Destructors

calling destructor manually


object.classname::∼classname();

Constructors and Destructors October 19, 2022 10 / 17


Operator Overloading

Introduction

In C++, we can make operators work for user-defined classes. This means
C++ has the ability to provide the operators with a special meaning for a
data type, this ability is known as operator overloading.
Operator overloading is a compile-time polymorphism. It is an idea of
giving special meaning to an existing operator in C++ without changing
its original meaning.
Example:

int a;
float b,sum;
sum=a+b;

Constructors and Destructors October 19, 2022 11 / 17


The Keyword Operator

The Keyword Operator

Returntype Operator operatorsymbol(parameters)


{
statement 1;
statement 2;
}

Constructors and Destructors October 19, 2022 12 / 17


Binary operator overloading

Binary Operator Overloading

Constructors and Destructors October 19, 2022 13 / 17


Unary Opeartor Overloading

Unary Operator Overloading

The unary operators operate on a single operand and following are the
examples of Unary operators
The increment (++) and decrement (–) operators.
The unary minus (-) operator.
The logical not (!) operator.

Constructors and Destructors October 19, 2022 14 / 17


Rules for operator overloading

Rules for operator overloading


Only built-in operators like (+, -, *, /, etc)can be overloaded.
We cannot overload all of those operators that are not a part of C++
language like ‘$’.
We can’t change the arity of the operators. The arity of an operator
is the number of operands that the operator takes.
During the operator overloading, we cannot change the actual
meaning of an operator. For example, We cannot overload the
plus(+) operator to subtract one value form the other value.
The precedence of the operators remains the same during operator
overloading.
The operator overloading is not possible for built-in data types. At
least one user-defined data types must be there.
Some operators like assignment “=”, address “&” and comma “,” are
by default overloaded.
When using binary operators overloaded through a member function,
the left-hand operand must be an object of the relevant class.
Constructors and Destructors October 19, 2022 15 / 17
Rules for operator overloading

Frame Title

List of operators that cannot be overloaded are mentioned below;


Scope Resolution Operator (::)
Pointer-to-member Operator (.*)
Member Access or Dot operator (.)
Ternary or Conditional Operator (?:)
Object size Operator (sizeof)

Constructors and Destructors October 19, 2022 16 / 17


Operator Overloading using friend function

Operator Overloading using friend function

\textbf{Syntax:}
friend Complex operator + (Complex c1, Complex c2);

Constructors and Destructors October 19, 2022 17 / 17

You might also like