Sum of the series Using Destructor in C++.
The destructor ~T4Tutorials_Series()
is a member function of the class T4Tutorials_Series
. Destructor ~T4Tutorials_Series()
has the same name as the name of its class T4Tutorials_Series
. The tild sign ∼ is used before the name of the destructor ~T4Tutorials_Series().
- When the object
obj(5)
of the classT4Tutorials_Series
destroyed, then the destructor~T4Tutorials_Series()
also destroyed automatically. - One class
T4Tutorials_Series
can have only one destructor~T4Tutorials_Series()
. However, one class can have many constructors. - Destructor
~T4Tutorials_Series()
overloading is impossible. - The Destructor
~T4Tutorials_Series()
can’t have any arguments(parameters). - The destructor
~T4Tutorials_Series()
has no data type.
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 | #include <iostream> using namespace std; class T4Tutorials_Series { public: T4Tutorials_Series (int number) { int k=9; int sum=0; cout<<"input the number of terms"; cin>>number; for(int i=1;i<=number;i++) { sum=sum+k; k=k*10+9; } cout<<"sum os T4Tutorials_Series"<<sum; } ~T4Tutorials_Series() { cout<<"construction complete its work, FREE MEMORY"<<endl; } }; int main() { T4Tutorials_Series obj(5); } |
Output
Please input the number of terms. 5
The sum of T4Tutorails_ Series is 111105.