1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
jeka94
2 years ago
15

Given ProblemSolution class. Use a pointer to object of "ProblemSolution" class to invoke the "CalculateSum" and "Print" methods

of "ProblemSolution".
Write a function:
void Solution(int N1, int N2)

that accepts two integers N1 and N2. The function should instantiate "ProblemSolution" object with N1 and N2 values and call "CalculateSum" and "Print" method by creating a pointer to the object of "ProblemSolution".

Input
12 5

Output
17
Assume that,

N1, N2 and sum are integers within the range [-1,000,000,000 to 1,000,000,000].
Given Code to work with:

#include
#include
#include
using namespace std;

class ProblemSolution{
int N1,N2,result;
public:
ProblemSolution(int N1, int N2){
Computers and Technology
1 answer:
faltersainse [42]2 years ago
3 0

Answer:

#include <iostream>

using namespace std;

class  ProblemSolution {

private:

int num1, num2;

public:

ProblemSolution(int n1, int n2) {

 num1 = n1;

 num2 = n2;

}

int calculateSum() {

 int sum = 0;

 sum = num1 + num2;

 return sum;

}

void printSum() {

 // calculateSum will return sum value that will be printed here

 cout <<"Sum = "<< calculateSum();

}

~ProblemSolution() {

 cout << "\nDestructor is called " << endl;

};

};

int main() {

int a, b;

cout << "Enter a: ";

cin >> a;

cout << "Enter b: ";

cin >> b;

// Initiallizing object pointer of type ProblemSolution

ProblemSolution *objPtr = new ProblemSolution(a,b);

// printing Sum

objPtr->printSum();

// delete objPtr to relaease heap memory :important

delete objPtr;

return 0;

}

Explanation:

we will initialize  a pointer "objPtr" and initallize the constructor by passing 2 values a and b followed by the keyword  "new". the keyword "new" allocates memory in the heap. we can access class member functions using arrow "->". it is important to delete objPtr at the end of the program so that the heap memory can be freed to avoid memory leakage problems.  

You might be interested in
C programming question:
denpristay [2]

Answer:

You should do this:

if (0==strcmp(modelName,"Extravagant") && modelYear >= 1999 && modelYear <= 2002)

printf("%s\n","RECALL");

Explanation:

Lets consider this program and analyze the output.

int main()

{

int modelYear;

char modelName[30];

printf("enter the year");

scanf("%d",&modelYear);

printf("enter the modelname");

scanf("%s",modelName);

if (0==strcmp(modelName,"Extravagant") && modelYear >= 1999 && modelYear <= 2002)

printf("%s\n","RECALL");    }

if (0==strcmp(modelName,"Extravagant") && modelYear >= 1999 && modelYear <= 2002)

This statement has a function strcmp  which is used to compare two strings.

If the user enters Extravagant as model name then this input will be compared by the string stored in modelName i.e. Extravagant, character by character. If the string entered by the user matches the string stored in modelName this means this part of the if condition evaluates to true. 0==strcmp means that both the strings should be equal.

The next part of the if statement checks if the model year entered is greater than or equal to 1999 AND less than or equal to 2002. This means that the model year should be between 1999 and 2002 (inclusive).

&& is the logical operator between both the parts of if statement which means that both should evaluate to true in order to display RECALL.

This is correct way to use because the if (modelName == "Extravagant" && modelYear >= 1999 && modelYear <= 2002)  printf("%s\n","RECALL"); statement will not display RECALL. After entering the model name and model year it will move to the next line and exit.

So in if (0==strcmp(modelName,"Extravagant") && modelYear >= 1999 && modelYear <= 2002)

printf("%s\n","RECALL"); statement when the if condition evaluates to true, then RECALL is displayed in the output.

6 0
3 years ago
What does using indirect quotations allow a writer to do?
Marrrta [24]
Indirect quotations can add information that strengthens your content in many of the same ways as direct quotations so. Essentially, indirect quotes carry the meaning of a speaker or writer's original words without using the exact words.
7 0
3 years ago
Read 2 more answers
Which peripheral device is used to record sound?
faust18 [17]

he will need a microphone, it is a recording device.

8 0
2 years ago
Read 2 more answers
The process of executing a program is also called ________.
irga5000 [103]
It is also called running it.  Hope this helps!!!
5 0
2 years ago
Which of these skills are used in game design?
olasank [31]

Answer:

I think the answer is Writing but am not sure

8 0
2 years ago
Other questions:
  • What natural boundary separated british territory from louisiana?
    7·1 answer
  • What's the answer to the image? True or False
    15·1 answer
  • Evaluate the following expression with precedence of operator:<br> ​ X = 2* 3/ 5 + 10 //3 – 1
    13·1 answer
  • What does digital mean, in the term of computer science.
    11·1 answer
  • Anyone want to play mine mincraft w/ me?
    9·1 answer
  • Which of the following is not an operating system a) boss b) window xp c) linux d) bindux​
    15·1 answer
  • Which of the following statements about ip addresses is true?
    5·1 answer
  • Please help me asapppp!​
    8·2 answers
  • Which type of computer is used microprocessor​
    5·1 answer
  • A 4"x6" photo is digitized using 10,000 pixels. An 11"x7" photo is digitized using 30,000 pixels. Which image will have the bett
    6·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!