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

What Is A Constructor?

A constructor is a special member function that initializes objects of a class. It has the same name as the class and no return type. Constructors are called automatically when objects are created. There are different types of constructors like default, parameterized, and copy constructors. The default constructor initializes all objects with the same values while parameterized constructors allow initializing objects with different values. Copy constructors initialize one object using another object of the same class. Defining copy constructors is important to properly initialize objects.

Uploaded by

Alone Stranger
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views

What Is A Constructor?

A constructor is a special member function that initializes objects of a class. It has the same name as the class and no return type. Constructors are called automatically when objects are created. There are different types of constructors like default, parameterized, and copy constructors. The default constructor initializes all objects with the same values while parameterized constructors allow initializing objects with different values. Copy constructors initialize one object using another object of the same class. Defining copy constructors is important to properly initialize objects.

Uploaded by

Alone Stranger
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 15

What is a constructor?

 It is a special type of member function whose


task is to initialize the objects of the class.
class marks
{
 A constructor has: int n, m;
(i) the same name as the class itself public:
marks()
(ii) no return type {
 A constructor is called automatically n=12;m=13;
whenever a new instance of a class is created.
}
void add()
 You must supply the arguments to the {
constructor when a new instance is created. cout<<(n+m);
 If you do not specify a constructor, the
}
};
compiler generates a default constructor for
you (expects no parameters and has an empty void main()
body). {
marks ob;
ob.add();
}
Special characteristics of constructors

They should be declared in the public section.


They are invoked automatically when the objects are
created.
They should not have return types, not even void and
therefore can’t return values.
They can’t be inherited through a derived class.
They can have default arguments.
Syntax for declaration of constructor Types of constructors

class_name
{  
public:  The three basic types of
class_name( parameter list) constructor available in c++ are :
{
function body //constructor function  Default constructor.
 Parameterized constructor.
}  Copy constructor.
~ class_name( parameter list)
{
function body // destructor function
}
};
Default Constructor
A constructor that accepts no parameters is called default constructor.

Example :- When a class contains a constructor like the


one defined above, it is clear that an object
Class integer created by the class will be initialized
{ automatically. The declaration
int m,n;
Public:
Integer int1; // object int1 is created
integer() //default constructor
{
m=10; Note only creates the object int1 but also
n=10; initialized its data member m and n to 10.
}
};
Parameterized Constructor
In default constructor every objects are initialized with the same values but in many cases, it
may be necessary to initialize the various data elements of different object with different
values when they are permits us to achieve this objective by passing argument to the construct
function. When the objects are created, the constructors that can take arguments are called
parameterized constructors.

When a constructor has been parameterized, the


E.g. Class integer
object of declaration statement as
{
int m,n;
integer int1;
Public:
integer int1=integer(0,100);
integer(int x, int y) //
parameterized constructor
This statement creates an integer objects int1 &
{
passes the values 0 and 100 to it.
m=x;
integer int1(0,100);
n=y;
}
};
Constructor with default arguments

Constructor with default argument means an argument value is specified in


constructor declaration which is used if the corresponding actual
parameter value is omitted when constructor is invoked.

Syntax for default argument :


While declaring the function the
arguments without default values are return_type function_name(data_type
placed first and those with default values parameter_name = constant_value);
are placed later as the first or middle
arguments can not be omitted. Example :
The default arguments are specified
in function prototype if function is int test(int a, int b = 5); // function test
defined later else those are given in is having argument b with default
function definition.
value.
Multiple constructors (constructor overloading)
Example :-
Class integer
{
int m,n;
Public:
Integer() // default constructor
{
m=10;
n=10;
}
integer(int x) // parameterized constructor
{
m=x;
n=x;
}
integer(int x,int y,int z=100) // constructor with default argument
{
m=x;
n=y+z;
}
};
Operator overloading
Overloading unary and binary operators. :

It allows to extend functionality of an existing operator to operate on user defined data


types also. The operation of operator when the operands are of user defined data types
depends upon operator overloading done in program.

Unary operator overloading : Overloading of operators operating on single operand.


There should be no arguments to the operator function.

Binary operator overloading : Overloading of operators operating on two


operands. There should be one parameter to the operator function.

Following operator can’t be overloaded :-


 
1) :: Scope resolution operator.
2) Sizeof operator
3) ?: conditional (ternary) operator
4) * pointer to class member operator
5) . dot operator
Syntax for declaration of operator function :-

For declaration of the operator overloading

return_ datatype operator operator_to_overload(arguments);


e.g.
int operator + (test t); // operator function to overload '+' operator

To declare a class distance to store distance in feet and inches. Inches will increment by 1 if
++ operator is used with the object i.e. ++ operator is overloaded.

• Rules for overloading operators :-

1. Only existing operator can be overloaded. new operator cannot be created.


2. The overloaded operator must have at least one operand that is of user define type.
3. We cannot change the basic meaning of an operator. that is cannot redefine the
plus(+) operator to subtract one value from the other.
4. Overloaded operator follow the syntax rules of original operator.
Copy Constructor
A copy constructor can be used to declare
and initialized an object of another object.
A constructor can accept a reference For example, the statement
to its own class as a parameter which is
called as a copy constructor. Time T2(T1);
Therefore the following constructor is Would define the object T2 and at the same
valid time initialize it to the values of T1.
The process of initializing through a copy
Class Time constructor is known as copy initialization.
{ But the statement
……
Public: T2=T1;
Time(Time&); //copy
constructor Will not invoke the copy constructor.
}; However , if T1 & T2 are objects, this
statement is legal and simply assigns the
values of T1 to T2 member by member.
Example of copy constructor
#include<iostream> main()
class sample {
{ clrscr();
int number; sample a(40);
public: sample b(a);
sample(int x) sample c=a;
{ cout << "\n The data for a object \n";
number=x; a.display();
} cout << "\n The data for b object \n";
sample(sample &m) // copy constructor b.display();
{ cout << "\n The data for c object \n";
number=m.number; c.display();
} getch();
void display() }
{
cout << "\n a = " << number;
}
};
Defining copy constructors is very
important

In the absence of a copy constructor, the C++ compiler


builds a default copy constructor for each class which is
doing a memberwise copy between objects.
Default copy constructors work fine unless the class
contains pointer data members ... why???
Following cases may result in a call to a copy
constructor
When an object is returned by value
When an object is passed (to a function) by
value as an argument
When an object is thrown
When an object is caught
When an object is placed in a brace-enclosed
initializer list
Destructor Function

It is a special function just


Syntax for declaration of destructor
opposite constructor. The
important characteristics of class class_name
destructor are :- {
1) Destructor function also public:
class_name( parameter list)
has the same name as the class but {
preceded by a tilde (~). function body //constructor function
2) When an object is out of }
~ class_name( parameter list)
scope destructor function is called
{
automatically. function body // destructor function
3) The destructor has no }
arguments and also does not };
return any value.
Example of Destructor
main()
{
#include<iostream> clrscr();
class sample sample x;
{ x.display();
int a,b; sample y;
public: y.display();
sample() cout << "\n The y object is
{ destroyed";
a=100; sample z;
b=200; z.display();
} cout << "\n the z object is
~sample() destroyed ";
{ getch();
cout << "\n OBJECT IS DESTROYED "; }
}
void display()
{
cout << "\n a = " << a << "\n b = " << b;
}
};

You might also like