float x, y; public:
point(float xn = 0, float yn=0)
{
x = xn; y=yn;
}
friend float masofa(point, line);
};
class line
{
float А, В, C;
public:
line(float a, float b, float c)
{
A=a; B=b; C = c;
}
friend float masofa(point, line);
};
float masofa(point P, line L)
{
return L.A*P.x + L.B*P.y + L.C;
};
int main()
{
point P(16.0,12.3); line L(10.0,-42.3, 24.0);
cout « "\n P nuqtasi L chiziqdan cheklanishi:"; cout« raasofa(P, L); return 0;
}
Dastur bajarilishi natijasi P nuqtasi L chiziqdan cheklanishi: -336.29009
Bir sinf ikkinchi sinfga dostona boiishi mumkin. Bu holda sinfning hamma komponenta funksiyalari boshqa sinfga dostona bo'ladi. Do stona sinf o‘zga sinf tanasidan tashqari ta'riflangan bo‘lishi lozim.
Do‘stona sinflarni ta'riflash: friend < sinf nomi >
Quyidagi misolda book sinfi librarian sinfini o'ziga dostona sinf deb belgilagan: class book {
public:
book(string, string, string); void show_book(void); friend librarian; private:
string title; string author; string catalog;
};
Shuning uchun librarian sinf obyektlari book sinfning xususiy elementlariga, nuqta operatoridan foydalangan holda, to g ridan to g ri murojaat etishi mumkin:
Do'stlaringiz bilan baham: |