# Write a c++ program to find out the sum of an A.P. series using constructor and destructor.

Write a c++ program to find out the sum of an A.P. series using constructor and destructor.

## Constructor

The constructor `T4Tutorials_AP_Series()`

is a member function of the class `T4Tutorials_AP_Series`

. The constructor `T4Tutorials_AP_Series()`

has the same name as the name of its class `T4Tutorials_AP_Series`

.

- When a new object
`T4Tutorials_AP_Series a;`

of the class`T4Tutorials_AP_Series`

is executed, the constructor`T4Tutorials_AP_Series()`

also executed automatically. - The constructor
`T4Tutorials_AP_Series()`

has no data type. Even we can’t use void also. - The constructor
`T4Tutorials_AP_Series()`

can have arguments. - The constructor
`T4Tutorials_AP_Series()`

can be only public. - There is no inheritance of the constructor
`T4Tutorials_AP_Series()`

.

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 |
#include<iostream> using namespace std; class T4Tutorials_AP_Series { protected : int n1, df, n2, i, ln, s1; public : T4Tutorials_AP_Series() { s1 = 0; cout << "Please input the starting number of the A.P. series: "<<endl; cin >> n1; cout << "Please input the number of items for the A.P. series: "<<endl; cin >> n2; cout << "Please input the common difference of A.P. series: "<<endl; cin >> df; s1 = (n2 * (2 * n1 + (n2 - 1) * df)) / 2; ln = n1 + (n2 - 1) * df; cout << "The Sum of the A.P. series are : "<<endl; for (i = n1; i <= ln; i = i + df) { if (i != ln) cout << i << " + "; else cout << i << " = " << s1 << endl; } } }; int main() { T4Tutorials_AP_Series a; } |

## Destructor

The destructor `~T4Tutorials_AP_Series()`

is a member function of the class `T4Tutorials_AP_Series`

. Destructor `~T4Tutorials_AP_Series()`

has the same name as the name of its class `T4Tutorials_AP_Series`

. The tild sign ∼ is used before the name of the destructor `~T4Tutorials_AP_Series()`

.

- When the object
`T4Tutorials_AP_Series a;`

of the class`T4Tutorials_AP_Series`

destroyed, then the destructor`~T4Tutorials_AP_Series()`

also destroyed automatically. - One class
`T4Tutorials_AP_Series`

can have only one destructor`~T4Tutorials_AP_Series()`

. However, one class can have many constructors. - Destructor
`~T4Tutorials_AP_Series()`

overloading is impossible. - The Destructor
`~T4Tutorials_AP_Series()`

can’t have any arguments(parameters). - The destructor
`~T4Tutorials_AP_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 28 29 30 31 32 33 |
#include<iostream> using namespace std; class T4Tutorials_AP_Series { protected : int n1, df, n2, i, ln, s1; public : ~T4Tutorials_AP_Series() { s1 = 0; cout << "Please input the starting number of the A.P. series: "<<endl; cin >> n1; cout << "Please input the number of items for the A.P. series: "<<endl; cin >> n2; cout << "Please input the common difference of A.P. series: "<<endl; cin >> df; s1 = (n2 * (2 * n1 + (n2 - 1) * df)) / 2; ln = n1 + (n2 - 1) * df; cout << "The Sum of the A.P. series are : "<<endl; for (i = n1; i <= ln; i = i + df) { if (i != ln) cout << i << " + "; else cout << i << " = " << s1 << endl; } } }; int main() { T4Tutorials_AP_Series a; } |