#include <iostream>
usingnamespace std;
class Rectangle {
int width, height;
public:
Rectangle(int w = 1, int h = 1):width(w),height(h){}
friendvoid display(Rectangle &);
};
void display(Rectangle &r) {
cout << r.width * r.height << endl;
}
int main () {
Rectangle rect(5,10);
display(rect);
return 0;
{
first It defined a class (rectangle)that It has two private member,then we see two line inside public.
one is : Rectangle(int w = 1, int h = 1):width(w),height(h){}
i don't undestand nothing from it.what does it mean ?is this line function or constructor?
other is :
declaring friend function: friendvoid display(Rectangle &);
what does (Rectangle &) mean?why is Rectangle &؟ inside argument
and in the below code:
i don't understand arguments inside friend function.how do we name arguments for friend function?should not it be for example (int y ,float x)
plz explain me alot.i don't know nothing about arguments inside friend function.
tanx
I do not have any desire to answer your questions because you have no any desire to read any book on C++.
Please read a book on C++ or some materials on C++ that are present in net.
i don't undestand nothing from it.what does it mean ?is this line function or constructor?
A constructor is a function. A function is not necessarily a constructor. That one is a constructor
what does (Rectangle &) mean?why is Rectangle &؟ inside argument
It means "a reference to an object of type Rectangle". It is inside because the function takes a reference to an object of type Rectangle as parameter
i don't understand arguments inside friend function.how do we name arguments for friend function?
You don't need to give names to parameters of friend functions. The parameters names are mandatory only in the function definition. Since a friend function is only a declaration (telling the compiler that somewhere in the code there is a function, with a certain return type that takes a certain set of parameters, that can access the private members of the class it's decalred in) you can name its parameters however you want or not name them at all.
Rectangle(int w = 1, int h = 1):width(w),height(h){}
i don't undestand nothing from it.what does it mean ?is this line function or constructor?
It is both function and constructor. The declaration part Rectangle(int w = 1, int h = 1) says the constructor takes two arguments; w and h. w and h have default values of 1 which means if they're not supplied, 1 is assumed. The initialization portion : width(w),height(h) says that the member width is initialized to w and that height is initialized to h.
friend void display(Rectangle &r);
what does (Rectangle &) mean?
A friend function is global in scope and although it can reference members of the class, it is itself not a member of the class, therefore the Rectangle that is to be displayed must be passed as an argument. Note that I added r as an argument name. It's not required. Just a sylistic preference.
Again note that display is defined as a global function (it's not Rectangle::display).
The rectangle you want to display is passed as an argument. Note that because it was declared as a friend function, it can access width and height which are private members of r.
The classic friend function to learn is overloading the << operator in a class to write your class to an ostream.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
class A
{ int some_val;
public:
A () { some_val = 0; };
friend ostream & operator << (ostream & os, const A & a)
{ os << a.some_val;
return os;
}
};
This allows doing the following:
[code]
A a;
cout << a << endl;
If you don't understand the above, pick up a book and learn it.