Question

In: Computer Science

Single inheritance Given two integer numbers N1 and N2, create a class 'ProblemSolution' with following characteristics....

Single inheritance

Given two integer numbers N1 and N2, create a class 'ProblemSolution' with following characteristics.

  1. Must extend a 'Base' class.
  2. Create a method 'solution' with two integer parameters (N1 and N2) and long return type.
  3. The 'solution' method should call the addition and subtraction methods of 'Base' class and return multiplication of returned results of addition and subtraction method.

Result = (N1 + N2) * (N1 - N2)

What is inheritance?
Inheritance is a mechanism in which one object acquires all the properties and behaviors of the parent object.
Reference: https://www.tutorialspoint.com/cplusplus/cpp_inheritance.htm

Input
    50
    10

Output
    2400

Given:

#include <iostream>
using namespace std;

// Base class
class Base {
public:
int addition(int N1, int N2) {
int result = N1 + N2;
return result;
}
  
int subtraction(int N1, int N2) {
int result = N1 - N2;
return result;
}
  

};

   //write your code here

int main() {
int N1;
int N2;
cin >> N1;
cin >> N2;
ProblemSolution problemSolution;
cout << problemSolution.solution(N1,N2);
return 0;
}

C++

Must Run:

Both numbers being 0

1 negative and 1 positive number

both negative numbers

Large numbers

Solutions

Expert Solution

I have included my code and screenshots in this answer. In case, there is any indentation issue due to editor, then please refer to code screenshots to avoid confusion.

-------------------main.cpp-------------------

#include <iostream>
#include <string>
using namespace std;

// Base class
class Base
{
   public:
   int addition(int N1, int N2) //adds two numbers
   {
       int result = N1 + N2;
       return result;
   }

   int subtraction(int N1, int N2) //subtracts two numbers
   {
       int result = N1 - N2;
       return result;
   }
};

//write your code here
class ProblemSolution: public Base //Class ProblemSolution inherited from Base class
{
   public:
       long solution(int N1, int N2)
       {
           long ans;
           ans = (long)Base::addition(N1, N2) * (long)Base::subtraction(N1, N2); //(N1+N2)*(N1-N2)
           return ans;
       }
};

int main()
{
   int N1;
   int N2;
   cin >> N1;
   cin >> N2;
   ProblemSolution problemSolution; //object of ProblemSolution class
   cout << problemSolution.solution(N1,N2) << endl;
   return 0;
}

-------------------Screenshot main.cpp-------------------

-------------------Output----------------------

--------------------------------------------------------

I hope this helps you,

Please rate this answer if it helped you,

Thanks for the opportunity


Related Solutions

C++ coding plase Given three integer numbers N1, N2 and N3. Write 2 overloaded functions which...
C++ coding plase Given three integer numbers N1, N2 and N3. Write 2 overloaded functions which perform addition of passed values. Write two functions inside 'ProblemSolution' class: Function 1: solution that accepts two parameters, N1 and N2. A function should return addition of those numbers. Function 2: solution that accepts three parameters, N1, N2 and N3. A function should return addition of 3 numbers. Input     2     3     4 Output     5     9 Where, The first line of output contains addition of...
Write a program in java which randomly generates two integer number n1 and n2 (suppose the...
Write a program in java which randomly generates two integer number n1 and n2 (suppose the range for each integer is [1, 100]), then asks the user what is the value of n1*n2, if the user’s answer is correct, call method printGoodComment to print out something nice, otherwise, call printBadComment to print out something “mean”. The method signatures are:                   public static void printGoodComment() and                   public static void printBadComment() in your printGoodComment method, it will randomly print one sentence from the...
Write a program in java which randomly generates two integer number n1 and n2 (suppose the...
Write a program in java which randomly generates two integer number n1 and n2 (suppose the range for each integer is [1, 100]), then asks the user what is the value of n1*n2, if the user’s answer is correct, call method printGoodComment to print out something nice, otherwise, call printBadComment to print out something “mean”. The method signatures are:                   public static void printGoodComment() and                   public static void printBadComment() in your printGoodComment method, it will randomly print one sentence from the...
Say that two users use n1, n2 in the RSA and gcd(n1, n2) =/= 1. How...
Say that two users use n1, n2 in the RSA and gcd(n1, n2) =/= 1. How can we break their system?
Given two integers N1 and N2, display the given box number pattern of '1's with a...
Given two integers N1 and N2, display the given box number pattern of '1's with a '0' at the center (Square number patterns). Input: 5 5     where: First line represents the value of N1( number of rows ). Second line represents the value of N2( number of columns ).
Let n be a positive integer. Prove that two numbers n2+3n+6 and n2+2n+7 cannot be prime...
Let n be a positive integer. Prove that two numbers n2+3n+6 and n2+2n+7 cannot be prime at the same time.
Given two independent random samples with the following results: n1   463   n2   295 p1   0.77   p2  ...
Given two independent random samples with the following results: n1   463   n2   295 p1   0.77   p2   0.89 Use this data to find the 99%99% confidence interval for the true difference between the population proportions. Step 1 of 3: Find the critical value that should be used in constructing the confidence interval. Step 2 of 3: Find the value of the standard error. Round your answer to three decimal places. Step 3 of 3: Construct the 99% confidence interval. Round your...
- Create a list with 40 integer random numbers - With a function (def) create two...
- Create a list with 40 integer random numbers - With a function (def) create two new lists from the list created by random numbers, in which on one are the even elements and on the other the odd elements - Create two variables with the length of both new lists and print the variables All exercices must be done in Phyton
1. Given with the following sample information n1 = 125, n2 = 120; s1 = 31,...
1. Given with the following sample information n1 = 125, n2 = 120; s1 = 31, s2 = 38; x1-bar = 116, x2-bar = 105 Test the following hypotheses, assuming a significance level of 0.05 is to be used with equal variances. Ho : µ1 - µ2 = 0 HA : µ1 - µ2 ≠ 0 What is your conclusion? a) Since t test statistic = 1.82 < t-critical value = 2.49, we do not reject Ho. b) Since t...
In a preschool class of n, exactly n1 children are needed for activity 1, n2 for...
In a preschool class of n, exactly n1 children are needed for activity 1, n2 for activity 2, and n3 for activity 3. Luckily n = n1 + n2 + n3. The teachers want to know in how many distinct ways the children can be assigned into these activities. (Two assignments are distinct if at least one student is in a different activity in each.) (a) They figure they could start by lining up the children arbitrarily. How many different...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT