constructor and destructor

A

asit

1 #include <iostream>
2
3 using namespace std;
4
5 class my_class
6 {
7 int a;
8 int b;
9 public:
10 my_class()
11 {
12 a=0;
13 b=0;
14 cout<<"Default constructor"<<endl;
15 }
16 my_class(int x, int y):a(x),b(y)
17 {
18 cout<<"Parameterized constructor called"<<endl;
19 }
20 ~my_class()
21 {
22 cout<<"Destructor called"<<endl;
23 }
24 void put_data()
25 {
26 cout<<a<<" "<<b<<endl;
27 }
28
29 };
30
31 int main()
32 {
33 my_class ob1;
34 my_class ob2(10,20);
35 my_class ob3=my_class(100,300);
36 ob1.put_data();
37 ob2.put_data();
38 ob3.put_data();
39 ob1=my_class(1000,3000);
40 ob1.put_data();
41 return 0;
42 }

In the above program, will the destructor be called at line 39. Will
it be called before parameterized constructor or before ??
 
A

Alf P. Steinbach

* asit:
1 #include <iostream>
2
3 using namespace std;
4
5 class my_class
6 {
7 int a;
8 int b;
9 public:
10 my_class()
11 {
12 a=0;
13 b=0;
14 cout<<"Default constructor"<<endl;
15 }
16 my_class(int x, int y):a(x),b(y)
17 {
18 cout<<"Parameterized constructor called"<<endl;
19 }
20 ~my_class()
21 {
22 cout<<"Destructor called"<<endl;
23 }
24 void put_data()
25 {
26 cout<<a<<" "<<b<<endl;
27 }
28
29 };
30
31 int main()
32 {
33 my_class ob1;
34 my_class ob2(10,20);
35 my_class ob3=my_class(100,300);
36 ob1.put_data();
37 ob2.put_data();
38 ob3.put_data();
39 ob1=my_class(1000,3000);
40 ob1.put_data();
41 return 0;
42 }

In the above program, will the destructor be called at line 39. Will
it be called before parameterized constructor or before ??

Why don't you try this with your compiler?

By the way, for such investigations it might be a good idea to let the output
identify the relevant object.


Cheers, & hth.,

- Alf
 
E

Erik Wikström

1 #include <iostream>
2
3 using namespace std;
4
5 class my_class
6 {
7 int a;
8 int b;
9 public:
10 my_class()
11 {
12 a=0;
13 b=0;
14 cout<<"Default constructor"<<endl;
15 }
16 my_class(int x, int y):a(x),b(y)
17 {
18 cout<<"Parameterized constructor called"<<endl;
19 }
20 ~my_class()
21 {
22 cout<<"Destructor called"<<endl;
23 }
24 void put_data()
25 {
26 cout<<a<<" "<<b<<endl;
27 }
28
29 };
30
31 int main()
32 {
33 my_class ob1;
34 my_class ob2(10,20);
35 my_class ob3=my_class(100,300);
36 ob1.put_data();
37 ob2.put_data();
38 ob3.put_data();
39 ob1=my_class(1000,3000);
40 ob1.put_data();
41 return 0;
42 }

In the above program, will the destructor be called at line 39. Will
it be called before parameterized constructor or before ??

This sounds like something that is answered in the FAQ, take a look at
http://www.parashift.com/c++-faq-lite/how-to-post.html#faq-5.2 (actually
reading the whole of section 5 might be a good idea). Also you might
want to add a copy-constructor and assignment operator to my_class it
will give you a better idea of what is going on.
 
J

James Kanze


[...]
Why don't you try this with your compiler?
By the way, for such investigations it might be a good idea to
let the output identify the relevant object.

And to define and instrument the copy constructor as well.

Note too that implementations have a certain degree of freedom
with regards to how many copies they make, so the results may
differ between compilers.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,190
Members
46,736
Latest member
zacharyharris

Latest Threads

Top