Create a class A with data member to store a array of N integers. Define a default constructor to read the array. Define a member function show() that will print the integer array.Create a derived class B with a member function show() that will print the maximum value in the integer array.Make the member finction show() in base class as virtual.Create objects and pointers to access the member functions.Sample Input5 (Read N)1297343Sample Output34
Question
Create a class A with data member to store a array of N integers. Define a default constructor to read the array. Define a member function show() that will print the integer array.Create a derived class B with a member function show() that will print the maximum value in the integer array.Make the member finction show() in base class as virtual.Create objects and pointers to access the member functions.Sample Input5 (Read N)1297343Sample Output34
Solution
Here is a step-by-step guide to creating the classes in C++:
- First, we will create the base class
A:
#include <iostream>
#include <vector>
#include <algorithm>
class A {
protected:
std::vector<int> arr;
public:
A(int n) {
arr.resize(n);
for(int i = 0; i < n; i++) {
std::cin >> arr[i];
}
}
virtual void show() {
for(int i = 0; i < arr.size(); i++) {
std::cout << arr[i] << " ";
}
std::cout << std::endl;
}
};
- Next, we will create the derived class
B:
class B : public A {
public:
B(int n) : A(n) {}
void show() override {
int max_val = *std::max_element(arr.begin(), arr.end());
std::cout << max_val << std::endl;
}
};
- Finally, we will create objects and pointers to access the member functions:
int main() {
int n;
std::cin >> n;
A* a = new A(n);
a->show();
B* b = new B(n);
b->show();
delete a;
delete b;
return 0;
}
In this code, we first create an object of class A and call the show() function, which prints the array. Then, we create an object of class B and call the `
Similar Questions
Write a C program that takes an array of integers and its size as input and returns the maximum element in the array.Requirements:The function should be named findMax.The function should take two parameters: an integer array and its size.The function should return the maximum element in the array.
ai is creating a program to find the maximum number from the given two integers using pointers.Help him with the task.Input format :The input consists of two space-separated integers.Output format :The output prints the maximum number.Code constraints :In this scenario, the test cases fall under the following constraints:1 ≤ input integers ≤ 100Sample test cases :
Problem StatementYou are given a function,static String FindMax(int[] arr){}The function accepts an integer array 'arr' of length 'size' as its argument. Implement the function to find and return the maximum number that can be formed by any permutation or arrangement of all the digits obtained from all the numbers present in the array. You have to return the number formed as a string.Note: You may need to rearrange the digits of the numbers to form the maximum number.Example:Input:34 79 58 64Output:98765443Explanation:All digits obtained from all the numbers of array are 3, 4, 7, 9, 5, 8, 6, 4. Maximum number obtained after rearranging all these digit gives 98765443. Thus, output is 98765443.The custom input format for the above case:434 79 58 64(The first line represents the 'size', the second line represents the elements of the array 'arr')Sample input21 90 23Sample Output932210The custom input format for the above case:321 90 23(The first line represents the 'size', the second line represents the elements of the array 'arr')Instructions :This is a template based question, DO NOT write the "main" function.Your code is judged by an automated system, do not write any additional welcome/greeting messages."Save and Test" only checks for basic test cases, more rigorous cases will be used to judge your code while scoring.Additional score will be given for writing optimized code both in terms of memory and execution time.Now let's start coding :Language:Read-only code below . . .1class SolutionClass {2 public static void main(String[] args) throws java.lang.Exception {3 //Input read from STDIN4 String result = FindMax(arr);5 //Value in result printed to STDOUT6 }7}8Write your code below . . .9static String FindMax(int[] arr) throws java.lang.Exception10{11 /* Write your code here. */12}13
Given two arrays (arr1[], arr2[]) of integers, display the largest number in arr1, where that element should not be present in arr2. If the constraint is not satisfied return 0.Variable Constraints:Size of the array <= 5;Array data type = integer.Input Size of array 1Elements of array 1Size of array 2Elements of array 2OutputLargest element in array 1
a) Write java code statements to accomplish the following. [5 Marks]i) declares following variables: ‘Max and Minii) Initializes the two variables above with values 5 and 3 respectivelyiii) Determines largest variable amongst the two aboveiv) Prints results of the largest variable:v) Declare and initialize array
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.