Answer to Question #205464 in C++ for Amir khan

Question #205464

. 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-06-10T10:26:45-0400
#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;    
}

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