Constructors and Destructors
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
PARAMETERIZED CONSTRUCTOR
Copy Constructor
Syntax
class classname
{
constructorname(classname &objname)
{}
};
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;
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.
Frame Title
\textbf{Syntax:}
friend Complex operator + (Complex c1, Complex c2);