In: Computer Science
Single inheritance
Given two integer numbers N1 and N2, create a class 'ProblemSolution' with following characteristics.
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
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