Write a C++ program to create hierarchal inheritance to implement of odd or even numbers based on the user's choice. Fir
-
- Site Admin
- Posts: 899603
- Joined: Mon Aug 02, 2021 8:13 am
Write a C++ program to create hierarchal inheritance to implement of odd or even numbers based on the user's choice. Fir
Write a C++ program to create hierarchal inheritance to implement of odd or even numbers based on the user's choice. First, create a base class numbers with one public member data 'n' and one member function read() to read the value for 'n' from the user. Second, create the derived_class_1 from base class and have a public member function odd_sum() to calculate sum of odd numbers until 'n' value and print the result. Third, create the derived_class_2 from base class and have a public member function even_sum() to calculate sum of even numbers until 'n' value and print the result. Note: - Write a main function that print either sum of odd or even numbers until 'n' values: Take the choice from the user to calculate and print the sum of odd or even numbers until 'n' values. (1 - sum of odd numbers until 'n' values.) or (2 - sum of even numbers until 'n' values) Create an object to both of the derived classes and use the corresponding object to calculate and print the sum of odd or even numbers until 'n' values as per user's choice. You may decide the type of the member data as per the requirements. Output is case sensitive. Therefore, it should be produced as per the sample test case representations.
'n' and choice should be positive only. Choice should be either 1 or 2. Otherwise, print "Invalid". In samples test cases in order to understand the inputs and outputs better the comments are given inside a particular notation (.....). When you are inputting get only appropriate values to the corresponding attributes and ignore the comments (.......) section. In the similar way, while printing output please print the appropriate values of the corresponding attributes and ignore the com (.......) section Sample test cases:- case-one input=5 ('n' value) 1 (choice to perform sum of odd numbers until 'n' values (1+3+5)) output=9 grade reduction=15% case=two input=5 ('n' value) 3 (choice) output=Invalid
case=two input=5 ('n' value) 3 (choice) output=Invalid grade reduction=15% case=three input=-5 ('n' value) 2 (choice) output=Invalid grade reduction=15% case=four input=5 ('n' value) 2 (choice to perform sum of even numbers until 'n' values (2+4)) output=6 grade reduction=15%
case=four input=5 ('n' value) 2 (choice to perform sum of even numbers until 'n' values (2+4)) output=6 grade reduction=15% case=five input=5 ('n' value) 3 (Wrong choice) output=Invalid grade reduction=15%