Write a program in C++ to display the n terms of odd natural number and their sum 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 | #include<iostream> using namespace std; class T4Tutorials { public: int s=0; }; class A:public virtual T4Tutorials { public: int i; }; class B:public virtual T4Tutorials { public: int n; }; class D: public A,B { public: void disp() { cout<<"Please enter the value : To print the odd Number."<<endl; cin>>n; for(int i=1;i<=n;i++) { cout<<2*i-1<<" "; s=s+(2*i-1); cout<<endl; } cout<<"sum of given input = "<<s; } }; int main() { D object; object.disp(); } |
Output
Output
Please enter the value : To print the odd Number.
5
1
3
5
7
9
Sum of given input = 25
FAQ
Write a program in C++ to display the n terms of even natural number and their sum by using the virtual base class in OOP.
Develop a C++ program to print odd numbers Sum using for loop by using the virtual base class in OOP.