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 ClassSum{
private:
int x;
int y;
public:
ClassSum(int a,int b){
x=a;
y=b;
}
void calcuateSum(){
int sum=x+y;
cout<<"The sum of "<<x<<" and "<<y<<" is "<<sum;
}
};
int main()
{
ClassSum c(50,71);
c.calcuateSum();
return 0;
}
Comments
Leave a comment