Call Destructor

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/*
Title		:	Lab Task 3 Question 3
Description :	Phone Call
Author		:	Lim Boon Jye
Date		:	6 September 2012
*/
#include <iostream>
#include <string>
#include<conio.h>
using namespace std;

class PhoneCall
{
private:
	string phNumber;
	double callLength , rateOfCharge;
public:
	PhoneCall();
	~PhoneCall();
	void setInput();
	void getPhone();
	PhoneCall(string);
	PhoneCall(string ,double,double);
};

//Default constructor
PhoneCall::PhoneCall()
{
	phNumber="";
	callLength   = 0;
	rateOfCharge = 0;
}

PhoneCall::~PhoneCall()
{
	cout<<"Object Destroyed ! "<<endl;
}

//Get user input
void PhoneCall::setInput()
{
	cout << "Phone Number   : ";
	cin  >> phNumber;
	cout << "Call of Length : ";
	cin  >> callLength;
	cout << "Rate Of Charge : ";
	cin  >> rateOfCharge;
}

//Show user Input
void PhoneCall::getPhone()
{
	cout << "The Phone number is    : "<< phNumber << endl;
	cout << "The Call of Length is : "<< callLength << endl;
	cout << "The Rate Of Charge is : "<< rateOfCharge << endl;
}

//Non-Default Constructor with parameters
PhoneCall::PhoneCall(string newNumber)
{
	phNumber = newNumber;
}

//Non-Default Constructor with parameters
PhoneCall::PhoneCall(string newNo ,double length , double rate)
{
	phNumber = newNo;
	callLength = length;
	rateOfCharge = rate;
}

int main()
{
	//Create object with array size of pointer 
	PhoneCall *thephone[5];
	

	thephone[0] = new PhoneCall("1231232");
	thephone[1] = new PhoneCall();
	thephone[2] = new PhoneCall();
	thephone[3] = new PhoneCall();
	thephone[4] = new PhoneCall();

	//Looping for user input
	for(int i = 0 ; i < 5 ; i++ )
	{
		cout << "Phone "<<i+1<<endl;
		thephone[i]->setInput();
		cout<<endl;
	}

	//Looping for show out user input
	for(int i = 0 ; i < 5 ; i++ )
	{
		cout << "Phone "<<i+1<<endl;
		thephone[i]->getPhone();
		cout<<endl;
	}

	
	getch();//Pause the window
	return 0;
}


The code is done , just want to ask how to call how the
1
2
~PhoneCall //Default destructor
~PhoneCall(string)//Non-Default destructor 


How to clal them out to cout my destructor in my code? thanks
Last edited on
The title says constructor but the question says destructor. There is only one kind of destructor and it takes no arguments.
closed account (o3hC5Di1)
Hi there,

You cannot call the destructor explicitly.
It is called automatically when the object is destroyed, such as when the program ends, when the function it is in ends, or when it was created with new and you use delete.

Hope that helps.

All the best,
NwN
NwN wrote:
You cannot call the destructor explicitly.

It is possible to call the destructor explicitly but as far as I know it's only useful when using placement new.
closed account (o3hC5Di1)
Hi there Peter,

Appologies - I thought it was not possible, could you please give an example? :)
Or do you mean you can call it explicitly by using delete ?

Thanks,
NwN
Peter and Nwn .

sorry for it .

if i call my constructor.
so how my destructor will be show out?

delete?

Can provide example? thanks
@NwN
1
2
3
4
char buffer[sizeof(PhoneCall)];
PhoneCall* pc = new (buffer) PhoneCall("abc", 123, 456);
pc->getPhone();
pc->~PhoneCall();


@BasicNewbie
The constructor is always called when the object is created. The destructor is always called when the object is destroyed. If you create the object with new you destroy with delete.
1
2
PhoneCall* pc = new PhoneCall; // default constructor is called here
delete pc; // destructor is called here 

If the object has automatic storage duration it will be destroyed when it goes out of scope at the end of the block.
1
2
3
4
{
	PhoneCall pc; // default constructor is called here
	...
} // destructor is called here because pc goes out of scope and will no longer exist. 
Last edited on
Thanks Peter . i done with my code and

easy to get understanding from u , thanks
Topic archived. No new replies allowed.