Write a program in C++ to find the sum of the series by using the constructor and 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 |
#include<iostream> #include<math.h> using namespace std; class T4Tutorials_Series_Class { private: int a,sum,c; int i,n,x,T4Tutorials_Variable1,T4Tutorials_Variable2; public: T4Tutorials_Series_Class() { cout<<"enter the value of a="; cin>>a; cout<<"enter the number of term"; cin>>n; sum=a; x=-1; cout<<"the value of T4Tutorials_Series_Class\n"; cin>>a; } void sol() { for(i=1;i<n;i++) c=(2*i+1); T4Tutorials_Variable1= pow(a,c); T4Tutorials_Variable2=T4Tutorials_Variable1*x; cout<<"y"<<T4Tutorials_Variable2<<endl; sum=sum+T4Tutorials_Variable2; x=x*(-1); cout<<"\n sum of T4Tutorials_Series_Class"<<sum; } ~ T4Tutorials_Series_Class() { cout<<endl<<"object destroyed"; } }; int main() { T4Tutorials_Series_Class obj; obj.sol(); } |
Output