Page 1 of 1

Write a C++ program to create hierarchal inheritance to implement of odd or even numbers based on the user’s choice. Fir

Posted: Fri May 20, 2022 11:01 am
by answerhappygod
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”.