Answer to Question #197762 in C++ for Asif

Question #197762

Write a program to initialize the data members through passing two parameters using a

constructor. Calculate their sum and print the output on the screen using a separate member

function of a class.


1
Expert's answer
2021-05-24T07:42:26-0400
#include <iostream>


using namespace std;


class Number{
  public:
    int a, b;
    Number(int x, int y){
        a = x;
        b = y;
    }
    void getSum(){
        int sum = a + b;
        cout <<"Sum: "<< sum ;
    }
};


int main()
{
    Number num(1,9);
    num.getSum();
    return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog