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.
#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;
}
Comments
Leave a comment