Question #244772

#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 include a private constructor in the class. Create
objects of this class. Test the code and write down how the constructor will be called
or unable to be called?

Expert's answer

The private constructor can be called in the main function by making the main() function a friend function the class add.

This is shown below:

#include<iostream>


using namespace std;


class add //Specifies the class


{


private:
add(){
	
}
int iNum1, iNum2, iNum3; //Member data
add(int a, int b, int c){
	iNum1  =a;
	iNum2  =b;
	iNum3  =c;
}
public:
friend int main();
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///////////


int 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");


}

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!

LATEST TUTORIALS
APPROVED BY CLIENTS