You are on page 1of 26

CONSTRUCTORS AND DESTRUCTORS

M.G.Karthikeyan Click to edit Master subtitle style NIT-Calicut

4/30/12

Constructors
• • •

some facts about constructors - 1 1.they are functions 2.they are functions declared in the public section of a class 3. they are functions declared in the public section of a class but cannot be called using dot operator 4. they are functions declared in the public section of a class. They cannot 4/30/12

they can be called without using dot operator outside the class definition explicitly 7.they can be called without using dot operator and it can be done so even outside the class definition 6. they can be called without using dot operator outside the class definition explicitly and they also get 4/30/12 called implicitly • • .Constructors • 5.

they are functions which cannot return anything 3.they are functions which cannot return anything including void 4/30/12 • • • .they are functions 2.Constructors • some more facts about constructors 3 1.

get :: ABC() ABCcalled implicitly(1.2.2.7) called implicitly • 4/30/12 . declared in public section(1.3 ABC(void).2. }public: 1.1. //constructor functions(1.7. //ABC() int var1.1) void fun(void).8.3.1.1.2) }.1.Constructors-Example-1 • • • • • • • int main() class ABC { ABC abc.

1.4) }.Constructors-Example-2 • • • • • • • int main() class ABC { ABC abc = ABC(). operator(1.4.5. //explicit call int var1.1. }public: 1.6 //constructor ABC(void). can beABC() from outside the class ABC :: called • • 4/30/12 . can be called without using dot void fun(void).

they are functions which can take arguments including default arguments 4/30/12 • • • .they are functions which can take arguments 3.they are functions 2.Constructors • some more facts about constructors 4 1.

Constructors-Example-3 • • • • • • • int main() class ABC { ABC abc(10).2 ABC(int). ABC :: ABC(int a) • 4/30/12 . }public: 4. (4. //now ABC() will be called int var1. //constructor constructors can take arguments void fun(void).2) }.

{ int var2.Constructors-Example-4 • • • • • • • void ABC classABC :: fun() { cout<<“hai2”.int y=2).3 void fun(void). • 4/30/12 .} int main() int var1. ABC abc(10). constructors can take default }. 4. //now ABC() will be called public: } ABC(int x.

} { int var1. ABC abc.} { var1 arguments class:: ABC(int x) ABC ABC { var1=x. • • 4/30/12 } ABC(int x=1). int main() public: { ABC().Constructors with default arguments • • • • • • • • • • • • ABC::ABC() caution while using constructors with default= 1. .

Constructors – why? • 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 built-in types gets initialized when declared we want such behavior in our userdefined data types 4/30/12 • • • Hence constructors .

constructors-some more facts • constructors can take arguments (parameterized constructors) constructors can take arguments including default arguments (constructors with default arguments) constructors can take arguments that are reference to it`s own class (copy constructors) 4/30/12 • • • there may be more than one .

such constructors are called copy constructors 4/30/12 • • .Parameterized constructors • parameterized constructors : constructors that take parameters parameters of a constructor can be of any type except that of the class to which it belongs but a constructor can accept a reference to its own class as a parameter.

int var1.Parameterized constructors : Ex-1 • • • • • • • int main() class ABC { ABC abc(10). ABC :: ABC(int a) { var1=a. } • 4/30/12 . error compilation }. }public: no ABC(int x).

int var1.var1. main() { public: XYZ xyz. ABC abc(xyz).Parameterized constructors : Ex-2 • • • • • • • ABC XYZ class:: ABC(XYZ xyz) { var1=xyz. no compilation error class ABC • 4/30/12 . } int int var2. } }. void fun(void).

{ public: ABC abc2. } }.var1. compilation error because abc2 is of • 4/30/12 . ABC abc(abc2).Parameterized constructors : Ex-3 • • • • • • • ABC ABC class:: ABC(ABC abc1) { var1=abc1. int var1. ABC(ABC abc1). } int main() int var2.

ABC abc(abc2).Copy constructors : Ex-1 • • • • • • • • • • • • • ABC constructor: constructor that copy:: ABC(ABC &abc1) can accept a reference to it`s own { var1=abc1. } class as a parameter int main() class ABC { { ABC abc2.var1. 4/30/12 . int var2. public: } int var1.

int var2.var1. } of declaration int main() class ABC { { ABC abc2. public: } int var1.Copy constructors : Ex-2 • • • • • • • • • • • • • ABC constructor: copy copy:: ABC(ABC &abc1)constructor gets called when initialization is part { var1=abc1. ABC abc = abc2. 4/30/12 .

• ABC abc = abc2.Copy constructors : Ex-3 ABC constructor: copy copy:: ABC(ABC &abc1)constructor does not get called } • { var1=abc1.var1. • public: • ABC abc3. when initialization is not part of declaration • int main() • class ABC • { • { • ABC abc2. • int var2. • int var1. //copy constructor does not get • • . • 4/30/12 abc3 = abc2.

Copy constructors • When no copy constructor is defined . the compiler supplies it`s own copy construtor 4/30/12 .

ABC abc2(10). • 4/30/12 . } public: int main() ABC(ABC abc1).var1. }. { ABC(int a).Constructor overloading – Ex-1 • • • • • • • ABC ABC class:: ABC(ABC abc1) { var1=abc1. ABC abc(abc2). { var1=a. } ABCint ABC(int a) :: var1.

that behave very similar to the built-in types built-in data types gets destroyed when they go out of scope we want such behavior in our userdefined data types 4/30/12 • • • Hence destructors .Destructors • one of the aims of C++ is to create user-defined data types such as class.

Destructors • • destructor is a member function destructor is a member function whose name is same as the class name destructor is a member function whose name is same as the class name but is preceded by a tilde • 4/30/12 .

Destructors – Example-1 • • • • • • • int main() class ABC { { int a. } • 4/30/12 . ~ABC(). ABC } ABC(int b). } }. ABC::ABC(int b) { a = b. cout<<“deleted”. o/p would be. public:abc(10).

o/p • • . • public: • } • ~ABC(). • cout<<“deleted”. • 4/30/12 would be. • { • } • int a.Destructors – Example-2 int main() destructors can be used to destroy objects that have not been created • { using constructors • { • class ABC • ABC abc.

END 4/30/12 .

 €°%% W n #f# W ° ¯f°%% W W  .

f n  .

%% $$ ½n nf W W  W nf° nf ¾°– ½ f% % W nf° nf €¯¾   nf¾¾ €°°% % W ½nnf% % .

.

°¾n¾ W W W W ¾¯ ¯ €fn¾f n°¾n¾    f €°n°¾   f €°n°¾nnf°f f–¯ °¾   f €°n°¾nnf°f f–¯ °¾ °n °– €ff–¯ °¾ .

.

°¾n¾ f¯½  W nf¾¾ .

W W ° f W ½ n  .

%°% $$n°¾n W W  €°% % W W  .

  .

%° f% W f f  W   .

 €°%% W n #f# W ° ¯f°%% W W  .

f n%% $$° .

%%  nf W W  W n°¾n¾nf°f  f–¯ °¾% % .

.

°¾n¾ f¯½  W nf¾¾ .

W W ° f W ° f W ½ n W  .

%°  °  %  W  €°% % W W  .

  .

%° f ° % W f f f  W   .

 €°%% W n #f# W ° ¯f°%% W W  .

f n%% $$° .

%%  nf W W  W n°¾n¾nf°f  €ff–¯ °¾% % .

.

°¾n¾ €ff–¯ °¾ W nf° ¾°– n°¾n¾ €f f–¯ °¾ W nf¾¾ .

W W ° f W ½ n W  .

%% W  .

%°  %  W W W W W W W W W W  .

 .

%% f   .

  .

%° % f   ° ¯f°%%  .

f n n¯½f°   nf¾  €f¯ –°  n°¾n nf .

.

°¾n¾ " W ° € f¯¾€.

++¾n f ¾  €°  ff½ ¾¾nf¾nf¾¾ f f   ¾¯f   °½ ¾ W  °½ ¾– ¾°f  ° nf W  f°¾n f°¾  €°  ff½ ¾ W  °n n°¾n¾ .

n°¾n¾ ¾¯ ¯ €fn¾ W n°¾n¾nf°f f–¯ °¾ %½ff¯   n°¾n¾% W n°¾n¾nf°f f–¯ °¾°n °– €ff–¯ °¾ %n°¾n¾ €ff–¯ °¾% W n°¾n¾nf°f f–¯ °¾ff   €  °n ¾°nf¾¾%n½n°¾n¾% W   ¯f ¯ f°° n°¾n°f nf¾¾%n°¾n f °–% .

9ff¯   n°¾n¾ W ½ff¯   n°¾n¾ n°¾n¾f f ½ff¯  ¾ W ½ff¯  ¾€fn°¾nnf° €f° ½  n ½f€ nf¾¾n °–¾ W fn°¾nnf°fnn ½f €  °n ¾ °nf¾¾f¾f½ff¯   ¾nn°¾n¾ f nf n½n°¾n¾ .

9ff¯   n°¾n¾   W nf¾¾ .

W W ° f W ½ n W  .

%° %  W W  .

  .

%° f% W f f  W ° ¯f°%% W W  .

f n%%  W W °n¯½f°  .

9ff¯   n°¾n¾   W nf¾¾O W W ° f W ½ n W ° f  W  €°% %  W W nf¾¾ .

W W ° f W ½ n W  .

%O%  W W  .

  .

%O% W f  f  W ° ¯f°%% W W O W  .

f n%%  W W °n¯½f°  .

9ff¯   n°¾n¾   W nf¾¾ .

W W ° f W ½ n W ° f W  .

% .

f n%  W W  .

  .

% .

f n% W f f n f  W ° ¯f°%% W W  .

f n W  .

f n%f n%  W W n¯½f°  nf¾ f n¾€½   .

.

.

½n°¾n¾   W n½n°¾n  n°¾nfnf° fnn ½f €  °n ¾ °nf¾¾f¾f½ff¯   W nf¾¾ .

W W ° f W ½ n W ° f W  .

% .

 f n%  W W  .

  .

% .

 f n% W f f n f  W ° ¯f°%% W W  .

f n W  .

f n%f n%  W .

.

½n°¾n¾   W n½n°¾n n½ n°¾n– ¾nf   °°ff°¾½f € nff° W nf¾¾ .

W W ° f W ½ n W ° f W  .

% .

 f n%  W W  .

  .

% .

 f n% W f f n f  W ° ¯f°%% W W  .

f n W  .

f n f n  W .

.

½n°¾n¾   W n½n°¾n n½ n°¾n  ¾°–  nf  °°ff° ¾°½f€ nff° W nf¾¾ .

W W ° f W ½ n W ° f W  .

% .

 f n%  W W  .

  .

% .

 f n% W f f n f  W ° ¯f°%% W W  .

f n W  .

f n f n  W  .

f n W f n f n $$n½ n°¾n  ¾°– nf W .

.

½n°¾n¾ W J °°n½n°¾n¾ €°    n¯½ ¾½½ ¾¾°n½n°¾ .

.

°¾n f °–   W nf¾¾ .

W W ° f W ½ n W  .

% .

f n%  W  .

%° f% W W W W W W W W W W  .

  .

% .

f n% f f n f   .

  .

%° f% f f  ° ¯f°%%  .

f n%%  .

f n%f n%  .

 ¾n¾ W ° € f¯¾€.

++¾n f ¾  €°  ff½ ¾¾nf¾nf¾¾ f f   ¾¯f   °½ ¾ W  ° ff½ ¾– ¾ ¾  °  –€¾n½ W  f°¾n f°¾  €°  ff½ ¾ W  °n  ¾n¾ .

 ¾n¾ W ¾n¾f¯ ¯ €°n° W ¾n¾f¯ ¯ €°n°¾ °f¯  ¾¾f¯ f¾ nf¾¾°f¯ W ¾n¾f¯ ¯ €°n°¾ °f¯  ¾¾f¯ f¾ nf¾¾°f¯  ¾½ n  f  .

 ¾n¾ f¯½  W nf¾¾ .

W W ° f W ½ n W  .

%° % W  .

%% W W  .

 .

%° % f   W  .

 .

%% W n #    # W W W W W W W W W W ° ¯f°%%  .

f n%% n #   # $½        .

 ¾n¾ f¯½  W ¾n¾nf° ¾  ¾ © n¾ff  ° °n f ¾°– n°¾n¾ W nf¾¾ .

W W ° f W ½ n W  .

%% W W  .

 .

%% W n #    # W W W W W W W W W W ° ¯f°%%  .

f n n #   # $½        .

- .