Question

IN C++ Given ProblemSolution class. Use a pointer to object of “ProblemSolution” class to invoke the...

IN C++

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<iostream>
#include<cstdio>
#include<cstdlib>
using namespace std;

class ProblemSolution{
int N1,N2,result;
public:
ProblemSolution(int N1, int N2){
this->N1=N1;
this->N2=N2;
}
void CalculateSum();
void print();
};

void ProblemSolution :: CalculateSum(){
result=N1+N2;
}

void ProblemSolution :: print(){
cout<<result;
}

void solution(int N1, int N2){

   //write your code here

}

int main()
{
int N1, N2;
cin>>N1;
cin>>N2;
solution(N1,N2);
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Program:

#include<iostream>
#include<cstdio>
#include<cstdlib>
using namespace std;

class ProblemSolution{
int N1,N2,result;
public:
ProblemSolution(int N1, int N2){
this->N1=N1;
this->N2=N2;
}
void CalculateSum();
void print();
};

void ProblemSolution :: CalculateSum(){
result=N1+N2;
}

void ProblemSolution :: print(){
cout<<result;
}

void solution(int N1, int N2)
{

ProblemSolution *ps=new ProblemSolution(N1,N2);
ps->CalculateSum();
ps->print();

}

int main()
{
int N1, N2;
cin>>N1;
cin>>N2;
solution(N1,N2);
}

Output:

12 5 17 ... Program finished with exit code o Press ENTER to exit console.

Add a comment
Know the answer?
Add Answer to:
IN C++ Given ProblemSolution class. Use a pointer to object of “ProblemSolution” class to invoke the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • CC++ Compiler: CPP (gcc-5.x) 0 Simple Calculator Given two integers and a string, create a simple...

    CC++ Compiler: CPP (gcc-5.x) 0 Simple Calculator Given two integers and a string, create a simple calculator which performs the following operations: • Addition if the string is "+" • Difference if the string is "-" Multiplication if the string is "*" • Return quotient (obtained by dividing the first number by the second) if the string is "/" • Return greater value if the string is "max" • Return lesser value if the string is "min" solution.cpp 2 Create...

  • Given two complex numbers, find the sum of the complex numbers using operator overloading. Write an...

    Given two complex numbers, find the sum of the complex numbers using operator overloading. Write an operator overloading function     ProblemSolution operator + (ProblemSolution const &P) which adds two ProblemSolution objects and returns a new ProblemSolution object. Input     12 -10      -34 38     where, Each row is a complex number. First element is real part and the second element is imaginary part of a complex number. Output     -22 28 Two complex numbers are 12-10i and -34+38i. Sum of complex numbers are =...

  • PLEASE USE C++ Source Code Attached is a linked list with 2 nodes. You can use...

    PLEASE USE C++ Source Code Attached is a linked list with 2 nodes. You can use this or write a similar one. The assignment is to write 2 functions. One function will add another node at the end of the list. The other function will delete a node. Don't forget - No dangling pointers ! Example linked source code attached below #include<iostream> using namespace std; class Node { int data; Node *next; public: void setdata(int d) {data = d;} void...

  • Fix the following code to print whether the input is positive or negative #include <iostream> #include...

    Fix the following code to print whether the input is positive or negative #include <iostream> #include <string> using namespace std; void positiveOrNegative(int); // declare function int main(){    int y; int result; cin >> y; result = positiveOrNegative(y);    return 0; } void positiveOrNegative(int x){ if (x>=0) cout << "Positive"; else cout << "Negative"; }

  • Fix the code to print the "exact" result of the division of a/b (Pass by reference)...

    Fix the code to print the "exact" result of the division of a/b (Pass by reference) #include <iostream> using namespace std; void division (int& a, int& b, int& result) {    result = a/b;    } int main () { float a, b, result;    cin >> a; cin >> b;    division(a, b, result); cout << "result = " << result << endl; return 0; }

  • Make an abstract class Number that allows the rest of the code to work correctly. printDoubled...

    Make an abstract class Number that allows the rest of the code to work correctly. printDoubled should be able to take either an Int or a Double and print out twice its value. #include <iostream> using namespace std; //Do not modify anything on or above the line below this //YOUR_CODE_BELOW //YOUR_CODE //YOUR_CODE_ABOVE //Do not modify anything on or below the line above this void printDoubled(const Number& n) { cout << n.getValue() * 2 << endl; } class Int : public...

  • C++ Language I have a class named movie which allows a movie object to take the...

    C++ Language I have a class named movie which allows a movie object to take the name, movie and rating of a movie that the user inputs. Everything works fine but when the user enters a space in the movie's name, the next function that is called loops infinetly. I can't find the source of the problem. Down below are my .cpp and .h file for the program. #include <iostream> #include "movie.h" using namespace std; movie::movie() { movieName = "Not...

  • 10.18 LAB: Plant information (vector) Given a base Plant class and a derived Flower class, complete...

    10.18 LAB: Plant information (vector) Given a base Plant class and a derived Flower class, complete main() to create a vector called myGarden. The vector should be able to store objects that belong to the Plant class or the Flower class. Create a function called PrintVector(), that uses the PrintInfo() functions defined in the respective classes and prints each element in myGarden. The program should read plants or flowers from input (ending with -1), adding each Plant or Flower to...

  • Do not use pointer variables, pointer arithmetic or operater new ITCS-2530    Project                        Write an...

    Do not use pointer variables, pointer arithmetic or operater new ITCS-2530    Project                        Write an interactive program that performs fractional calculations. You need define a class CFrac for a fractional number according to the UML diagram below: CFrac - numerator : int - denominator : int - simplify () : void + CFrac (int = 0, int = 1) + ~CFrac() + add (const CFrac&) const : CFrac + subtract (const CFrac&) const : CFrac + multiply (const CFrac&)...

  • 2 Class Vec Message1 2.1 Introduction • Write a class Vec Message1 with the following declaration....

    2 Class Vec Message1 2.1 Introduction • Write a class Vec Message1 with the following declaration. class Vec_Message1 { public: Vec_Message1(); Vec_Message1(int n); Vec_Message1(int n, const Message1 &a); Vec_Message1(const Vec_Message1 &orig); Vec_Message1& operator= (const Vec_Message1 &rhs); ~Vec_Message1(); int capacity() const; int size() const; Message1 front() const; Message1 back() const; void clear(); void pop_back(); void push_back(const Message1 &a); Message1& at(int n); private: void allocate(); void release(); int _capacity; int _size; Message1 * _vec; }; 2.2 allocate() and release() • Examine the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT