. 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>
class TestClass
{
int a_;
int b_;
public:
TestClass(int a, int b) : a_(a), b_(b)
{
}
void PrintSum() const
{
std::cout << (a_ + b_) << "\n";
}
};
int main()
{
TestClass test(1, 2);
test.PrintSum();
return 0;
}
Comments
Leave a comment