problem understanding a functionality of constructor.
-
i was going through a article about friend function and then in i came across this code:
#include
using namespace std;class Distance
{
private:
int meter;
public:
Distance(): meter(0) { }
//friend function
friend int addFive(Distance);
};// friend function definition
int addFive(Distance d)
{
//accessing private data from non-member function
d.meter += 5;
return d.meter;
}int main()
{
Distance D;
cout<<"Distance: "<< addFive(D);
return 0;
}in the above code what is
Quote:
Distance(): meter(0) { }
and how is a object of class distance is able to access a private member ?
Quote:
int addFive(Distance d) { //accessing private data from non-member function d.meter += 5; return d.meter; }
Thank you.
-
i was going through a article about friend function and then in i came across this code:
#include
using namespace std;class Distance
{
private:
int meter;
public:
Distance(): meter(0) { }
//friend function
friend int addFive(Distance);
};// friend function definition
int addFive(Distance d)
{
//accessing private data from non-member function
d.meter += 5;
return d.meter;
}int main()
{
Distance D;
cout<<"Distance: "<< addFive(D);
return 0;
}in the above code what is
Quote:
Distance(): meter(0) { }
and how is a object of class distance is able to access a private member ?
Quote:
int addFive(Distance d) { //accessing private data from non-member function d.meter += 5; return d.meter; }
Thank you.
The first uses the expression list of the constructor to initialise a member by passing to the member's constructor. See Constructors and member initializer lists - cppreference.com[^] for the various possible methods about member initialisation besides assigning in the compound statement (the final block enclosed by braces). The access to private and protected members is granted by the friend declaration - cppreference.com[^].
-
i was going through a article about friend function and then in i came across this code:
#include
using namespace std;class Distance
{
private:
int meter;
public:
Distance(): meter(0) { }
//friend function
friend int addFive(Distance);
};// friend function definition
int addFive(Distance d)
{
//accessing private data from non-member function
d.meter += 5;
return d.meter;
}int main()
{
Distance D;
cout<<"Distance: "<< addFive(D);
return 0;
}in the above code what is
Quote:
Distance(): meter(0) { }
and how is a object of class distance is able to access a private member ?
Quote:
int addFive(Distance d) { //accessing private data from non-member function d.meter += 5; return d.meter; }
Thank you.
-
i was going through a article about friend function and then in i came across this code:
#include
using namespace std;class Distance
{
private:
int meter;
public:
Distance(): meter(0) { }
//friend function
friend int addFive(Distance);
};// friend function definition
int addFive(Distance d)
{
//accessing private data from non-member function
d.meter += 5;
return d.meter;
}int main()
{
Distance D;
cout<<"Distance: "<< addFive(D);
return 0;
}in the above code what is
Quote:
Distance(): meter(0) { }
and how is a object of class distance is able to access a private member ?
Quote:
int addFive(Distance d) { //accessing private data from non-member function d.meter += 5; return d.meter; }
Thank you.