Virtual base class C++ program sum of an A.P. series
Write a C++ program to find out the sum of an A.P. series by using the virtual base class in object oriented programming.
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 |
#include<iostream> using namespace std; class T4Tutorials_Grand_Class { public : int n1,df; }; class p1:virtual public T4Tutorials_Grand_Class { public : int ln; }; class p2:virtual public T4Tutorials_Grand_Class { public : int s1; }; class derived : public p1 , public p2 { protected : int n2, i; public : int apseries() { s1 = 0; cout << " Find out the sum of A.P. series :”<<endl; 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 >> 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 << " + “<<endl; else cout << i << " = " << s1 << endl; } } }; int main() { derived a; a.apseries(); } |