Write a c++ program to find out the sum of an A.P. series by using the constructor overloadin.
Write a c++ program to find out the sum of an A.P. series by using the constructor overloading.
The concept of using more than one constructor with the same name is called constructor overloading.
In this program, the constructor must obey one or both of the following rules.
- All constructors with the same name and having a different number of parameters.
T4Tutorials_Constructor()
and another constructor asT4Tutorials_Constructor(int n1, int n2,int T4Tutorials_Difference)
.
- All constructors with the same name and have the same number of parameters but of different data types.
T4Tutorials_Constructor(int n1, int n2,int T4Tutorials_Difference)
and another constructor as4Tutorials_Constructor(double n1, double n2, double T4Tutorials_Difference)
.
Note: In this example, we are overloading the constructor with the following rule;
One constructor as T4Tutorials_Constructor()
and another constructor as T4Tutorials_Constructor(int n1, int n2,int T4Tutorials_Difference)
.
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 |
#include<iostream> using namespace std; class T4Tutorials_Constructor { protected : int n1, T4Tutorials_Difference, n2, i, ln, s1; public : T4Tutorials_Constructor() { s1 = 0; cout << "Input the starting number of the A.P. series: "<<endl; cin >> n1; cout << "Input the number of items for the A.P. series: "<<endl; cin >> n2; cout << "Input the common difference of A.P. series: "<<endl; cin>>T4Tutorials_Difference; s1 = (n2 * (2 * n1 + (n2 - 1) * T4Tutorials_Difference)) / 2; ln = n1 + (n2 - 1) * T4Tutorials_Difference; cout << "The Sum of the A.P. series are : "<<endl; for (i = n1; i <= ln; i = i + T4Tutorials_Difference) { if (i != ln) cout << i << " + "; else cout << i << " = " << s1 << endl; } } T4Tutorials_Constructor(int n1, int n2,int T4Tutorials_Difference) { s1 = 0; s1 = (n2 * (2 * n1 +
(n2 - 1) * T4Tutorials_Difference)) / 2; ln = n1 + (n2 - 1) * T4Tutorials_Difference; cout << "The Sum of the A.P. series are : "<<endl; for (i = n1; i <= ln; i = i + T4Tutorials_Difference) { if (i != ln) cout << i << " + "; else cout << i << " = " << s1 << endl; } } }; int main() { int option; cout<<"Please Enter the 1 for T4Tutorials_Constructor with No paranter :"<<endl; cout<<"Please Enter the 2 for T4Tutorials_Constructor with Paramter : \n"; cout<<"Please Enter the 1 or 2 here : "; cin>>option; system("cls"); if(option==1) { cout<<"You Have Slected No Paramter T4Tutorials_Constructoructor.... "<<endl; T4Tutorials_Constructor a; } else if(option==2) { int n1,n2,T4Tutorials_Difference; cout << "Input the starting number of the A.P. series: "<<endl; cin >> n1; cout << "Input the number of items for the A.P. series: "<<endl; cin >> n2; cout << "Input the common difference of A.P. series: "<<endl; cin>>T4Tutorials_Difference; T4Tutorials_Constructor (n1,n2,T4Tutorials_Difference); } else { cout<<"Your Input is Wrong : Try Again."<<endl; } } |