Answer to Question #244771 in C++ for Zeeshan

Question #244771
#include<iostream>
using namespace std;
class add //Specifies the class
{
private:
int iNum1, iNum2, iNum3; //Member data
public:
void input(int iVar1, int iVar2) //Member function
{
cout<<"Functions to assign values to the member data"<<endl;
iNum1=iVar1;
iNum2=iVar2;
}
void sum(void) //Member function
{
cout<<"Functions to find the sum of two numbers"<<endl;
iNum3=iNum1+iNum2;
}
void disp(void) //Member function
{
cout<<"The sum of the two numbers is "<<iNum3<<endl;
}
};
/////////main function of the program///////////
void main()
{
add A1;
int iX, iY;
cout<<"Input two numbers"<<endl;
cin>>iX;
cin>>iY;
A1.input(iX, iY);
A1.sum();
A1.disp();
system("pause");
}
Code the example given above and check the errors if you try to access the private
data members in main() function.
Modify the above task by making the scope of public member functions as private.
Create access functions in public scope to access private member functions from
main().
1
Expert's answer
2021-09-30T04:46:29-0400
#include<iostream>


using namespace std;


class add //Specifies the class
{
private:
	int iNum1, iNum2, iNum3; //Member data
	void input(int iVar1, int iVar2) //Member function
	{
		cout<<"Functions to assign values to the member data"<<endl;
		iNum1=iVar1;
		iNum2=iVar2;
	}
	void sum(void) //Member function
	{
		cout<<"Functions to find the sum of two numbers"<<endl;
		iNum3=iNum1+iNum2;
	}
	void disp(void) //Member function
	{
		cout<<"The sum of the two numbers is "<<iNum3<<endl;
	}
public:
	void input(){
		int iX, iY;
		cout<<"Input two numbers"<<endl;
		cin>>iX;
		cin>>iY;
		input(iX,iY);
		
	}
	void report(){
		sum();
		disp();
	}
};


/////////main function of the program///////////


void main(){
	add A1;
	A1.input();
	A1.report();
	system("pause");


}

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
APPROVED BY CLIENTS