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");
}
Comments
Leave a comment